
    i@                         d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZ  ej"                  e      Z G d	 d
      Zy)a  
A minimal scheduler to schedule tasks to run in the future (sync 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.
    N)	monotonic)heappushheappop)AnyCallableListOptional   )Task)LockEventc                   h    e Zd ZddZdZdedeeg ef      de	fdZ
dedeeg ef      de	fd	Zdd
Zy)	SchedulerreturnNc                 N    g | _         t               | _        t               | _        y )N)_queuer   _lockr   _event)selfs    r/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg_pool/sched.py__init__zScheduler.__init__   s    "$V
g    g     @delayactionc                 @    t               |z   }| j                  ||      S )zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        )r   enterabs)r   r   r   times       r   enterzScheduler.enter&   s!    
 {U"}}T6**r   r   c                     t        ||      }| j                  5  t        | j                  |       | j                  d   |u }ddd       r| j                  j                          |S # 1 sw Y   'xY w)znEnter a new task in the queue at an absolute time.

        Schedule a `!None` to stop the execution.
        r   N)r   r   r   r   r   set)r   r   r   taskfirsts        r   r   zScheduler.enterabs.   sd    
 D&!ZZ 	+T[[$'KKNd*E	+ KKOO	+ 	+s   (A''A0c                 @   | j                   }	 | j                  5  t               }|r|d   nd}|r-|j                  |k  rt	        |       n|j                  |z
  }d}n| j
                  }| j                  j                          ddd       r|j                  sy	 |j                          n| j                  j                         # 1 sw Y   FxY w# t        $ r@}t        j                  d|j                  |j                  j                  |       Y d}~Qd}~ww xY w)zExecute the events scheduled.r   Nz$scheduled task run %s failed: %s: %s)r   r   r   r   r   EMPTY_QUEUE_TIMEOUTr   clearr   	Exceptionloggerwarning	__class____name__wait)r   qnowr!   r   es         r   runzScheduler.run=   s    KK $k qtdyyC'
 $		C# 44E!!#$ {{KKM   '9 $ $$ ! NN>,,	 s$   A)CC C	D6DD)r   N)r*   
__module____qualname__r   r$   floatr	   r   r   r   r   r   r/    r   r   r   r      sd    
  +5 +(8BG3D*E +$ +U HXb#g5F,G D (r   r   )__doc__loggingr   r   heapqr   r   typingr   r   r   r	   _taskr   _acompatr   r   	getLoggerr*   r'   r   r3   r   r   <module>r;      s=   
   # 0 0  !			8	$?( ?(r   