
    (phV                     J   S r SSKJr  SSKJrJr  SSKJr  SSKJr  SSK	J
r
  SSKJrJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr  S rS rS\R6                  4S jrS)S jrSSS\R6                  4S jrS*S jrS\R6                  4S jr S)S jr!SSS\R6                  4S jr"S+S jr#S\R6                  4S jr$S,S jr%S\R6                  4S jr&S-S jr'S-S jr(S-S jr)S  r*S\R6                  \RV                  S4S! jr, " S" S#5      r- " S$ S%5      r. " S& S'5      r/S( r0g).zProvides task functionality.    )Value)sleeptime)IntegrityError)timezone)
get_broker)Conflogger)uuid)ScheduleTask)Queue)pre_enqueue)SignedPackagec                 @   UR                  5       nSnUR                  S0 5      n[        5       nUS   UR                  SS5      =(       d    UR                  SS5      =(       d    US   U US.nU H1  nU(       a  X;   a  XX   Xx'   M  X;   d  M  UR                  U5      Xx'   M3     UR                  SS5      =(       d    [        UR	                  S	5      5      n	S
U;  a(  [
        R                  (       a  [
        R                  US
'   SU;  a(  [
        R                  (       a  [
        R                  US'   SU;  a(  [
        R                  (       a  [
        R                  US'   X7S'   [        R                  " 5       US'   [        R                  " SUS9  [        R                  " U5      n
UR	                  SS5      (       a  [        U
5      $ U	R!                  U
5      n["        R$                  " SU	R&                   SU 35        ["        R(                  " SU 35        US   $ )zQueue a task for the cluster.)hookgroupsavesynccachedack_failure
iter_countiter_cachedchainbrokerclustertimeout	q_options   	task_nameNr   )idnamefuncargsr   r   r   r   r   kwargsstarteddjango_q)sendertaskFz
Enqueued [z] zPushed r!   )copypopr   r   getr	   CACHEDSYNCACK_FAILURESr   nowr   sendr   dumps_syncenqueuer
   infolist_keydebug)r#   r$   r%   keywordsopt_keysr   tagr)   keyr   pack
enqueue_ids               A/var/www/html/venv/lib/python3.13/site-packages/django_q/tasks.py
async_taskr?      s   {{}HH ["-I
&C !f[$/ ==d+q6D )!DI_ S)DI	  XXh%HDHHY4G)HFtXTdiiyyVD T%6%6"//]NllnDOJT2t$DxxT{%J
KK*V__-R
|<=
LL73%!:    c                 x   UR                  SS5      nUR                  SS5      nUR                  S[        R                  5      nUR                  SS5      nUR                  SS5      nUR                  S[        R                  " 5       5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nU(       a;  [        R
                  R                  US9R                  5       (       a  [        S5      e[        UU UUUUUUUU	U
US9nUR                  5         UR                  5         U$ )aQ  
Create a schedule.

:param func: function to schedule.
:param args: function arguments.
:param name: optional name for the schedule.
:param hook: optional result hook function.
:type schedule_type: Schedule.TYPE
:param repeats: how many times to repeat. 0=never, -1=always.
:param next_run: Next scheduled run.
:type next_run: datetime.datetime
:param cluster: optional cluster name.
:param cron: optional cron expression
:param intended_date_kwarg: optional identifier to pass intended schedule date.
:param kwargs: function keyword arguments.
:return: the schedule object.
:rtype: Schedule
r"   Nr   schedule_typeminutesrepeatsnext_runcronr   intended_date_kwarg)r"   z-A schedule with the same name already exists.)r"   r#   r   r$   r%   rB   rC   rD   rF   rG   r   rH   )r+   r   ONCEr   r0   objectsfilterexistsr   
full_cleanr   )r#   r$   r%   r"   r   rB   rC   rD   rF   rG   r   rH   ss                r>   schedulerO   Q   s   & ::fd#D::fd#DJJ>MjjD)GjjB'Gzz*hlln5H::fd#DjjD)G **%:DA   ''T'299;;LMM 	#/	A LLNFFHHr@   c                     U(       a  [        X5      $ [        5       n [        R                  " U 5      nUb  U$ [        5       U-
  S-  Us=:  a  S:  a   g  [	        S5        MF  )a  
Return the result of the named task.

:type task_id: str or uuid
:param task_id: the task name or uuid
:type wait: int
:param wait: number of milliseconds to wait for a result
:param bool cached: run this against the cache backend
:return: the result object of this task
:rtype: object
N  r   {Gz?)result_cachedr   r   
get_resultr   )task_idwaitr   startrs        r>   resultrY      s`     W++FE
OOG$=HFUNd"d/a/ 0d r@   Nc                 $   U(       d
  [        5       n[        5       n UR                  R                  UR                   SU  35      nU(       a  [
        R                  " U5      S   $ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        Mu  )z*
Return the result from the cache backend
:rY   rQ   r   rR   N)r   r   cacher,   r6   r   loadsr   )rU   rV   r   rW   rX   s        r>   rS   rS      s     FE
LL0';< &&q)(33FUNd"d/a/ 0d r@   Fc                 f   U(       a  [        XX#5      $ [        5       nU(       aC   [        U 5      U:X  d%  U(       a  [        5       U-
  S-  Us=:  a  S:  a  O  OO[        S5        MB   [        R
                  " X5      nU(       a  U$ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        MJ  )a  
Return a list of results for a task group.

:param str group_id: the group id
:param bool failures: set to True to include failures
:param int count: Block until there are this many results in the group
:param bool cached: run this against the cache backend
:return: list or results
rQ   r   rR   N)result_group_cachedr   count_groupr   r   get_result_groupgroup_idfailuresrV   countr   rW   rX   s          r>   result_grouprf      s     "8tCCFEH%.Ve^t+t8q8$K  !!(5HFUNd"d/a/ 0d r@   c                 N   U(       d
  [        5       n[        5       nU(       aC   [        U 5      U:X  d%  U(       a  [        5       U-
  S-  Us=:  a  S:  a  O  OO[        S5        MB   UR                  R                  UR                   SU  S35      nU(       ac  / nU HY  n[        R                  " UR                  R                  U5      5      n	U	S   (       d	  U(       d  ME  UR                  U	S   5        M[     U$ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        M  )	zB
Return a list of results for a task group from the cache backend
rQ   r   rR   r[   :keyssuccessrY   N)
r   r   count_group_cachedr   r\   r,   r6   r   r]   append)
rc   rd   rV   re   r   rW   
group_listresult_listtask_keyr)   s
             r>   r_   r_      s     FE"8,5Ve^t+t7a7$K  \\%%(98*E&JK
K&$**6<<+;+;H+EF	?hh&&tH~6 ' FUNd"d/a/ 0d r@   c                     U(       a  [        X5      $ [        5       n [        R                  " U 5      nU(       a  U$ [        5       U-
  S-  Us=:  a  S:  a   g  [	        S5        MJ  )a  
Return the processed task.

:param task_id: the task name or uuid
:type task_id: str or uuid
:param wait: the number of milliseconds to wait for a result
:type wait: int
:param bool cached: run this against the cache backend
:return: the full task object
:rtype: Task
rQ   r   rR   N)fetch_cachedr   r   get_taskr   )rU   rV   r   rW   ts        r>   fetchrs      s^     G**FE
MM'"HFUNd"d/a/ 0d r@   c                    U(       d
  [        5       n[        5       n UR                  R                  UR                   SU  35      nU(       ab  [
        R                  " U5      n[        US   US   US   UR                  S5      US   US   UR                  S5      US	   US
   US   US   S9$ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        M  )z2
Return the processed task from the cache backend
r[   r!   r"   r#   r   r$   r%   r   r&   stoppedrY   ri   )r!   r"   r#   r   r$   r%   r   r&   ru   rY   ri   rQ   r   rR   N)	r   r   r\   r,   r6   r   r]   r   r   )rU   rV   r   rW   rX   r)   s         r>   rp   rp     s     FE
LL0';< &&q)D:&\&\XXf%&\H~+YYH~Y  FUNd"d/a/ 0d' r@   Tc                 f   U(       a  [        XX#5      $ [        5       nU(       aC   [        U 5      U:X  d%  U(       a  [        5       U-
  S-  Us=:  a  S:  a  O  OO[        S5        MB   [        R
                  " X5      nU(       a  U$ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        MJ  )z
Return a list of Tasks for a task group.

:param str group_id: the group id
:param bool failures: set to False to exclude failures
:param bool cached: run this against the cache backend
:return: list of Tasks
rQ   r   rR   N)fetch_group_cachedr   r`   r   r   get_task_grouprb   s          r>   fetch_groupry   !  s     !(dBBFEH%.Ve^t+t8q8$K  3HFUNd"d/a/ 0d r@   c                    U(       d
  [        5       n[        5       nU(       aC   [        U 5      U:X  d%  U(       a  [        5       U-
  S-  Us=:  a  S:  a  O  OO[        S5        MB   UR                  R                  UR                   SU  S35      nU(       a  / nU H  n[        R                  " UR                  R                  U5      5      n	U	S   (       d	  U(       d  ME  [        U	S   U	S   U	S	   U	R                  S
5      U	S   U	S   U	R                  S5      U	S   U	S   U	S   U	R                  S5      U	S   S9n
UR                  U
5        M     U$ [        5       U-
  S-  Us=:  a  S:  a   g  [        S5        GM  )z>
Return a list of Tasks for a task group in the cache backend
rQ   r   rR   r[   rh   ri   r!   r"   r#   r   r$   r%   r   r&   ru   rY   r   )r!   r"   r#   r   r$   r%   r   r&   ru   rY   r   ri   N)r   r   rj   r   r\   r,   r6   r   r]   r   rk   )rc   rd   rV   re   r   rW   rl   	task_listrn   r)   rr   s              r>   rw   rw   ?  sj    FE"8,5Ve^t+t8q8$K  \\%%(98*E&JK
I&$**6<<+;+;H+EF	?hh:!&\!&\!XXf-!&\#H~ $ 3 $Y $Y#H~"hhw/ $YA $$Q'# '$ FUNd"d/a/ 0d3 r@   c                 R    U(       a  [        X5      $ [        R                  " X5      $ )z
Count the results in a group.

:param str group_id: the group id
:param bool failures: Returns failure count if True
:param bool cached: run this against the cache backend
:return: the number of tasks/results in a group
:rtype: int
)rj   r   get_group_count)rc   rd   r   s      r>   r`   r`   k  s#     !(5533r@   c                 H   U(       d
  [        5       nUR                  R                  UR                   SU  S35      nU(       a_  U(       d  [	        U5      $ SnU HC  n[
        R                  " UR                  R                  U5      5      nUS   (       a  M>  US-  nME     U$ g)z3
Count the results in a group in the cache backend
r[   rh   r   ri   r   N)r   r\   r,   r6   lenr   r]   )rc   rd   r   rl   failure_countrn   r)   s          r>   rj   rj   z  s     !!V__$5Qxj"FGJz?""H &&v||'7'7'ABD	??" #  r@   c                 R    U(       a  [        U 5      $ [        R                  " X5      $ )z
Delete a group.

:param str group_id: the group id
:param bool tasks: If set to True this will also delete the group tasks.
Otherwise just the group label is removed.
:param bool cached: run this against the cache backend
:return:
)delete_group_cachedr   delete_group)rc   tasksr   s      r>   r   r     s#     "8,,X--r@   c                     U(       d
  [        5       nUR                   SU  S3nUR                  R                  U5      nUR                  R	                  U5        UR                  R                  U5        g)z'
Delete a group from the cache backend
r[   rh   N)r   r6   r\   r,   delete_manydelete)rc   r   	group_keyrl   s       r>   r   r     s]     ??#1XJe4I!!),J
LLZ(
LL	"r@   c                 x    U(       d
  [        5       nUR                  R                  UR                   SU  35      $ )z&
Delete a task from the cache backend
r[   )r   r\   r   r6   )rU   r   s     r>   delete_cachedr     s3     <<&//!2!G9=>>r@   c                 D    U (       d
  [        5       n U R                  5       $ )z
Returns the current queue size.
Note that this doesn't count any tasks currently being processed by workers.

:param broker: optional broker
:return: current queue size
:rtype: int
)r   
queue_size)r   s    r>   r   r     s     r@   c                    [        U5      n[        5       S   nUR                  SU5      nUR                  SS5        UR                  S[	        5       5      US'   XES'   X5S'   UR                  SS5      (       a  US   US	'   S
US'   US   nUR
                  R                  UR                   SU S3[        R                  " U5      5        U H*  n[        U[        5      (       d  U4n[        U /UQ70 UD6  M,     U$ )z-
enqueues a function with iterable arguments
r   r   r   Nr   r   r   r   r   Tr[   z:args)r   r   r,   r+   r   r\   setr6   r   r2   
isinstancetupler?   )r#   	args_iterr%   r   
iter_groupoptionsr   r$   s           r>   
async_iterr     s     YJJjjf-GKKHjl;GH!G&L{{8T""!(!2GHXF
LL??
1ZL.0C0CI0N $&&7D4*$*'*  r@   c                 B   U(       d  [        5       S   nSn0 nU R                  S5      n[        U5      [        La  U4n[	        U5      S:  a  US   n[	        U5      S:  a  US   nXS'   XS'   X&S'   X6S'   U=(       d
    [        5       US	'   [        US   /UQ70 UD6  U$ )
zk
enqueues a chain of tasks
the chain must be in the format [(func,(args),{kwargs}),(func,(args),{kwargs})]
r    r      r   r   r   r   r   )r   r+   typer   r   r   r?   )r   r   r   r   r   r$   r%   r)   s           r>   async_chainr     s    
 q	DF99Q<DDzw
4y1}Aw
4y1}a7O7O86N-F8tAw(((Lr@   c                   |    \ rS rSrSrSSS\R                  \R                  S4S jrS r	S r
SS jrSS jrS	 rS
rg)Iteri  z'
An async task with iterable arguments
Nc                     Xl         U=(       d    / U l        U=(       d    0 U l        SU l        U=(       d
    [	        5       U l        X@l        XPl        SU l        g N F)	r#   r$   r%   r!   r   r   r   r   r&   )selfr#   r$   r%   r   r   r   s          r>   __init__Iter.__init__  sE     	JB	l,
	r@   c                     U R                   R                  U5        U R                  (       a  SU l        U R                  5       $ )z
add arguments to the set
F)r$   rk   r&   length)r   r$   s     r>   rk   Iter.append	  s0     			<< DL{{}r@   c                    U R                   U R                  S'   U R                  U R                  S'   U R                  U R                  S'   [	        U R
                  U R                  40 U R                  D6U l        SU l        U R                  $ )zE
Start queueing the tasks to the worker cluster
:return: the task id
r   r   r   T)	r   r%   r   r   r   r#   r$   r!   r&   r   s    r>   runIter.run  si    
 !%H"iiF $HTYY		AT[[Awwr@   c                 b    U R                   (       a  [        U R                  XR                  S9$ g)z
return the full list of results.
:param int wait: how many milliseconds to wait for a result
:return: an unsorted list of results
rV   r   Nr&   rY   r!   r   r   rV   s     r>   rY   Iter.result  s%     <<$''[[AA r@   c                 b    U R                   (       a  [        U R                  XR                  S9$ g)z
get the task result objects.
:param int wait: how many milliseconds to wait for a result
:return: an unsorted list of task objects
r   Nr&   rs   r!   r   r   s     r>   rs   
Iter.fetch'  s%     <<tKK@@ r@   c                 ,    [        U R                  5      $ )zO
get the length of the arguments list
:return int: length of the argument list
)r   r$   r   s    r>   r   Iter.length0  s    
 499~r@   )r$   r   r   r#   r!   r%   r&   r   r   )__name__
__module____qualname____firstlineno____doc__r	   r-   r.   r   rk   r   rY   rs   r   __static_attributes__r   r@   r>   r   r     sC     {{YY$
BAr@   r   c                       \ rS rSrSrSS\R                  \R                  S4S jrS r	S r
SS jrSS jrS	 rS
 rSrg)Chaini8  z
A sequential chain of tasks
Nc                     U=(       d    / U l         U=(       d    SU l        U=(       d
    [        5       U l        X0l        X@l        SU l        g r   )r   r   r   r   r   r   r&   )r   r   r   r   r   r   s         r>   r   Chain.__init__=  s9     [b
[b
,
	r@   c                     U R                   R                  XU45        U R                  (       a  [        U R                  5        SU l        U R                  5       $ )zC
add a task to the chain
takes the same parameters as async_task()
F)r   rk   r&   r   r   r   r   r#   r$   r%   s       r>   rk   Chain.appendG  sA    
 	

4v./<<$ DL{{}r@   c                     [        U R                  SS U R                  U R                  U R                  U R
                  S9U l        SU l        U R                  $ )zN
Start queueing the chain to the worker cluster
:return: the chain's group id
N)r   r   r   r   r   T)r   r   r   r   r   r   r&   r   s    r>   r   	Chain.runS  sJ    
 !**Q-**;;;;

 zzr@   c                     U R                   (       a-  [        U R                  XR                  5       U R                  S9$ g)z
return the full list of results from the chain when it finishes. blocks until
timeout.
:param int wait: how many milliseconds to wait for a result
:return: an unsorted list of results
)rV   re   r   N)r&   rf   r   r   r   r   s     r>   rY   Chain.resultb  s2     <<

[[]4;;  r@   c                     U R                   (       a/  [        U R                  UUU R                  5       U R                  S9$ g)z
get the task result objects from the chain when it finishes. blocks until
timeout.
:param failures: include failed tasks
:param int wait: how many milliseconds to wait for a result
:return: an unsorted list of task objects
rd   rV   re   r   N)r&   ry   r   r   r   )r   rd   rV   s      r>   rs   Chain.fetchn  s:     <<

!kkm{{  r@   c                 b    U R                   (       d  g[        U R                  U R                  S9$ )zY
get the index of the currently executing chain element
:return int: current chain index
N)r   )r&   r`   r   r   r   s    r>   currentChain.current  s#    
 ||4::dkk::r@   c                 ,    [        U R                  5      $ )z>
get the length of the chain
:return int: length of the chain
)r   r   r   s    r>   r   Chain.length  s    
 4::r@   )r   r   r   r   r&   r   r   )Tr   )r   r   r   r   r   r	   r-   r.   r   rk   r   rY   rs   r   r   r   r   r@   r>   r   r   8  s@    
 T[[tyyQU

";r@   r   c                      \ rS rSrSrS r\S 5       r\R                  S 5       r\S 5       r	\	R                  S 5       r	\S 5       r
\
R                  S	 5       r
\S
 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       rS rSS jrS rSS jrSS jrSS jrSS jrSrg)	AsyncTaski  z
an async task
c                 D    SU l         SU l        Xl        X l        X0l        g r   )r!   r&   r#   r$   r%   r   s       r>   r   AsyncTask.__init__  s    		r@   c                 &    U R                  SS 5      $ Nr   _get_optionr   s    r>   r   AsyncTask.broker  s    $//r@   c                 (    U R                  SU5        g r   _set_optionr   values     r>   r   r         5)r@   c                 &    U R                  SS 5      $ Nr   r   r   s    r>   r   AsyncTask.sync      --r@   c                 (    U R                  SU5        g r   r   r   s     r>   r   r         'r@   c                 &    U R                  SS 5      $ Nr   r   r   s    r>   r   AsyncTask.save  r   r@   c                 (    U R                  SU5        g r   r   r   s     r>   r   r     r   r@   c                 &    U R                  SS 5      $ Nr   r   r   s    r>   r   AsyncTask.hook  r   r@   c                 (    U R                  SU5        g r   r   r   s     r>   r   r     r   r@   c                 &    U R                  SS 5      $ Nr   r   r   s    r>   r   AsyncTask.group  s    ..r@   c                 (    U R                  SU5        g r   r   r   s     r>   r   r     s    %(r@   c                 B    U R                  S[        R                  5      $ Nr   )r   r	   r-   r   s    r>   r   AsyncTask.cached  s    $++66r@   c                 (    U R                  SU5        g r   r   r   s     r>   r   r     r   r@   c                 r    SU R                   ;   a  X R                   S   U'   OX R                   U'   SU l        g )Nr   F)r%   r&   )r   r;   r   s      r>   r   AsyncTask._set_option  s2    $++%,1KK$S)$KKr@   Nc                     SU R                   ;   a  U R                   S   R                  X5      $ U R                   R                  X5      $ )Nr   )r%   r,   )r   r;   defaults      r>   r   AsyncTask._get_option  s;    $++%;;{+//==;;??300r@   c                     [        U R                  /U R                  Q70 U R                  D6U l        SU l        U R                  $ )NT)r?   r#   r$   r%   r!   r&   r   s    r>   r   AsyncTask.run  s4    TYYBBdkkBwwr@   c                 b    U R                   (       a  [        U R                  XR                  S9$ g Nr   r   r   s     r>   rY   AsyncTask.result  s#    <<$''[[AA r@   c                 b    U R                   (       a  [        U R                  XR                  S9$ g r   r   r   s     r>   rs   AsyncTask.fetch  s#    <<tKK@@ r@   c                     U R                   (       a3  U R                  (       a!  [        U R                  UUUU R                  S9$ g g Nr   )r&   r   rf   r   r   rd   rV   re   s       r>   rf   AsyncTask.result_group  s:    <<DJJ

!{{  '<r@   c                     U R                   (       a3  U R                  (       a!  [        U R                  UUUU R                  S9$ g g r   )r&   r   ry   r   r   s       r>   ry   AsyncTask.fetch_group  s:    <<DJJ

!{{  '<r@   )r$   r#   r!   r%   r&   Nr   )Fr   N)Tr   N)r   r   r   r   r   r   propertyr   setterr   r   r   r   r   r   r   r   rY   rs   rf   ry   r   r   r@   r>   r   r     s9    0 0 ]]* * . . 
[[( ( . . 
[[( ( . . 
[[( ( / / \\) ) 7 7 ]]* *1
BAr@   r   c                    SSK Jn  SSKJn  [	        5       n[	        5       n[
        R                  " U 5      nUR                  U5        UR                  S5        U" X4[        SS5      5        UR                  S5        U" U5        UR                  5         UR                  5         UR                  5         UR                  5         US   $ )z2Simulate a package travelling through the cluster.r   )monitor)workerSTOPfrE   r!   )django_q.monitorr  django_q.workerr	  r   r   r]   putr   closejoin_thread)r<   r  r	  
task_queueresult_queuer)   s         r>   r3   r3     s    (&J7Lt$DNN4NN6
:U3^4VL:r@   )r   N)Fr   NN)Tr   NN)FNr  )1r   multiprocessingr   r   r   	django.dbr   django.utilsr   django_q.brokersr   django_q.confr	   r
   django_q.humanhashr   django_q.modelsr   r   django_q.queuesr   django_q.signalsr   django_q.signingr   r?   rO   r-   rY   rS   rf   r_   rs   rp   ry   rw   r`   rj   r   r   r   r   r   r.   r   r   r   r   r3   r   r@   r>   <module>r     s   " "  % ! ( & # * ! ( *9x3l 4;; 0  %*dkk >< $++ 0: $(atDKK <)X $) 4$ "'t{{ .	#?6 "$++DIId 2C CLU Uph hVr@   