o
    fv                     @   s   d 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mZmZmZ dd	lmZ d
ZG dd deZdddZG dd dejZG dd deZG dd deZdS )zModel managers.    Nwraps)count)maybe_timedelta)settings)connectionsmodelsroutertransaction   )nowz
Polling results with transaction isolation level 'repeatable-read'
within the same transaction may give outdated results.

Be sure to commit the transaction for each poll iteration.
c                   @   s   e Zd ZdZdS )TxIsolationWarningzAWarning emitted if the transaction isolation level is suboptimal.N)__name__
__module____qualname____doc__ r   r   Q/var/www/html/venv/lib/python3.10/site-packages/django_celery_results/managers.pyr      s    r   c                    s    fdd}|S )zDecorate a function to retry database operations.

    For functions doing database operations, adding
    retrying if the operation fails.

    Keyword Arguments:
        max_retries (int): Maximum number of retries.  Default one retry.

    c                    s   t   fdd}|S )Nc               	      sN   | d}tdD ]}z
 | i |W   S  ty$   ||kr" Y q
w d S )Nexception_retry_countr   )popr   	Exception)argskwargs_max_retriesretries)funmax_retriesr   r   _inner%   s   z1transaction_retry.<locals>._outer.<locals>._innerr   )r   r   r   )r   r   _outer#   s   z!transaction_retry.<locals>._outerr   )r   r   r   r   r   transaction_retry   s   
r    c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )ResultManagerz#Generic manager for celery results.c                 C   s\   d|    v r(|   }|dr*| d }|dkr,ttt	
  d S d S d S d S )NmysqlzPSHOW VARIABLES WHERE variable_name IN ('tx_isolation', 'transaction_isolation');r   zREPEATABLE-READ)current_enginelowerconnection_for_readcursorexecutefetchonewarningswarnr   W_ISOLATION_REPstrip)selfr&   	isolationr   r   r   warn_if_repeatable_read;   s   
z%ResultManager.warn_if_repeatable_readc                 C   s   t t| j S N)r   r	   db_for_writemodelr-   r   r   r   connection_for_writeG   s   z"ResultManager.connection_for_writec                 C   s
   t | j S r0   )r   dbr3   r   r   r   r%   J   s   
z!ResultManager.connection_for_readc                 C   s,   z	t j| j d W S  ty   t j Y S w )NENGINE)r   	DATABASESr5   AttributeErrorDATABASE_ENGINEr3   r   r   r   r#   M   s
   
zResultManager.current_enginec                 C   s   | j t t| dS )zGet all expired results.)date_done__lt)filterr   r   r-   expiresr   r   r   get_all_expiredS   s   zResultManager.get_all_expiredc                 C   sB   t j| jd | |  W d   dS 1 sw   Y  dS )zDelete all expired results.usingN)r
   atomicr5   r>   deleter<   r   r   r   delete_expiredW   s   "zResultManager.delete_expiredN)
r   r   r   r   r/   r4   r%   r#   r>   rC   r   r   r   r   r!   8   s    r!   c                   @   s8   e Zd ZdZdZdd Zedd				d	ddZdS )
TaskResultManagerz0Manager for :class:`~.models.TaskResult` models.Nc                 C   J   z| j |dW S  | jjy$   | j|kr|   || _| j|d Y S w )a[  Get result for task by ``task_id``.

        Keyword Arguments:
            exception_retry_count (int): How many times to retry by
                transaction rollback on exception.  This could
                happen in a race condition if another worker is trying to
                create the same task.  The default is to retry once.

        )task_idgetr2   DoesNotExist_last_idr/   )r-   rF   r   r   r   get_taskb      

zTaskResultManager.get_task   r   c                 C   sf   ||||||||	|
||d}|  |j||d\}}|s1| D ]
\}}t||| q |j|d |S )aF  Store the result and status of a task.

        Arguments:
            content_type (str): Mime-type of result and meta content.
            content_encoding (str): Type of encoding (e.g. binary/utf-8).
            task_id (str): Id of task.
            periodic_task_name (str): Celery Periodic task name.
            task_name (str): Celery task name.
            task_args (str): Task arguments.
            task_kwargs (str): Task kwargs.
            result (str): The serialized return value of the task,
                or an exception instance raised by the task.
            status (str): Task status.  See :mod:`celery.states` for a list of
                possible status values.
            worker (str): Worker that executes the task.
            using (str): Django database connection to use.
            traceback (str): The traceback string taken at the point of
                exception (only passed if the task failed).
            meta (str): Serialized result meta data (this contains e.g.
                children).

        Keyword Arguments:
            exception_retry_count (int): How many times to retry by
                transaction rollback on exception.  This could
                happen in a race condition if another worker is trying to
                create the same task.  The default is to retry twice.

        )statusresult	tracebackmetacontent_encodingcontent_typeperiodic_task_name	task_name	task_argstask_kwargsworker)rF   defaultsr?   )r@   get_or_createitemssetattrsave)r-   rS   rR   rF   rO   rN   rP   rQ   rT   rU   rV   rW   rX   r@   fieldsobjcreatedkvr   r   r   store_resultt   s(   $
zTaskResultManager.store_result)NNNNNNNN)r   r   r   r   rJ   rK   r    rc   r   r   r   r   rD   ]   s    rD   c                   @   s2   e Zd ZdZdZdd Zedd	d	ddZdS )
GroupResultManagerz1Manager for :class:`~.models.GroupResult` models.Nc                 C   rE   )a]  Get result for group by ``group_id``.

        Keyword Arguments:
            exception_retry_count (int): How many times to retry by
                transaction rollback on exception.  This could
                happen in a race condition if another worker is trying to
                create the same task.  The default is to retry once.

        )group_idrG   )r-   re   r   r   r   	get_group   rL   zGroupResultManager.get_grouprM   r   c                 C   sb   |||d}|s| j }| |j||d\}}|s/| D ]
\}	}
t||	|
 q|j| j d |S )N)rO   rR   rS   )re   rY   r?   )r5   r@   rZ   r[   r\   r]   )r-   rS   rR   re   rO   r@   r^   r_   r`   ra   rb   r   r   r   store_group_result   s   
z%GroupResultManager.store_group_resultr0   )r   r   r   r   rJ   rf   r    rg   r   r   r   r   rd      s    rd   )r   )r   r)   	functoolsr   	itertoolsr   celery.utils.timer   django.confr   	django.dbr   r   r	   r
   utilsr   r+   UserWarningr   r    Managerr!   rD   rd   r   r   r   r   <module>   s    
%P