
    Ki1                       U 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mZmZmZmZ ddlmZ erd	d
lmZ d	dlmZ d	dlmZ  edd      Z ed      Zded<    ed      Zded<    ed      Zded<   ee_        ee_        ee_        d!dZe G d d             Z G d de       Z! G d dee         Z" G d dee         Z# G d d ee         Z$y)"z?Base Dependency class and ContextVars for dependency injection.    )annotationsN)
ContextVar)	dataclassfield)	timedelta)TYPE_CHECKINGAny	AwaitableCallableTypeVar)
Dependency   )Docket)	Execution)WorkerTT)	covariantcurrent_docketzContextVar[Docket]current_workerzContextVar[Worker]current_executionzContextVar[Execution]c                *    | dk  r	| dz  ddS | ddS )z!Format a duration for log output.d   i  z6.0fmszs  )secondss    e/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/docket/dependencies/_base.pyformat_durationr   $   s,    }D.&b))$r""    c                  T    e Zd ZU dZded<    ed      Zded<    ed      Zded	<   y)
TaskOutcomez6Captures the outcome of a task execution for handlers.r   durationN)defaultr	   resultzBaseException | None	exception)__name__
__module____qualname____doc____annotations__r   r#   r$   r   r   r   r    r    ,   s*    @%FC%&+D&9I#9r   r    c                  >     e Zd ZdZ	 dddd	 	 	 	 	 	 	 d fdZ xZS )AdmissionBlockeda  Raised when a task cannot start due to admission control.

    This is the base exception for admission control mechanisms like
    concurrency limits, rate limits, or health gates.

    When ``reschedule`` is True (default), the worker re-queues the task
    with a short delay.  When False, the task is quietly acknowledged
    and dropped with an INFO-level log (appropriate for debounce/cooldown
    where re-trying would just hit the same window).

    ``retry_delay`` overrides the default reschedule delay when set.
    TN)
rescheduleretry_delayc               |    || _         || _        || _        || _        t        |   d|j                   d|        y )NzTask z blocked by )	executionreasonr,   r-   super__init__key)selfr/   r0   r,   r-   	__class__s        r   r2   zAdmissionBlocked.__init__C   s@     #$&5|F8DEr   )zadmission control)r/   r   r0   strr,   boolr-   ztimedelta | None)r%   r&   r'   r(   r2   __classcell__)r5   s   @r   r+   r+   5   sQ      *F
  (,FF F
 F &F Fr   r+   c                  N    e Zd ZdZdZej                  	 	 	 	 	 	 	 	 	 	 dd       Zy)RuntimezBase class for dependencies that control task execution.

    Only one Runtime dependency can be active per task (single=True).
    The Worker will call run() to execute the task.
    Tc                   K   yw)a*  Execute the function with this runtime's behavior.

        Args:
            execution: The task execution context
            function: The task function to call
            args: Positional arguments for the function
            kwargs: Keyword arguments including resolved dependencies
        Nr   )r4   r/   functionargskwargss        r   runzRuntime.run[   s       	   N)
r/   r   r<   zCallable[..., Awaitable[Any]]r=   ztuple[Any, ...]r>   zdict[str, Any]returnr	   )r%   r&   r'   r(   singleabcabstractmethodr?   r   r   r   r:   r:   R   sV     F 0 	
  
 r   r:   c                  :    e Zd ZdZdZej                  dd       Zy)FailureHandlera0  Base class for dependencies that control what happens when a task fails.

    Called on exceptions. If handle_failure() returns True, the handler
    took responsibility (e.g., scheduled a retry) and Worker won't mark
    the execution as failed.

    Only one FailureHandler per task (single=True).
    Tc                   K   yw)a0  Handle a task failure.

        Args:
            execution: The task execution context
            outcome: The task outcome containing duration and exception

        Returns:
            True if handled (Worker won't mark as failed)
            False if not handled (Worker proceeds normally)
        Nr   r4   r/   outcomes      r   handle_failurezFailureHandler.handle_failurez         	r@   Nr/   r   rI   r    rA   r7   )r%   r&   r'   r(   rB   rC   rD   rJ   r   r   r   rF   rF   n   &     F r   rF   c                  :    e Zd ZdZdZej                  dd       Zy)CompletionHandleraY  Base class for dependencies that control what happens after task completion.

    Called after execution is truly done (success, or failure with no retry).
    If on_complete() returns True, the handler took responsibility (e.g.,
    scheduled follow-up work) and did its own logging.

    Only one CompletionHandler per task (single=True).
    Tc                   K   yw)a8  Handle task completion.

        Args:
            execution: The task execution context
            outcome: The task outcome containing duration, result, and exception

        Returns:
            True if handled (did own logging/metrics)
            False if not handled (Worker does normal logging)
        Nr   rH   s      r   on_completezCompletionHandler.on_complete   rK   r@   NrL   )r%   r&   r'   r(   rB   rC   rD   rQ   r   r   r   rO   rO      rM   r   rO   )r   floatrA   r6   )%r(   
__future__r   rC   contextvarsr   dataclassesr   r   datetimer   typingr   r	   r
   r   r   uncalled_forr   docketr   r/   r   workerr   r   r   r)   r   r   r   r    	Exceptionr+   r:   rF   rO   r   r   r   <module>r\      s    E " 
 " (  C C 1%C4 %/0@%A" A%/0@%A" A+56I+J ( J )
 "
 "
 # : : :Fy F:jm 8Z] 6
1 r   