o
    Df
                     @  s*  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddl
m
Z dd	lmZ dd
lmZ ddlmZ ddlmZ ejdkrRddlmZ nddlmZ er`ddlmZ dZeeZdZe dedZ!e"edZ#eddZ$edefddZ%e	G dd dZ&G dd dZ'dS )z"Timer scheduling Python callbacks.    )annotationsN)
namedtuple)datetime)total_ordering)	monotonic)time)TYPE_CHECKING)proxy)wraps)
get_logger)   	   )ZoneInfo)TracebackType)EntryTimerto_timestamp   UTCpypy_version_info	scheduled)etapriorityentryc                 C  sF   t | tr!| jdu r| j|d} t |  }t| t  | dS | S )z^Convert datetime to timestamp.

    If d' is already a timestamp, then that will be used.
    N)tzinfor   )
isinstancer   r   replace_timemaxEPOCHtotal_seconds)ddefault_timezoner   diff r$   Q/home/ubuntu/webapp/venv/lib/python3.10/site-packages/kombu/asynchronous/timer.pyr   &   s   

r   c                   @  s\   e Zd ZdZes
dZdddZdd Zdd	 Zd
d Z	dd Z
edd Zejdd ZdS )r   zSchedule Entry.)funargskwargstrefcanceled	_last_run__weakref__Nc                 C  s4   || _ |pg | _|pi | _t| | _d | _d| _d S )NF)r&   r'   r(   weakrefproxyr)   r+   r*   )selfr&   r'   r(   r$   r$   r%   __init__=   s   



zEntry.__init__c                 C  s   | j | ji | jS N)r&   r'   r(   r.   r$   r$   r%   __call__E   s   zEntry.__call__c                 C  s$   zd| j _W d S  ty   Y d S w NT)r)   r*   ReferenceErrorr1   r$   r$   r%   cancelH   s
   zEntry.cancelc                 C  s   d | jj| j| jS )Nz<TimerEntry: {}(*{!r}, **{!r}))formatr&   __name__r'   r(   r1   r$   r$   r%   __repr__N   s   zEntry.__repr__c                 C  s   t | t |k S r0   )id)r.   otherr$   r$   r%   __lt__S   s   zEntry.__lt__c                 C  s   | j S r0   r*   r1   r$   r$   r%   	cancelledV   s   zEntry.cancelledc                 C  s
   || _ d S r0   r<   )r.   valuer$   r$   r%   r=   Z   s   
NN)r7   
__module____qualname____doc__IS_PYPY	__slots__r/   r2   r5   r8   r;   propertyr=   setterr$   r$   r$   r%   r   3   s    

r   c                   @  s   e Zd ZdZeZdZd3ddZdd Zd4ddZd5ddZ	d5ddZ
d5ddZddefddZdefddZejfddZdd  Zd!d" Zd#d$ Zeeejejfd%d&Zd'd( Zd)d* Zd+d, Zd-d. Zeejfd/d0Zed1d2 ZdS )6r   zAsync timer implementation.Nc                 K  s$   t |pt| _|p| j| _g | _d S r0   )floatDEFAULT_MAX_INTERVALmax_intervalon_error_queue)r.   rI   rJ   r(   r$   r$   r%   r/   f   s   
zTimer.__init__c                 C     | S r0   r$   r1   r$   r$   r%   	__enter__k      zTimer.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | NonereturnNonec                 C  s   |    d S r0   )stop)r.   rO   rQ   rS   r$   r$   r%   __exit__n   s   zTimer.__exit__r$   r   c                 C  s$   |si n|}|  | |||||S r0   )enter_atr   )r.   r   r&   r'   r(   r   r$   r$   r%   call_atv      zTimer.call_atc                 C  s$   |si n|}|  || ||||S r0   )enter_afterr   )r.   secsr&   r'   r(   r   r$   r$   r%   
call_afterz   r[   zTimer.call_afterc                   sP   |si n|}  ||t  fdd}|_d _S )Nc                    s   j t }}|r|j  n}zE|r:|kr:|_  | i |W js9j }|r/||  n}| S S W jsTj }|rI||  n}| d S d S jsnj }|rd||  n}| w w r0   )r+   r   r*   r\   )r'   r(   lastnowlsincenextr&   r   r]   r.   r)   r$   r%   _reschedules   s*    
z+Timer.call_repeatedly.<locals>._reschedules)r   r
   r&   r+   r\   )r.   r]   r&   r'   r(   r   rd   r$   rc   r%   call_repeatedly~   s   zTimer.call_repeatedlyc              
   C  sf   |du r| }t |tr,zt|}W n ty+ } z| |s  W Y d}~dS d}~ww | |||S )zEnter function into the scheduler.

        Arguments:
        ---------
            entry (~kombu.asynchronous.timer.Entry): Item to enter.
            eta (datetime.datetime): Scheduled time.
            priority (int): Unused.
        N)r   r   r   	Exceptionhandle_error_enter)r.   r   r   r   r   excr$   r$   r%   rY      s   	

zTimer.enter_atc                 C  s   |  || t| |S r0   )rY   rG   )r.   r]   r   r   r   r$   r$   r%   r\      s   zTimer.enter_afterc                 C  s   || j t||| |S r0   )rK   r   )r.   r   r   r   pushr$   r$   r%   rh      s   zTimer._enterc              
   C  s\   z|  W d S  t y- } z| |s"tjd|dd W Y d }~d S W Y d }~d S d }~ww )NzError in timer: %rT)exc_info)rf   rg   loggererror)r.   r   ri   r$   r$   r%   apply_entry   s   
zTimer.apply_entryc                 C  s   | j r
|  | dS d S r3   )rJ   )r.   rk   r$   r$   r%   rg      s   
zTimer.handle_errorc                 C  s   d S r0   r$   r1   r$   r$   r%   rW      rN   z
Timer.stopc                 c  s    | j }| j}	 |r@|d }| |d }}	||	k r%||	| |dfV  n||}
|
|u r:|d }|js9d|fV  q|||
 ndV  q)zIterate over schedule.

        This iterator yields a tuple of ``(wait_seconds, entry)``,
        where if entry is :const:`None` the caller should wait
        for ``wait_seconds`` until it polls the schedule again.
           r   Nr   r?   )rI   rK   r*   )r.   minnowfunpoprj   rI   queueeventAr`   r   eventBr   r$   r$   r%   __iter__   s$   
zTimer.__iter__c                 C  s   g | j d d < d S r0   )rK   r1   r$   r$   r%   clear   s   zTimer.clearc                 C  s   |   d S r0   )r5   )r.   r)   r$   r$   r%   r5      s   zTimer.cancelc                 C  s
   t | jS r0   )lenrK   r1   r$   r$   r%   __len__   s   
zTimer.__len__c                 C  s   dS r3   r$   r1   r$   r$   r%   __nonzero__   rN   zTimer.__nonzero__c                   s&   t | j} fdd|gt| D S )z%Snapshot of underlying datastructure.c                   s   g | ]} |qS r$   r$   ).0v_popr$   r%   
<listcomp>   s    zTimer.queue.<locals>.<listcomp>)listrK   rx   )r.   r~   eventsr$   r}   r%   rs      s   
zTimer.queuec                 C  rL   r0   r$   r1   r$   r$   r%   schedule   s   zTimer.scheduler?   )rO   rP   rQ   rR   rS   rT   rU   rV   )r$   Nr   )r7   r@   rA   rB   r   rJ   r/   rM   rX   rZ   r^   re   r   rY   r\   heapqheappushrh   rn   rg   rW   rp   heappoprv   rw   r5   ry   rz   rE   rs   r   r$   r$   r$   r%   r   _   s6    





r   )(rB   
__future__r   r   syscollectionsr   r   	functoolsr   r   r   r   typingr   weakrefr	   r-   
vine.utilsr
   	kombu.logr   version_infozoneinfor   backports.zoneinfotypesr   __all__r7   rl   rH   fromtimestampr   hasattrrC   r   r   r   r   r$   r$   r$   r%   <module>   s8    


+