o
    wvXjü  ã                   @   sÜ   d Z ddlmZmZ ddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddl	mZ ddlmZ dd	lmZ d
dlmZ zddlmZ W n eyS   ddlmZ Y nw dZG dd„ deƒZddd„ZG dd„ dejƒZdS )zModel managers.é    )Úabsolute_importÚunicode_literalsN©Úwraps)Úcount)ÚconnectionsÚrouterÚtransaction)Úmodels)Úsettings)Úitemsé   )Únow)Úmaybe_timedeltaz·
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   ú[/var/www/html/myproject/venv/lib/python3.10/site-packages/django_celery_results/managers.pyr      s    r   c                    s   ‡ fdd„}|S )a	  Decorate 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                   @   sf   e Zd ZdZdZdd„ Zedd�			ddd„ƒZd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚTaskResultManagerz5Manager for :class:`celery.models.TaskResult` models.Nc                 C   sJ   z| j |d�W S  | jjy$   | j|kr|  ¡  || _| j|d� Y S w )au  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)ÚgetÚmodelÚDoesNotExistÚ_last_idÚwarn_if_repeatable_read)Úselfr%   r   r   r   Úget_taskG   s   
üzTaskResultManager.get_taské   r!   c                 C   sd   ||||||||	|
|dœ
}|   |¡j||d�\}}|s0t|ƒD ]
\}}t|||ƒ q|j|d� |S )a1  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.
            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.

        Keyword Arguments:
        -----------------
            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).
            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Ú	task_nameÚ	task_argsÚtask_kwargsÚworker)r%   Údefaults)Úusing)r9   Úget_or_creater   ÚsetattrÚsave)r+   r3   r2   r%   r/   r.   r0   r1   r4   r5   r6   r7   r9   ÚfieldsÚobjÚcreatedÚkÚvr   r   r   Ústore_resultZ   s&   $ö
ÿzTaskResultManager.store_resultc                 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)r+   rG   Ú	isolationr   r   r   r*   ‘   s   
÷z)TaskResultManager.warn_if_repeatable_readc                 C   s   t t | j¡ S ©N)r   r   Údb_for_writer'   ©r+   r   r   r   Úconnection_for_write�   s   z&TaskResultManager.connection_for_writec                 C   s
   t | j S rO   )r   ÚdbrQ   r   r   r   rF       s   
z%TaskResultManager.connection_for_readc                 C   s,   z	t j| j d W S  ty   t j Y S w )NÚENGINE)r   Ú	DATABASESrS   ÚAttributeErrorÚDATABASE_ENGINErQ   r   r   r   rD   £   s
   
ÿz TaskResultManager.current_enginec                 C   s   | j tƒ t|ƒ d�S )zGet all expired task results.)Údate_done__lt)Úfilterr   r   ©r+   Úexpiresr   r   r   Úget_all_expired©   s   z!TaskResultManager.get_all_expiredc                 C   s<   t  ¡ � |  |¡ ¡  W d  ƒ dS 1 sw   Y  dS )zDelete all expired results.N)r	   Úatomicr\   ÚdeleterZ   r   r   r   Údelete_expired­   s   
"ÿz TaskResultManager.delete_expired)NNNNNNN)r   r   r   r   r)   r,   r#   rB   r*   rR   rF   rD   r\   r_   r   r   r   r   r$   B   s    ü6r$   )r   )r   Ú
__future__r   r   rJ   Ú	functoolsr   Ú	itertoolsr   Ú	django.dbr   r   r	   r
   Údjango.confr   Úcelery.fiver   Úutilsr   Úcelery.utils.timer   ÚImportErrorÚcelery.utils.timeutilsrL   ÚUserWarningr   r#   ÚManagerr$   r   r   r   r   Ú<module>   s&    ÿ
 