o
    wi                      @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlZddlm	Z	 e
eZedd ddd dedf	ddZedd ddd dedf	d	d
Zdd Zdd Zdd Zdd ZdedfddZedd ddd dedf	ddZddedd ddd dedfddZdS )    N)partial   	decoratorFc
              
   C   s   ||}
}|
rHz|  W S  |yC } z,|
d8 }
|r"t ||||
|| |
s% |	d ur-|	| t| t||||}W Y d }~nd }~ww |
sd S d S Nr   )_log_attempttimesleep
_new_delayf
exceptionstriesdelay	max_delaybackoffjittershow_tracebackloggerfail_callback_tries_delaye r   H/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/reretry/api.py__retry_internal   s"   

r   c
              
      s   ||}
}|
rRz|  I d H W S  |yM } z2|
d8 }
|r&t ||||
|| |
s) |	d ur4|	|I d H  t|I d H  t||||}W Y d }~nd }~ww |
sd S d S r   )r   asyncior
   r   r   r   r   r   __retry_internal_async2   s$   
r   c                 C   sf   |r!|rd td ||j}|| |d|| | | | d S | dkr1|d|| | |  d S d S )N z4%s, attempt %s/%s failed - retrying in %s seconds...r   z%%s, attempt %s/%s failed - giving up!)join	tracebackformat_exception__traceback__warning)r   r   r   r   r   r   tb_strr   r   r   r   U   s    
r   c                 C   s:   ||9 }|t |trtj| n|7 }| d urt|| }|S N)
isinstancetuplerandomuniformmin)r   r   r   r   r   r   r   r   i   s
   
r   c                 C   s   t | o
t|  S r&   )r   iscoroutinefunctioninspectisgeneratorfunctionr   r   r   r   	_is_asyncs   s   r0   c                 C   s   t | rtS tS r&   )r0   r   r   r/   r   r   r   _get_internal_functionw   s   r1   c                 C   sL   |r
|d us
J d|r t | rt |s"t | st |r$J dd S d S d S )Nz`show_traceback` needs `logger`zWIf the retried function is async, fail_callback needs to be async as well or vice versa)r0   )r   r   r   r   r   r   r   _check_params{   s   r2   c	           
   
      s$   t  f	dd}	|	S )a  Returns a retry decorator.

    :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
    :param tries: the maximum number of attempts. default: -1 (infinite).
    :param delay: initial delay between attempts (in seconds). default: 0.
    :param max_delay: the maximum value of delay (in seconds). default: None (no limit).
    :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
    :param jitter: extra seconds added to delay between attempts. default: 0.
                   fixed if a number, random if a range tuple (min, max)
    :param show_traceback: if True, the traceback of the exception will be logged.
    :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
                   default: retry.logging_logger. if None, logging is disabled.
    :param fail_callback: fail_callback(e) will be called on failed attempts.
    :returns: a retry decorator.
    c                    s   t | || S r&   )
retry_call)r   fargsfkwargs	r   r   r   r   r   r   r   r   r   r   r   retry_decorator   s   zretry.<locals>.retry_decoratorr   )
r   r   r   r   r   r   r   r   r   r7   r   r6   r   retry   s   r8   c                 C   sV   |pt  }|p	t }t| |	|
| t| }|t| g|R i ||||||||	|
|
S )a  
    Calls a function and re-executes it if it failed.

    :param f: the function to execute.
    :param fargs: the positional arguments of the function to execute.
    :param fkwargs: the named arguments of the function to execute.
    :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
    :param tries: the maximum number of attempts. default: -1 (infinite).
    :param delay: initial delay between attempts (in seconds). default: 0.
    :param max_delay: the maximum value of delay (in seconds). default: None (no limit).
    :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
    :param jitter: extra seconds added to delay between attempts. default: 0.
                   fixed if a number, random if a range tuple (min, max)
    :param show_traceback: if True, the traceback of the exception will be logged.
    :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
                   default: retry.logging_logger. if None, logging is disabled.
    :param fail_callback: fail_callback(e) will be called on failed attempts.
    :returns: the result of the f function.
    )listdictr2   r1   r   )r   r4   r5   r   r   r   r   r   r   r   r   r   argskwargsfuncr   r   r   r3      s    
!
r3   )r   loggingr)   r	   r!   	functoolsr   r-   compatr   	getLogger__name__logging_logger	Exceptionr   r   r   r   r0   r1   r2   r8   r3   r   r   r   r   <module>   sp    

%
#


1