
    3fi-                        d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZmZ  ej$                  e      Z	 ddlmZ eefZ G d d      Zy)a  
A minimal scheduler to schedule tasks to run in the future (async version).

Inspired to the standard library `sched.scheduler`, but designed for
multi-thread usage from the ground up, not as an afterthought. Tasks can be
scheduled in front of the one currently running and `Scheduler.run()` can be
left running without any tasks scheduled.

Tasks are called "Task", not "Event", here, because we actually make use of
`[threading/asyncio].Event` and the two would be confusing.
    )annotationsN)	monotonic)heappopheappush)Any)Callable   )Task)AEventALock)CancelledErrorc                  0    e Zd ZddZdZddZd	dZddZy)
AsyncSchedulerc                N    g | _         t               | _        t               | _        y )N)_queuer   _lockr   _event)selfs    V/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/psycopg_pool/sched_async.py__init__zAsyncScheduler.__init__'   s    "$W
h    g     @c                \   K   t               |z   }| j                  ||       d{   S 7 w)zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        N)r   enterabs)r   delayactiontimes       r   enterzAsyncScheduler.enter.   s+     
 {U"]]40000s   #,*,c                (  K   t        ||      }| j                  4 d{    t        | j                  |       | j                  d   |u }ddd      d{    r| j                  j                          |S 7 Y7 $# 1 d{  7  sw Y   4xY ww)znEnter a new task in the queue at an absolute time.

        Schedule a `!None` to stop the execution.
        Nr   )r
   r   r   r   r   set)r   r   r   taskfirsts        r   r   zAsyncScheduler.enterabs6   s     
 D&!:: 	+ 	+T[[$'KKNd*E	+ 	+ KKOO	+ 	+ 	+ 	+ 	+sD   BA9B(A=BA;#B;B=BBBBc                  K   | j                   }	 | j                  4 d{    t               }|r|d   ndx}r-|j                  |k  rt	        |       n|j                  |z
  }d}n| j
                  }| j                  j                          ddd      d{    r'|j                  sy	 |j                          d{    n#| j                  j                         d{    7 7 S# 1 d{  7  sw Y   cxY w7 B# t        $ r@}t        j                  d|j                  |j                  j                  |       Y d}~`d}~ww xY w7 lw)zExecute the events scheduled.Nr   z$scheduled task run %s failed: %s: %s)r   r   r   r   r   EMPTY_QUEUE_TIMEOUTr   clearr   CLIENT_EXCEPTIONSloggerwarning	__class____name__wait_timeout)r   qnowr    r   es         r   runzAsyncScheduler.runE   s*    KKzz 
$ 
$k$%AaD4040yyC'
 $		C# 44E!!#
$ 
$ {{++-'' kk..u5557 
$ 
$ 
$ 
$ 
$  (( NN>,,	  6s   EC*EA)C.EC,E-D  DD E$E%E,E.D 4C75D <ED 	E6E	E	EEN)returnNone)r   floatr   Callable[[], Any] | Noner/   r
   )r   r1   r   r2   r/   r
   )r)   
__module____qualname__r   r#   r   r   r.    r   r   r   r   &   s    
  16r   r   )__doc__
__future__r   loggingr   r   heapqr   r   typingr   collections.abcr   _taskr
   _acompatr   r   	getLoggerr)   r&   asyncior   	Exceptionr%   r   r5   r   r   <module>rA      sS   
 #   #  $  #			8	$& #N3
=6 =6r   