
    s,gBV                        d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZm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mZ dd	lmZ dd
lmZmZ ddl m!Z!m"Z"m#Z# erd dl$m%Z% d dl&m'Z' d dl(m)Z)  ejT                  d      Z+ G d d      Z, G d de,      Z- G d de,      Z. G d de,      Z/ G d de,      Z0 G d de,      Z1 G d de,      Z2d"ddd ee3   fd!Z4y)#    N)datetime	timedeltatimezone)TYPE_CHECKINGAnyCallableListOptionalTypeUnioncast)resolve_serializer   )DEFAULT_FAILURE_TTL)AbandonedJobErrorInvalidJobOperationNoSuchJobError)Job	JobStatus)Queue)BaseDeathPenaltyUnixSignalDeathPenalty)as_textbackend_classcurrent_timestamp)Redis)Pipeline)	Executionzrq.registryc                      e Zd ZU dZeZeZdZe	de
f   ed<   	 	 	 	 	 	 d*deded   d	eed
      ded   de
deee      fdZd Zd Zde
defdZedefd       Zd+defdZd,dd
ded   dedefdZd-ded
ef   ded   defdZd.dd
ded   fdZd.dee   fd Zd/d!ed"ed#ededee   f
d$Z d% Z!dd
de"fd&Z#d0d'ed
ef   d(edd
fd)Z$y)1BaseRegistryz
    Base implementation of a job registry, implemented in Redis sorted set.
    Each job is stored as a key in the registry, scored by expiration time
    (unix timestamp).
    zrq:registry:{0}.cleanupNname
connectionr   	job_classr   queuer   
serializerdeath_penalty_classc                 L   |r4|j                   | _         |j                  | _        |j                  | _        n|| _         || _        t        |      | _        | j                  j                  | j                         | _        t        | d|      | _        t        | d|      | _	        y )Nr$   )overrider'   )
r"   r#   r&   r   key_templateformatkeyr   r$   r'   )selfr"   r#   r$   r%   r&   r'   s          e/var/www/trellinator.diamondhoofcare.com/public_html/venv/lib/python3.12/site-packages/rq/registry.py__init__zBaseRegistry.__init__'   s     

DI#..DO#..DODI(DO0<DO$$++DII6&t[9M#07LWj#k     c                     | j                   S )z+Returns the number of jobs in this registry)countr-   s    r.   __len__zBaseRegistry.__len__=   s    zzr0   c                     | j                   |j                   k(  xrA | j                  j                  j                  |j                  j                  j                  k(  S N)r"   r#   connection_poolconnection_kwargs)r-   others     r.   __eq__zBaseRegistry.__eq__A   sG    II# x//AAUEUEUEeEeEwEww	
r0   itemreturnc                     |}t        || j                        r|j                  }| j                  j	                  | j
                  t        t        |            duS )z
        Returns a boolean indicating registry contains the given
        job instance or job id.

        Args:
            item (Union[str, Job]): A Job ID or a Job.
        N)
isinstancer$   idr#   zscorer,   r   str)r-   r;   job_ids      r.   __contains__zBaseRegistry.__contains__G   sG     dDNN+WWF%%dhhS&0AB$NNr0   c                 &    | j                  d      S )z{Returns the number of jobs in this registry after running cleanup

        Returns:
            int: _description_
        T)r!   )get_job_countr3   s    r.   r2   zBaseRegistry.countT   s     !!$!//r0   c                 p    |r| j                          | j                  j                  | j                        S )zReturns the number of jobs in this registry after optional cleanup.

        Args:
            cleanup (bool, optional): _description_. Defaults to True.

        Returns:
            int: _description_
        )r!   r#   zcardr,   )r-   r!   s     r.   rE   zBaseRegistry.get_job_count]   s)     LLN$$TXX..r0   jobpipeliner   xxc                    |dk  r|nt               |z   }|dk(  rd}|8t        t        |j                  | j                  |j
                  |i|            S | j                  j                  | j                  |j
                  |i|      S )a  Adds a job to a registry with expiry time of now + ttl, unless it's -1 which is set to +inf

        Args:
            job (Job): The Job to add
            ttl (int, optional): The time to live. Defaults to 0.
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            xx (bool, optional): .... Defaults to False.

        Returns:
            result (int): The ZADD command result
        r   +infrJ   )r   r   intzaddr,   r?   r#   )r-   rH   ttlrI   rJ   scores         r.   addzBaseRegistry.addj   s|     Qw$5$7#$=B;EX]]488cffe_]LMM##DHHsvvuo"#EEr0   
delete_jobc                 @   ||n| j                   }t        || j                        r|j                  n|}|j	                  | j
                  |      }|rKt        || j                        r|}n"t        j                  ||| j                        }|j                          |S )aE  Removes job from registry and deletes it if `delete_job == True`

        Args:
            job (Job): The Job to remove from the registry
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            delete_job (bool, optional): If should delete the job.. Defaults to False.
        r#   r&   )
r#   r>   r$   r?   zremr,   r   fetchr&   delete)r-   rH   rI   rT   r#   rB   resultjob_instances           r.   removezBaseRegistry.remove~   s     "*!5X4??
%c4>>:62#t~~.""yyJSWSbSbc!r0   c                     ||n| j                   }|j                         D cg c]  }|j                   }}|r |j                  | j                  g| S yc c}w )zRemoves job executions from registry

        Args:
            job (Job): The Job to remove from the registry
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
        N)r#   get_executionscomposite_keyrW   r,   )r-   rH   rI   r#   	executionexecution_idss         r.   remove_executionszBaseRegistry.remove_executions   s^     "*!5X4??
BEBTBTBVWY00WW":??488<m<<  Xs   A	timestampc                     ||n	t               }| j                  j                  | j                  d|      }|D cg c]  }t	        |       c}S c c}w )zReturns job ids whose score are less than current timestamp.

        Returns ids for jobs with an expiry time earlier than timestamp,
        specified as seconds since the Unix epoch. timestamp defaults to call
        time if unspecified.
        r   r   r#   zrangebyscorer,   r   )r-   rc   rR   expired_jobsrB   s        r.   get_expired_job_idsz BaseRegistry.get_expired_job_ids   sH     '2	8I8K44TXXq%H.:;F;;;s   Astartenddescc                     |r| j                          | j                  j                  | j                  |||      D cg c]  }t	        |       c}S c c}w )au  Returns list of all job ids.

        Args:
            start (int, optional): _description_. Defaults to 0.
            end (int, optional): _description_. Defaults to -1.
            desc (bool, optional): _description_. Defaults to False.
            cleanup (bool, optional): _description_. Defaults to True.

        Returns:
            _type_: _description_
        )rk   )r!   r#   zranger,   r   )r-   ri   rj   rk   r!   rB   s         r.   get_job_idszBaseRegistry.get_job_ids   sF     LLN.2oo.D.DTXXuVY`d.D.efFfffs   Ac                 Z    t        | j                  | j                  | j                        S )z3Returns Queue object associated with this registry.rV   )r   r"   r#   r&   r3   s    r.   	get_queuezBaseRegistry.get_queue   s    TYY4??tWWr0   c                     | j                   j                  | j                  |j                        }t	        j
                  |      S )zkReturns job's expiration time.

        Args:
            job (Job): The Job to get the expiration
        )r#   r@   r,   r?   r   utcfromtimestamp)r-   rH   rR   s      r.   get_expiration_timez BaseRegistry.get_expiration_time   s3     &&txx8((//r0   	job_or_idat_frontc                 t   t        || j                        r|}|j                  }n4| j                  }| j                  j                  || j                  |      }| j                  j                  | j                  |j                        }|st        | j                  j                         5 }t        |j                  | j                  | j                  |      }d|_        d|_        d|_        |j                          |j!                  |||      }|j#                          ddd       |S # 1 sw Y   |S xY w)a  Requeues the job with the given job ID.

        Args:
            job_or_id (Union[&#39;Job&#39;, str]): The Job or the Job ID
            at_front (bool, optional): If the Job should be put at the front of the queue. Defaults to False.

        Raises:
            InvalidJobOperation: If nothing is returned from the `ZREM` operation.

        Returns:
            Job: The Requeued Job.
        rV   )r#   r$   r&   N )rI   ru   )r>   r$   r&   rX   r#   rW   r,   r?   r   rI   r   origin
started_atended_at	_exc_infosave_enqueue_jobexecute)r-   rt   ru   rH   r&   rZ   rI   r%   s           r.   requeuezBaseRegistry.requeue   s     i0CJJ..&&yT__Yc&dC%%dhh7%%__%%' 	8#**DNNgqrE!CNCLCMHHJ$$S8h$OC	 
	 
s   ,A7D--D7)defaultNNNNN)T)r   NF)NFr6   )r   rL   FTF)%__name__
__module____qualname____doc__r   r$   r   r'   r*   r   r   __annotations__rA   r
   r   r   r/   r4   r:   boolrC   propertyrO   r2   rE   rS   r   r\   rb   floatrh   r	   rn   rp   r   rs   r    r0   r.   r    r       s    I0$Lc3h (,+/#'@Dll W%l DK(	l
  l l &d+;&<=l,
O O O 0s 0 0/S /Fu Fx
/C FPT Fad F(%s
+ x
7K `d &
=U 
=hz6J 
=	<Xe_ 	<g gs gt gVZ gfjknfo g X0u 0 0 ucz!2  d  u  r0   r    c            	       f    e Zd ZdZdZddee   dee   fdZdddd	d
de	de
fdZdddddd	d
de	fdZy)StartedJobRegistrya   
    Registry of currently executing jobs. Each queue maintains a
    StartedJobRegistry. Jobs in this registry are ones that are currently
    being executed.

    Jobs are added to registry right before they are executed and removed
    right after completion (success or failure).
    z
rq:wip:{0}Nrc   exception_handlersc                 l   ||n	t               }| j                  |      }|rtt        | j                  | j                  | j
                        }| j                         }| j                  j                         5 }|D ]  }	 | j                  j                  || j                  | j
                        }	|	j                  | j                  t        t               t        j                                |r8|D ]3  }
 |
|	t        t               t        j                               }|d}|r3 n |	j                   xr |	j                   dkD  }|r|	j#                  ||       dt        j$                   }t&        j)                  | j*                  j$                   d|	j,                   dt        j$                   d	| d
       |	j/                  t0        j2                         dt        j$                   d| dt5        j6                          |	_        |	j;                  |d       |	j=                  d|       |j?                  |	|	j@                         |jC                  |	        |jE                  | jF                  d|       |jI                          ddd       |S |S # t        $ r Y )w xY w# 1 sw Y   |S xY w)a  Remove abandoned jobs from registry and add them to FailedJobRegistry.

        Removes jobs with an expiry time earlier than timestamp, specified as
        seconds since the Unix epoch. timestamp defaults to call time if
        unspecified. Removed jobs are added to the global failed job queue.

        Args:
            timestamp (datetime): The datetime to use as the limit.
        Nr&   rV   Tr   zdue to z cleanup: Moving job z to z ()z	Moved to z, z, at F)rI   include_metarL   rQ   rI   )%r   rh   FailedJobRegistryr"   r#   r&   rp   rI   r$   rX   r   execute_failure_callbackr'   r   	tracebackextract_stackretries_leftretryr   loggerwarning	__class__r?   
set_statusr   FAILEDr   nowr{   r|   r!   rS   failure_ttlenqueue_dependentszremrangebyscorer,   r~   )r-   rc   r   rR   job_idsfailed_job_registryr%   rI   rB   rH   handlerfallthroughr   
exc_strings                 r.   r!   zStartedJobRegistry.cleanup   s    '2	8I8K**51"3DIIt[_[j[j"kNN$E))+ +#x% '6F!"nn226doobfbqbq2r 00002CEVEXZcZqZqZs *'9 
&G*1 #%68I8KYMdMdMf+K
  +2.2#. %
&  ,,E1A1AA1EE		%2 (//@/I/I.J%K
#~~6677LSVVHTXYjYsYsXt u  *|1. y'7'78*34E4N4N3OrR\Q]]bckcococqbr(s(GX>+//S__E005O'6R ))$((Au=  "W+#Z wS * ! !	+#Z s8   9J)2J3A2J)&E'J)	J&"J)%J&&J))J3r`   r   rI   r   rJ   r<   c                     |dk  r|nt               |z   }|dk(  rd}|j                  | j                  |j                  |i|      S )a  Adds an execution to a registry with expiry time of now + ttl, unless it's -1 which is set to +inf

        Args:
            execution (Execution): The Execution to add
            ttl (int, optional): The time to live. Defaults to 0.
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            xx (bool, optional): .... Defaults to False.

        Returns:
            result (int): The ZADD command result
        r   rL   rM   rN   )r   rP   r,   r_   )r-   r`   rI   rQ   rJ   rR   s         r.   add_executionz StartedJobRegistry.add_execution4  sJ     Qw$5$7#$=B;E}}TXX	(?(?'GB}OOr0   rH   r   rT   c                 r    ||n| j                   }|j                  | j                  |j                        }|S )a}  Removes job from registry and deletes it if `delete_job == True`

        Args:
            execution (Execution): The Execution to add
            job (Job): The Job to remove from the registry
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            delete_job (bool, optional): If should delete the job.. Defaults to False.
        )r#   rW   r,   r_   )r-   r`   rH   rI   rT   r#   rZ   s          r.   remove_executionz#StartedJobRegistry.remove_executionF  s5     "*!5X4??
9+B+BC r0   NN)r   Fr   )r   r   r   r   r*   r
   r   listr!   r   rO   r   r   r   r0   r.   r   r      su      L>% >XVZ^ >@P{ Pj PUY Pfi P$+ E Z ei r0   r   c                   (    e Zd ZdZdZddee   fdZy)FinishedJobRegistryz
    Registry of jobs that have been completed. Jobs are added to this
    registry after they have successfully completed for monitoring purposes.
    zrq:finished:{0}Nrc   c                 n    ||n	t               }| j                  j                  | j                  d|       yzRemove expired jobs from registry.

        Removes jobs with an expiry time earlier than timestamp, specified as
        seconds since the Unix epoch. timestamp defaults to call time if
        unspecified.
        Nr   r   r#   r   r,   r-   rc   rR   s      r.   r!   zFinishedJobRegistry.cleanup`  .     '2	8I8K((1e<r0   r6   )r   r   r   r   r*   r
   r   r!   r   r0   r.   r   r   X  s    
 %L=% =r0   r   c            	       P    e Zd ZdZdZddee   fdZ	 	 	 	 dddded	ed
   de	fdZ
y)r   z-
    Registry of containing failed jobs.
    zrq:failed:{0}Nrc   c                 n    ||n	t               }| j                  j                  | j                  d|       yr   r   r   s      r.   r!   zFailedJobRegistry.cleanupr  r   r0   rH   r   r   rI   r   _save_exc_to_jobc                 L   |t         }|dk  r|nt               |z   }|r|}n| j                  j                         }||_        |j                  |d|       |j                  ||       |j                  | j                  |j                  |i       |s|j                          yy)z
        Adds a job to a registry with expiry time of now + ttl.
        `ttl` defaults to DEFAULT_FAILURE_TTL if not specified.
        Nr   F)rI   r   include_resultr   )r   r   r#   rI   r{   r|   r!   rP   r,   r?   r~   )r-   rH   rQ   r   rI   r   rR   ps           r.   rS   zFailedJobRegistry.add|  s     ;%CQw$5$7#$=A((*A"!%@PQa(	txx#&&%)IIK r0   r6   )Nrw   NF)r   r   r   r   r*   r
   r   r!   rA   r   rS   r   r0   r.   r   r   k  s\     #L=% = )-!& 	
 :& r0   r   c                   0     e Zd ZdZdZddZd fd	Z xZS )DeferredJobRegistryzH
    Registry of deferred jobs (waiting for another job to finish).
    zrq:deferred:{0}c           	         ||n	t               }| j                  |      }|rt        | j                  | j                  | j
                        }| j                  j                         5 }|D ]  }	 | j                  j                  || j                  | j
                        }|j                  t        j                  |       dt        j                         z  }|j                  ||j                   ||d        |j#                  | j$                  d|       |j'                          ddd       |S |S # t        $ r Y w xY w# 1 sw Y   |S xY w)a,  Remove expired jobs from registry and add them to FailedJobRegistry.
        Removes jobs with an expiry time earlier than timestamp, specified as
        seconds since the Unix epoch. timestamp defaults to call time if
        unspecified. Removed jobs are added to the failed job registry.
        Nr   rV   )rI   z@Expired in DeferredJobRegistry, moved to FailedJobRegistry at %sTr   )r   rh   r   r"   r#   r&   rI   r$   rX   r   r   r   r   r   r   rS   r   r   r,   r~   )	r-   rc   rR   r   r   rI   rB   rH   exc_infos	            r.   r!   zDeferredJobRegistry.cleanup  s4    '2	8I8K**51"3DIIt[_[j[j"k))+ #x% \F!"nn226doobfbqbq2r NN9#3#3hNGadldpdpdrrH'++C(HVZ[\ ))$((Au=  "# w * ! !	# s1   )E02D4"BE4	E =E?E  EEc                 8    |d}t         t        |   ||||      S )zp
        Adds a job to a registry with expiry time of now + ttl.
        Defaults to -1 (never expire).
        rL   )superr   rS   )r-   rH   rQ   rI   rJ   r   s        r.   rS   zDeferredJobRegistry.add  s(    
 ;C($3ChKKr0   r6   )NNF)r   r   r   r   r*   r!   rS   __classcell__r   s   @r.   r   r     s      %L8L Lr0   r   c                        e Zd ZdZdZ fdZdddded   fdZd	 Zdd
ee	   ded   fdZ
dd
ee   dedee   fdZdedef   de	fdZ xZS )ScheduledJobRegistryz%
    Registry of scheduled jobs.
    zrq:scheduled:{0}c                 F    t        |   |i | | j                  | _        y r6   )r   r/   rh   get_jobs_to_enqueue)r-   argskwargsr   s      r.   r/   zScheduledJobRegistry.__init__  s%    $)&) $(#;#; r0   rH   r   rI   r   c                 v   |j                   sYt        t        t        j                  dk(  rt        j                  nt        j
                               }|j                  |      }t        j                  |j                               }| j                  j                  | j                  |j                  |i      S )z
        Adds job to registry, scored by its execution time (in UTC).
        If datetime has no tzinfo, it will assume localtimezone.
        r   )seconds)tzinfo)r   r   r   timedaylightaltzonereplacecalendartimegmutctimetupler#   rP   r,   r?   )r-   rH   scheduled_datetimerI   tzrc   s         r.   schedulezScheduledJobRegistry.schedule  s     "(()t}}PQ?QdmmW[WcWc,defB!3!;!;2!;!FOO$6$C$C$EF	##DHHsvvy.ABBr0   c                      yzThis method is only here to prevent errors because this method is
        automatically called by `count()` and `get_job_ids()` methods
        implemented in BaseRegistry.Nr   r3   s    r.   r!   zScheduledJobRegistry.cleanup       	r0   rc   c                 x    ||n| j                   }||n	t               }|j                  | j                  d|      S )a  Remove jobs whose timestamp is in the past from registry.

        Args:
            timestamp (Optional[datetime], optional): The timestamp. Defaults to None.
            pipeline (Optional[Pipeline], optional): The Redis pipeline. Defaults to None.
        r   )r#   r   r   r,   )r-   rc   rI   r#   rR   s        r.   remove_jobsz ScheduledJobRegistry.remove_jobs  s=     "*!5X4??
"+"7Y=N=P**488Q>>r0   
chunk_sizer<   c                     ||n	t               }| j                  j                  | j                  d|d|      }|D cg c]  }t	        |       c}S c c}w )a  Get's a list of job IDs that should be scheduled.

        Args:
            timestamp (Optional[int]): _description_. Defaults to None.
            chunk_size (int, optional): _description_. Defaults to 1000.

        Returns:
            jobs (List[str]): A list of Job ids
        r   )ri   numre   )r-   rc   r   rR   jobs_to_schedulerB   s         r.   get_jobs_to_schedulez)ScheduledJobRegistry.get_jobs_to_schedule  sR     #,"7Y=N=P??881eSTZd8e.>?F???s   Art   c                     t        |t              r|j                  }n|}| j                  j	                  | j
                  |      }|st        t        j                  |t        j                        S )a:  Returns datetime (UTC) at which job is scheduled to be enqueued

        Args:
            job_or_id (Union[Job, str]): The Job instance or Job ID

        Raises:
            NoSuchJobError: If the job was not found

        Returns:
            datetime (datetime): The scheduled time as datetime object
        )r   )r>   r   r?   r#   r@   r,   r   r   fromtimestampr   utc)r-   rt   rB   rR   s       r.   get_scheduled_timez'ScheduledJobRegistry.get_scheduled_time  sV     i%\\FF&&txx8  %%e==r0   r6   r   )Ni  )r   r   r   r   r*   r/   r
   r   r!   r   r   rO   r	   rA   r   r   r   r   r   s   @r.   r   r     s     &L<CE C*AU C	?Xh%7 	?(S]J^ 	?@hsm @PS @_cdg_h @>E%*,= >( >r0   r   c                   *    e Zd ZdZddee   fdZd Zy)CanceledJobRegistryzrq:canceled:{0}Nrc   c                     t         r6   )NotImplementedError)r-   rc   s     r.   rh   z'CanceledJobRegistry.get_expired_job_ids  s    !!r0   c                      yr   r   r3   s    r.   r!   zCanceledJobRegistry.cleanup   r   r0   r6   )r   r   r   r*   r
   r   rh   r!   r   r0   r.   r   r     s    $L"Xe_ "r0   r   r%   r   r   c                 0   t        | j                  | j                  | j                  | j                        j                          t        | j                  | j                  | j                  | j                        j                  |       t        | j                  | j                  | j                  | j                        j                          t        | j                  | j                  | j                  | j                        j                          y)zCleans StartedJobRegistry, FinishedJobRegistry and FailedJobRegistry, and DeferredJobRegistry of a queue.

    Args:
        queue (Queue): The queue to clean
    )r"   r#   r$   r&   )r   N)	r   r"   r#   r$   r&   r!   r   r   r   )r%   r   s     r.   clean_registriesr   '  s     ZZE$4$4\a\l\lgiZZE$4$4\a\l\lg!3g4ZZE$4$4\a\l\lgiZZE$4$4\a\l\lgir0   r6   )5r   loggingr   r   r   r   r   typingr   r   r   r	   r
   r   r   r   rq.serializersr   defaultsr   
exceptionsr   r   r   rH   r   r   r%   r   timeoutsr   r   utilsr   r   r   redisr   redis.clientr   rq.executionsr   	getLoggerr   r    r   r   r   r   r   r   r   r   r   r0   r.   <module>r      s        2 2 R R R - ) N N   > < <%' 
		=	)J JZk k`=, =&, ,^+L, +L\O>< O>d
, 
G $ r0   