
    Kia                        d Z ddlmZ ddlZddlmZmZmZ ddlmZm	Z	 ddl
mZmZmZmZmZ erdd	lmZ dd
lmZ  ej(                  d      Z G d de      Z G d ded         Z G d de      Zy)zRetry strategies for tasks.    )annotationsN)datetime	timedeltatimezone)TYPE_CHECKINGNoReturn   )FailureHandlerTaskOutcomecurrent_executioncurrent_workerformat_duration   )	Execution)TASKS_RETRIEDzdocket.dependenciesc                      e Zd ZdZy)ForcedRetryz7Raised when a task requests a retry via `after` or `at`N)__name__
__module____qualname____doc__     f/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/docket/dependencies/_retry.pyr   r      s    Ar   r   c                  |    e Zd ZU dZded<   ded<   ded<   d ed	      f	 	 	 	 	 dd
ZddZddZddZ	ddZ
ddZy)Retrya  Configures linear retries for a task.  You can specify the total number of
    attempts (or `None` to retry indefinitely), and the delay between attempts.

    Example:

    ```python
    @task
    async def my_task(retry: Retry = Retry(attempts=3)) -> None:
        ...
    ```
    
int | Noneattemptsr   delayintattemptr	   r   c                .    || _         || _        d| _        y)z
        Args:
            attempts: The total number of attempts to make.  If `None`, the task will
                be retried indefinitely.
            delay: The delay between attempts.
        r	   N)r   r   r!   )selfr   r   s      r   __init__zRetry.__init__.   s     !
r   c                   K   t        j                         }t        | j                  | j                        }|j
                  |_        |S w)Nr   r   )r   getr   r   r   r!   )r#   	executionretrys      r   
__aenter__zRetry.__aenter__;   s:     %))+	t}}DJJ?!))s   A	Ac                $    || _         t               )z&Request a retry after the given delay.)r   r   r#   r   s     r   afterzRetry.afterA   s    
mr   c                    t        j                  t        j                        }||z
  }|j	                         dk\  r|n
t        d      }| j                  |       y)z"Request a retry at the given time.r   N)r   nowr   utctotal_secondsr   r-   )r#   whenr/   diffs       r   atzRetry.atF   sD    ll8<<(cz))+q0til

4r   c                &    | j                  |       y)z Deprecated: use after() instead.N)r-   r,   s     r   in_z	Retry.in_M   s    

5r   c                   K   | j                   |j                  | j                   k\  ryt        j                  t        j
                        | j                  z   |_        |xj                  dz  c_        |j                  d       d{    t        j                         }t        j                  di |j                         |j                                |j                  rRt         j#                  dt%        |j&                  j)                               |j+                         |j                         t         j-                  dt%        |j&                  j)                               |j+                                y7 w)	z8Handle failure by scheduling a retry if attempts remain.NFr	   T)replaceu   ↩ [%s] %s)exc_infou   ↫ [%s] %s)r   r!   r   r/   r   r0   r   r2   scheduler   r'   r   addlabelsgeneral_labels	exceptionloggererrorr   durationr1   	call_reprinfo)r#   r(   outcomeworkers       r   handle_failurezRetry.handle_failureQ   s(    ==$):):dmm)K!hll3djj@	Q   ...##%!NN93K3K3MNOLL 0 0 > > @A##% **	   	G,,::<=!	
 ' 	/s   BE>E<	C4E>N)r   r   r   r   returnNone)rG   r   )r   r   rG   r   )r2   r   rG   r   )r(   r   rD   r   rG   bool)r   r   r   r   __annotations__r   r$   r*   r-   r4   r6   rF   r   r   r   r   r      s[    
 L &'9Q<"/8	
r   r   c                  ^     e Zd ZdZd ed       ed      f	 	 	 	 	 	 	 d fdZddZ xZS )	ExponentialRetryaO  Configures exponential retries for a task.  You can specify the total number
    of attempts (or `None` to retry indefinitely), and the minimum and maximum delays
    between attempts.

    Example:

    ```python
    @task
    async def my_task(retry: ExponentialRetry = ExponentialRetry(attempts=3)) -> None:
        ...
    ```
    r	   )seconds@   c                6    t         |   ||       || _        y)a  
        Args:
            attempts: The total number of attempts to make.  If `None`, the task will
                be retried indefinitely.
            minimum_delay: The minimum delay between attempts.
            maximum_delay: The maximum delay between attempts.
        r&   N)superr$   maximum_delay)r#   r   minimum_delayrQ   	__class__s       r   r$   zExponentialRetry.__init__|   s     	(-@*r   c                b  K   t        j                         }t        | j                  | j                  | j
                        }|j                  |_        |j                  dkD  rJd|j                  dz
  z  }| j                  |z  }|| j
                  kD  r| j
                  |_        |S ||_        |S w)N)r   rR   rQ   r	   r   )r   r'   rL   r   r   rQ   r!   )r#   r(   r)   backoff_factorcalculated_delays        r   r*   zExponentialRetry.__aenter__   s     %))+	 ]]**,,

 "))q 9#4#4q#89N#zzN:$"4"44"00  /s   B-B/)r   r   rR   r   rQ   r   rG   rH   )rG   rL   )r   r   r   r   r   r$   r*   __classcell__)rS   s   @r   rL   rL   n   sM      !#,Q#7#,R#8	++ !+ !	+
 
+ r   rL   )r   
__future__r   loggingr   r   r   typingr   r   _baser
   r   r   r   r   r(   r   instrumentationr   	getLoggerr?   	Exceptionr   r   rL   r   r   r   <module>r_      sm    ! "  2 2 *  % +			0	1B) BNN7# Nb1u 1r   