
    jgv                         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e      ZddZ G d dej*                        Z G d de      Z G d de      Zy)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                       e Zd ZdZy)TxIsolationWarningzAWarning emitted if the transaction isolation level is suboptimal.N)__name__
__module____qualname____doc__     Y/var/www/html/Stanley/venv/lib/python3.12/site-packages/django_celery_results/managers.pyr   r      s    Kr   r   c                       f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                 2     t                fd       }|S )Nc                      |j                  d      }t        d      D ]  }	  | i |c S  y # t        $ r	 ||k\  r Y !w xY w)Nexception_retry_countr   )popr   	Exception)argskwargs_max_retriesretriesfunmax_retriess       r   _innerz1transaction_retry.<locals>._outer.<locals>._inner%   sZ    !::&={KL 8	/// $ !  ,. /s   /A Ar   )r!   r#   r"   s   ` r   _outerz!transaction_retry.<locals>._outer#   s     	s	 
	 r   r   )r"   r$   s   ` r   transaction_retryr%      s    $ Mr   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ResultManagerz#Generic manager for celery results.c                 8   d| j                         j                         v rz| j                         j                         }|j	                  d      rJ|j                         d   }|dk(  r1t        j                  t        t        j                                      y y y y )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-   	isolations      r   warn_if_repeatable_readz%ResultManager.warn_if_repeatable_read;   s    d))+1133--/668F ~~ K L"OO-a0	 11MM"4_5J5J5L"MN 2L 4r   c                 N    t         t        j                  | j                           S N)r   r
   db_for_writemodelr4   s    r   connection_for_writez"ResultManager.connection_for_writeG   s    6..tzz:;;r   c                 (    t         | j                     S r8   )r   dbr;   s    r   r,   z!ResultManager.connection_for_readJ   s    477##r   c                     	 t         j                  | j                     d   S # t        $ r t         j                  cY S w xY w)NENGINE)r   	DATABASESr>   AttributeErrorDATABASE_ENGINEr;   s    r   r*   zResultManager.current_engineM   s<    	,%%dgg.x88 	,+++	,s   " >>c                 N    | j                  t               t        |      z
        S )zGet all expired results.)date_done__lt)filterr   r   r4   expiress     r   get_all_expiredzResultManager.get_all_expiredS   s    {{1I)I{JJr   c                     t        j                  | j                        5  | j                  |      j	                          ddd       y# 1 sw Y   yxY w)zDelete all expired results.usingN)r   atomicr>   rI   deleterG   s     r   delete_expiredzResultManager.delete_expiredW   s7    dgg.  )002 /..s    A

AN)
r   r   r   r   r6   r<   r,   r*   rI   rO   r   r   r   r'   r'   8   s%    -
O<$,K3r   r'   c                   B    e Zd ZdZdZd Z ed      	 	 	 	 dd       Zy)TaskResultManagerz0Manager for :class:`~.models.TaskResult` models.Nc                     	 | j                  |      S # | j                  j                  $ r; | j                  |k(  r| j	                          || _        | j                  |      cY S w xY 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getr:   DoesNotExist_last_idr6   )r4   rS   s     r   get_taskzTaskResultManager.get_taskb   sa    	/88G8,,zz&& 	/}}',,.#DM::g:..		/    AA('A(   r"   c                     ||||||||	|
||d}| j                  |      j                  ||      \  }}|s7|j                         D ]  \  }}t        |||        |j	                  |       |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)rS   defaultsrK   )rL   get_or_createitemssetattrsave)r4   rb   ra   rS   r^   r]   r_   r`   rc   rd   re   rf   rg   rL   fieldsobjcreatedkvs                      r   store_resultzTaskResultManager.store_resultt   s    H " 0("4""&
 zz%(66w@F 7 HW1Q" 'HH5H!
r   )NNNNNNNN)r   r   r   r   rW   rX   r%   rr   r   r   r   rQ   rQ   ]   s6    :H/$ 1% +/(,AE(,5 &5r   rQ   c                   <    e Zd ZdZdZd Z ed      	 dd       Zy)GroupResultManagerz1Manager for :class:`~.models.GroupResult` models.Nc                     	 | j                  |      S # | j                  j                  $ r; | j                  |k(  r| j	                          || _        | j                  |      cY S w xY w)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_idrT   )r4   rv   s     r   	get_groupzGroupResultManager.get_group   sa    	188X8..zz&& 	1}}(,,.$DM::x:00		1rY   rZ   r[   c                     |||d}|s| j                   }| j                  |      j                  ||      \  }}|sA|j                         D ]  \  }	}
t	        ||	|
        |j                  | j                          |S )N)r^   ra   rb   )rv   rh   rK   )r>   rL   ri   rj   rk   rl   )r4   rb   ra   rv   r^   rL   rm   rn   ro   rp   rq   s              r   store_group_resultz%GroupResultManager.store_group_result   s      0(
 GGEzz%(66@F 7 HW1Q" 'HH477H#
r   r8   )r   r   r   r   rW   rw   r%   ry   r   r   r   rt   rt      s+    ;H1$ 1%37 &r   rt   )r   )r   r0   	functoolsr   	itertoolsr   celery.utils.timer   django.confr   	django.dbr   r	   r
   r   utilsr   r2   UserWarningr   r%   Managerr'   rQ   rt   r   r   r   <module>r      sf        -   > > L L>"3FNN "3JM M`) )r   