
    KiW                        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m	Z	m
Z
mZ erddlmZ ddlmZ d	d
lmZmZ  G d ded         Zy)zTimeout dependency for tasks.    )annotationsN)	timedelta)TYPE_CHECKINGAny	AwaitableCallable   )	Execution)cancel_task   )Runtimecurrent_docketc                  t    e Zd ZU dZded<   ded<   ddZddZddZdd	Zdd
Z	dddZ
	 	 	 	 	 	 	 	 	 	 ddZy)Timeoutac  Configures a timeout for a task.  You can specify the base timeout, and the
    task will be cancelled if it exceeds this duration.  The timeout may be extended
    within the context of a single running task.

    Example:

    ```python
    @task
    async def my_task(timeout: Timeout = Timeout(timedelta(seconds=10))) -> None:
        ...
    ```
    r   basefloat	_deadlinec                    || _         y)zD
        Args:
            base: The base timeout duration.
        Nr   )selfr   s     h/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/docket/dependencies/_timeout.py__init__zTimeout.__init__"   s    
 	    c                6   K   t        | j                        S w)Nr   )r   r   r   s    r   
__aenter__zTimeout.__aenter__)   s     DII&&s   c                l    t        j                         | j                  j                         z   | _        y N)time	monotonicr   total_secondsr   r   s    r   startzTimeout.start,   s"    )DII,C,C,EEr   c                D    t        j                         | j                  k\  S r   )r   r    r   r   s    r   expiredzTimeout.expired/   s    ~~4>>11r   c                X    t        | j                  t        j                         z
        S )z1Get the remaining time until the timeout expires.)seconds)r   r   r   r    r   s    r   	remainingzTimeout.remaining2   s    $..2B!BCCr   Nc                f    || j                   }| xj                  |j                         z  c_        y)zExtend the timeout by a given duration.  If no duration is provided, the
        base timeout will be used.

        Args:
            by: The duration to extend the timeout by.
        N)r   r   r!   )r   bys     r   extendzTimeout.extend6   s)     :B"**,,r   c                `  K   | j                          t        j                         }t        j                   ||i ||j
                   d|j                         }d}	 |j                         s| j                         rd}n	 t        j                  t        j                  |      | j                         j                                d{   |j                         sWd|j                   d| j                  j                          d	}t        ||       d{    |rt        j                  |      S S |j                         sWd|j                   d| j                  j                          d	}t        ||       d{    |rt        j                  |      yy7 7 # t        j                  $ r Y [w xY w7 ;# |j                         sXd|j                   d| j                  j                          d	}t        ||       d{  7   |rt        j                  |      w w xY ww)
z.Execute the function with timeout enforcement.z - task:)nameFT)timeoutNzDocket task z exceeded timeout of s)r"   r   getasynciocreate_taskr,   keydoner$   wait_forshieldr'   r!   r   r   TimeoutError)	r   	executionfunctionargskwargsdockettask	timed_outtimeout_reasons	            r   runzTimeout.runA   s     	

##%""d%f%KK=8

 		?iik<<> $I!(!1!1t,dnn6F6T6T6V"  99;"9==/ 2""&))"9"9";!<A?  "$777!..~>>  499;"9==/ 2""&))"9"9";!<A?  "$777!..~>>   8 ++  8 99;"9==/ 2""&))"9"9";!<A?  "$777!..~>>  s   AH. #G A	F% F!F% A
H.F#A'H.F?H.!F% #H.%F<8G ;F<<G ?H.AH+HH++H.)r   r   returnNone)r@   r   )r@   rA   )r@   bool)r@   r   r   )r)   ztimedelta | Noner@   rA   )
r7   r
   r8   zCallable[..., Awaitable[Any]]r9   ztuple[Any, ...]r:   zdict[str, Any]r@   r   )__name__
__module____qualname____doc____annotations__r   r   r"   r$   r'   r*   r?    r   r   r   r      si     O'F2D	-%?%? 0%? 	%?
 %? 
%?r   r   )rF   
__future__r   r0   r   datetimer   typingr   r   r   r   r7   r
   _cancellationr   _baser   r   r   rH   r   r   <module>rN      s9    # "    : :% ' *U?gi  U?r   