
    s,g              	          d dl Z d dlZd dlZd dlZd dl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mZ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mZ er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$ ddl%m&Z&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l0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<  e jz                  d      Z> G d d edg d            Z?e G d d             Z@y)    N)
namedtuple)datetime	timedeltatimezone)total_ordering)
TYPE_CHECKINGAnyCallableDictListOptionalTupleTypeUnioncast)
WatchError   )BaseDeathPenaltyUnixSignalDeathPenalty)Redis)Pipeline)Retry)DEFAULT_RESULT_TTL)
Dependency)DequeueTimeoutNoSuchJobError)IntermediateQueue)CallbackJob	JobStatus)bluegreen)resolve_serializer)FunctionReferenceTypeJobDependencyType)as_textbackend_classcompactget_versionimport_attributenowparse_timeoutzrq.queuec                       e Zd ZdZdZy)EnqueueDataz^Helper type to use when calling enqueue_many
    NOTE: Does not support `depends_on` yet.
     N)__name__
__module____qualname____doc__	__slots__r/       b/var/www/trellinator.diamondhoofcare.com/public_html/venv/lib/python3.12/site-packages/rq/queue.pyr.   r.   "   s    . Ir5   r.   funcargskwargstimeout
result_ttlttlfailure_ttldescription
depends_onjob_idat_frontmetaretry
on_success
on_failure
on_stoppedc            &          e Zd ZU eZed   ed<   eZee	   ed<   dZ
eed<   dZeed<   dZeed	<   e	 	 	 dudddeed      deee	      ded    fd       Ze	 	 	 dudedddeed      dedeee	      dd fd       Zdd
d
dd
d
efdeded   dee   dedeeeed   f      dedeee	      fdZd Zd Zd Zdeeeef   fdZed        Zed        Zede fd       Z!ed        Z"edee   fd        Z#defd!Z$d" Z%d# Z&dvd$efd%Z'defd&Z(edefd'       Z)d(eded   fd)Z*d*edef   dee   fd+Z+dwd,ed-edee   fd.Z,dwd,ed-eded   fd/Z-edee   fd0       Z.eded   fd1       Z/edefd2       Z0ed3        Z1ed4        Z2ed5        Z3ed6        Z4ed7        Z5ed8        Z6dxd*edef   d9ed:   fd;Z7d< Z8dyd(ed9ed:   d=efd>Z9d
d
d
d
d
d
d
d
d
d
e:jv                  d
fd
d
d
d
d?d@dAdBeeed
f   dCee<   dDee   dEee   dFee   dGee   dHee   dIedJ   d(ee   dKee<   dLe:dMedN   dOeee=e>f      dPeee=e>f      dQeee=e>f      dRee   def$dSZ?dxdTdd9ed:   ddfdUZ@	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dzd@dAdBeeed
f   dCee<   dDee   dEee   dFee   dGee   dHee   dIedJ   d(ee   d=edKee<   dMedN   dOeee=e>dVef   f      dPeee=e>dVef   f      dQeee=e>dVef   f      d9ed:   def$dWZAeB	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d{d@dAdBeeed
f   dCee<   dDee   dEee   dFee   dGee   dHee   dIee   d(ee   d=edKee<   dMedN   dOeee=e>f      dPeee=e>f      dQeee=e>f      deCf"dX       ZD	 d|dYedZ   d9ed:   dRee   dee   fd[ZEdTddefd\ZFed}d]       ZGd~d^ZHd_eIfd`ZJdxdTdd_eId9ed:   fdaZKdbeLd@dAddfdcZMdydTdd9ed:   d=edefddZNdydTdd9ed:   d=edefdeZOddfZP	 d|dTdd9ed:   dgee   fdhZQdee   fdiZRedxdjee   dDee   ded   fdk       ZSedddedDee   fdl       ZTe	 	 	 dudmed    dDee   dddeed      dedeee	      deedn      fdo       ZUdp ZVdq ZWdr ZXds ZYdt ZZy
)Queuer   	job_classdeath_penalty_class   DEFAULT_TIMEOUTz	rq:queue:redis_queue_namespace_prefixz	rq:queuesredis_queues_keysN
connectionr   returnc                      dt         t        t        f   f fd}j                   j                        }|D cg c]  }|s ||       }}|S c c}w )a  Returns an iterable of all Queues.

        Args:
            connection (Optional[Redis], optional): The Redis Connection. Defaults to None.
            job_class (Optional[Job], optional): The Job class to use. Defaults to None.
            serializer (optional): The serializer to use. Defaults to None.
            death_penalty_class (Optional[Job], optional): The Death Penalty class to use. Defaults to None.

        Returns:
            queues (List[Queue]): A list of all queues.
        	queue_keyc                 B    j                  t        |             S )NrP   rJ   
serializerrK   )from_queue_keyr&   )rS   clsrP   rK   rJ   rV   s    r6   to_queuezQueue.all.<locals>.to_queue\   s/    %%	"%#%$7 &  r5   )r   bytesstrsmembersrO   )	rX   rP   rJ   rV   rK   rY   all_registerd_queuesrq_key
all_queuess	   `````    r6   allz	Queue.allH   s\    (	eSj 1 	 	  *2233H3HI5IT6Vhv&T
T Us   A

ArS   rV   c                     | j                   }|j                  |      st        dj                  |            |t	        |      d } | |||||      S )a  Returns a Queue instance, based on the naming conventions for naming
        the internal Redis keys.  Can be used to reverse-lookup Queues by their
        Redis keys.

        Args:
            queue_key (str): The queue key
            connection (Redis): Redis connection. Defaults to None.
            job_class (Optional[Job], optional): Job class. Defaults to None.
            serializer (Any, optional): Serializer. Defaults to None.
            death_penalty_class (Optional[BaseDeathPenalty], optional): Death penalty class. Defaults to None.

        Raises:
            ValueError: If the queue_key doesn't start with the defined prefix

        Returns:
            queue (Queue): The Queue object
        zNot a valid RQ queue key: {0}NrU   )rN   
startswith
ValueErrorformatlen)rX   rS   rP   rJ   rV   rK   prefixnames           r6   rW   zQueue.from_queue_keyi   s`    4 11##F+<CCINOOV'!! 3
 	
r5   defaultTrg   default_timeoutis_asyncc                    |st        d      || _        | j                  }	|| _        dj	                  |	|      | _        t        |      xs | j                  | _        || _	        t        | _        d|v r$|d   | _	        t        j                  dt               |(t        |t               rt#        |      | _        n|| _        || _        t)        |      | _        d| _        y)a  Initializes a Queue object.

        Args:
            name (str, optional): The queue name. Defaults to 'default'.
            default_timeout (Optional[int], optional): Queue's default timeout. Defaults to None.
            connection (Optional[Redis], optional): Redis connection. Defaults to None.
            is_async (bool, optional): Whether jobs should run "async" (using the worker).
                If `is_async` is false, jobs will run on the same process from where it was called. Defaults to True.
            job_class (Union[str, 'Job', optional): Job class or a string referencing the Job class path.
                Defaults to None.
            serializer (Any, optional): Serializer. Defaults to None.
            death_penalty_class (Type[BaseDeathPenalty, optional): Job class or a string referencing the Job class path.
                Defaults to UnixSignalDeathPenalty.
        z<Queue() missing 1 required positional argument: 'connection'z{0}{1}asyncz9The `async` keyword is deprecated. Use `is_async` insteadN)	TypeErrorrP   rN   rg   rd   _keyr,   rM   _default_timeout	_is_asyncloggerlogwarningswarnDeprecationWarning
isinstancer[   r*   rJ   rK   r#   rV   redis_server_version)
selfrg   rP   ri   rj   rJ   rV   rK   r:   rf   s
             r6   __init__zQueue.__init__   s    2 Z[[$22	OOFD1	 -o > V$BVBV!f#G_DNMMUWij  )S)!1)!<!*#6 ,Z8DH!r5   c                     | j                   S Ncountrx   s    r6   __len__zQueue.__len__   s    zzr5   c                      y)NTr/   r~   s    r6   __bool__zQueue.__bool__   s    r5   c              #      K   |  y wr{   r/   r~   s    r6   __iter__zQueue.__iter__   s     
s   c                 f    | j                   st        | j                        | _         | j                   S )zReturn Redis server version of connection

        Returns:
            redis_version (Tuple): A tuple with the parsed Redis version (eg: (5,0,0))
        )rw   r)   rP   r~   s    r6   get_redis_server_versionzQueue.get_redis_server_version   s*     (((3DOO(DD%(((r5   c                     | j                   S )z%Returns the Redis key for this Queue.)rn   r~   s    r6   keyz	Queue.key   s     yyr5   c                 @    t        j                  | j                        S )z-Returns the Redis key for intermediate queue.)r   get_intermediate_queue_keyrn   r~   s    r6   intermediate_queue_keyzQueue.intermediate_queue_key   s     !;;DIIFFr5   c                 D    t        | j                  | j                        S )z6Returns the IntermediateQueue instance for this Queue.rP   )r   r   rP   r~   s    r6   intermediate_queuezQueue.intermediate_queue   s     !dooFFr5   c                      d| j                   z  S )z7Redis key used to indicate this queue has been cleaned.zrq:clean_registries:%s)rg   r~   s    r6   registry_cleaning_keyzQueue.registry_cleaning_key   s     ($))33r5   c                     ddl m} | j                  j                  |j	                  | j
                              }|t        |j                               S d S )Nr   )RQScheduler)rq.schedulerr   rP   getget_locking_keyrg   intdecode)rx   r   pids      r6   scheduler_pidzQueue.scheduler_pid   sB    ,oo!!+"="=dii"HI$'Os3::< ==r5   c                 ^    | j                   j                  | j                  ddd      }|sy|S )zReturns a boolean indicating whether a lock to clean this queue
        is acquired. A lock expires in 899 seconds (15 minutes - 1 second)

        Returns:
            lock_acquired (bool)
        r   Ti  )nxexF)rP   setr   )rx   lock_acquireds     r6   acquire_maintenance_lockzQueue.acquire_maintenance_lock   s4     ++D,F,FdWZ+[r5   c                 N    | j                   j                  | j                         y)z?Deletes the maintenance lock after registries have been cleanedN)rP   deleter   r~   s    r6   release_maintenance_lockzQueue.release_maintenance_lock   s    t99:r5   c                     dj                  | j                  j                        j                  d      }| j                  j                  |      } || j                  g      S )a  Removes all messages on the queue.
        This is currently being done using a Lua script,
        which iterates all queue messages and deletes the jobs and it's dependents.
        It registers the Lua script and calls it.
        Even though is currently being returned, this is not strictly necessary.

        Returns:
            script (...): The Lua Script is called.
        a  
            local prefix = "{0}"
            local q = KEYS[1]
            local count = 0
            while true do
                local job_id = redis.call("lpop", q)
                if job_id == false then
                    break
                end

                -- Delete the relevant keys
                redis.call("del", prefix..job_id)
                redis.call("del", prefix..job_id..":dependents")
                count = count + 1
            end
            return count
        zutf-8)keys)rd   rJ   redis_job_namespace_prefixencoderP   register_scriptr   )rx   scripts     r6   emptyzQueue.empty   sQ      F4>><<=ffWo! 	" 008DHH:&&r5   delete_jobsc                 (   |r| j                          | j                  j                         5 }|j                  | j                  | j
                         |j                  | j
                         |j                          ddd       y# 1 sw Y   yxY w)zDeletes the queue.

        Args:
            delete_jobs (bool): If true, removes all the associated messages on the queue first.
        N)r   rP   pipelinesremrO   rn   r   execute)rx   r   r   s      r6   r   zQueue.delete  sk     JJL__%%' 	8MM$00$))<OODII&	 	 	s   ABBc                      | j                   dk(  S )z~Returns whether the current queue is empty.

        Returns:
            is_empty (bool): Whether the queue is empty
        r   r|   r~   s    r6   is_emptyzQueue.is_empty+  s     zzQr5   c                 ,    t        | j                        S )z+Returns whether the current queue is async.)boolrp   r~   s    r6   rj   zQueue.is_async3  s     DNN##r5   rA   c                     	 | j                   j                  || j                  | j                        }|j                  | j
                  k(  r|S y# t        $ r | j                  |       Y yw xY w)aH  Fetch a single job by Job ID.
        If the job key is not found, will run the `remove` method, to exclude the key.
        If the job has the same name as as the current job origin, returns the Job

        Args:
            job_id (str): The Job ID

        Returns:
            job (Optional[Job]): The job if found
        rP   rV   N)rJ   fetchrP   rV   originrg   r   remove)rx   rA   jobs      r6   	fetch_jobzQueue.fetch_job8  si    	..&&v$//VZVeVe&fC zzTYY&
  	 KK
 	 s   2A A-,A-	job_or_idc                 L   t        t        t        || j                        r|j                  n|      }| j                         dk\  r'	 | j                  j                  | j                  |      S || j                  v r| j                  j                  |      S y# t        $ r Y 5w xY w)a  Returns the position of a job within the queue

        Using Redis before 6.0.6 and redis-py before 3.5.4 has a complexity of
        worse than O(N) and should not be used for very long job queues. Redis
        and redis-py version afterwards should support the LPOS command
        handling job positions within Redis c implementation.

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

        Returns:
            _type_: _description_
        )   r   r   N)r   r[   rv   rJ   idr   rP   lposr   AttributeErrorjob_idsindex)rx   r   rA   s      r6   get_job_positionzQueue.get_job_positionM  s     c:i+P9<<V_`((*i7++DHHf==
 T\\!<<%%f-- " s   %B 	B#"B#offsetlengthc                     |}|dk\  r	||dz
  z   }n|}| j                   j                  | j                  ||      D cg c]  }t        |       }}| j                  j                  dt        | j                        t        |             |S c c}w )a  Returns a slice of job IDs in the queue.

        Args:
            offset (int, optional): The offset. Defaults to 0.
            length (int, optional): The slice length. Defaults to -1 (last element).

        Returns:
            _type_: _description_
        r   r   z$Getting jobs for queue %s: %d found.)	rP   lranger   r&   rr   debugr"   rg   re   )rx   r   r   startendrA   r   s          r6   get_job_idszQueue.get_job_idsh  s     Q;FQJ'CC151G1GRWY\1]^v76?^^=uTYY?OQTU\Q]^ _s   Bc                     | j                  ||      }t        |D cg c]  }| j                  |       c}      S c c}w )zReturns a slice of jobs in the queue.

        Args:
            offset (int, optional): The offset. Defaults to 0.
            length (int, optional): The slice length. Defaults to -1.

        Returns:
            _type_: _description_
        )r   r(   r   )rx   r   r   r   rA   s        r6   get_jobszQueue.get_jobs{  s8     ""662WE6v.EFFEs   ;c                 "    | j                         S )z+Returns a list of all job IDS in the queue.)r   r~   s    r6   r   zQueue.job_ids  s     !!r5   c                 "    | j                         S )z0Returns a list of all (valid) jobs in the queue.)r   r~   s    r6   jobsz
Queue.jobs  s     }}r5   c                 L    | j                   j                  | j                        S )z-Returns a count of all messages in the queue.)rP   llenr   r~   s    r6   r}   zQueue.count  s     ##DHH--r5   c                 L    ddl m}  || | j                  | j                        S )z'Returns this queue's FailedJobRegistry.r   )FailedJobRegistryqueuerJ   rV   )rq.registryr   rJ   rV   )rx   r   s     r6   failed_job_registryzQueue.failed_job_registry  s!     	2 tt~~RVRaRabbr5   c                 L    ddl m}  || | j                  | j                        S )z(Returns this queue's StartedJobRegistry.r   )StartedJobRegistryr   )r   r   rJ   rV   )rx   r   s     r6   started_job_registryzQueue.started_job_registry  s!     	3!SWSbSbccr5   c                 L    ddl m}  || | j                  | j                        S )z)Returns this queue's FinishedJobRegistry.r   )FinishedJobRegistryr   )r   r   rJ   rV   )rx   r   s     r6   finished_job_registryzQueue.finished_job_registry  s#     	4 #TXTcTcddr5   c                 L    ddl m}  || | j                  | j                        S )z)Returns this queue's DeferredJobRegistry.r   DeferredJobRegistryr   )r   r   rJ   rV   )rx   r   s     r6   deferred_job_registryzQueue.deferred_job_registry  !     	4"TXTcTcddr5   c                 L    ddl m}  || | j                  | j                        S )z*Returns this queue's ScheduledJobRegistry.r   ScheduledJobRegistryr   )r   r   rJ   rV   )rx   r   s     r6   scheduled_job_registryzQueue.scheduled_job_registry  s!     	5#$$..UYUdUdeer5   c                 L    ddl m}  || | j                  | j                        S )z)Returns this queue's CanceledJobRegistry.r   )CanceledJobRegistryr   )r   r   rJ   rV   )rx   r   s     r6   canceled_job_registryzQueue.canceled_job_registry  r   r5   r   r   c                     t        t        t        || j                        r|j                  n|      }||j                  | j                  d|      S | j                  j                  | j                  d|      S )a/  Removes Job from queue, accepts either a Job instance or ID.

        Args:
            job_or_id (Union[Job, str]): The Job instance or Job ID string.
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.

        Returns:
            _type_: _description_
        r   )r   r[   rv   rJ   r   lremr   rP   )rx   r   r   rA   s       r6   r   zQueue.remove  s^     c:i+P9<<V_`==1f55##DHHa88r5   c                    dj                  | j                  t        j                               }| j                  j                  | j                  |       	 | j                  j                  |      }|y| j                  j                  t        |      | j                        r&| j                  j                  | j                  |       t)zqRemoves all "dead" jobs from the queue by cycling through it,
        while guaranteeing FIFO semantics.
        z{0}_compact:{1}N)rd   rN   uuiduuid4rP   renamer   lpoprJ   existsr&   rpush)rx   COMPACT_QUEUErA   s      r6   r(   zQueue.compact  s     *001R1RTXT^T^T`atxx7__))-8F~~~$$WV_dooF%%dhh7 r5   rB   c                 j   ||n| j                   }|r|j                  n|j                  } || j                  |      }|;| j                  j                  dt        |      t        | j                        |       y| j                  j                  dt        |      t        | j                               y)a  Pushes a job ID on the corresponding Redis queue.
        'at_front' allows you to push the job onto the front instead of the back of the queue

        Args:
            job_id (str): The Job ID
            pipeline (Optional[Pipeline], optional): The Redis Pipeline to use. Defaults to None.
            at_front (bool, optional): Whether to push the job to front of the queue. Defaults to False.
        Nz.Pushed job %s into %s, %s job(s) are in queue.zPushed job %s into %s)	rP   lpushr   r   rr   r   r!   r"   rg   )rx   rA   r   rB   rP   pushresults          r6   push_job_idzQueue.push_job_id  s     "*!5X4??
#+z1A1Adhh'HHNNKTRX\[`aeajaj[kmst HHNN2DL%		BRSr5   )rE   rF   rG   group_idr8   r$   r9   r:   r;   r<   r=   r>   r?   r@   r%   rC   statusrD   r   rE   rF   rG   r   c                   t        |      }|| j                  }n|dk(  rt        d      t        |      }t        |      }t        |      }||dk  rt        d      | j                  j	                  |||| j
                  ||||||	||
| j                  || j                  ||||      }|r"|j                  |_	        |j                  |_        |S )a6  Creates a job based on parameters given

        Args:
            func (FunctionReferenceType): The function referce: a callable or the path.
            args (Union[Tuple, List, None], optional): The `*args` to pass to the function. Defaults to None.
            kwargs (Optional[Dict], optional): The `**kwargs` to pass to the function. Defaults to None.
            timeout (Optional[int], optional): Function timeout. Defaults to None, use -1 for infinite timeout.
            result_ttl (Optional[int], optional): Result time to live. Defaults to None.
            ttl (Optional[int], optional): Time to live. Defaults to None.
            failure_ttl (Optional[int], optional): Failure time to live. Defaults to None.
            description (Optional[str], optional): The description. Defaults to None.
            depends_on (Optional[JobDependencyType], optional): The job dependencies. Defaults to None.
            job_id (Optional[str], optional): Job ID. Defaults to None.
            meta (Optional[Dict], optional): Job metadata. Defaults to None.
            status (JobStatus, optional): Job status. Defaults to JobStatus.QUEUED.
            retry (Optional[Retry], optional): The Retry Object. Defaults to None.
            on_success (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on success. Defaults to
                None. Callable is deprecated.
            on_failure (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on failure. Defaults to
                None. Callable is deprecated.
            on_stopped (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on stopped. Defaults to
                None. Callable is deprecated.
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            group_id (Optional[str], optional): A group ID that the job is being added to. Defaults to None.

        Raises:
            ValueError: If the timeout is 0
            ValueError: If the job TTL is 0 or negative

        Returns:
            Job: The created job
        r   z50 timeout is not allowed. Use -1 for infinite timeoutzJob ttl must be greater than 0)r9   r:   rP   r<   r=   r>   r   r?   r@   r;   r   r   rC   rV   rE   rF   rG   r   )r,   ro   rc   rJ   createrP   rg   rV   maxretries_left	intervalsretry_intervals)rx   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rC   r   rD   rE   rF   rG   r   r   s                      r6   
create_jobzQueue.create_job  s    j  (?++G\TUU":.
#K0C ?sax=>>nn##!##!99!!!' $ 
, $yyC"'//C
r5   r   c                    t        |j                        dkD  r|j                  d      }||n| j                  j	                         }	 	 |j                  |j                         |j                  d|      }|j                          |D ]  }|j                  d      t        j                  k7  s'|j                  t        j                  |       |j                  |       |j                  |       |j                  |j                   |       ||j#                          |c S  	 |S ||j                          |S # t$        $ r |
||_        Y  w xY w)ac  If a _dependent_ job depends on any unfinished job, register all the
        _dependent_ job's dependencies instead of enqueueing it.

        `Job#fetch_dependencies` sets WATCH on all dependencies. If
        WatchError is raised in the when the pipeline is executed, that means
        something else has modified either the set of dependencies or the
        status of one of them. In this case, we simply retry.

        Args:
            job (Job): The job
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.

        Returns:
            job (Job): The Job
        r   FrefreshT)watchr   r   r=   r   )re   _dependency_ids
get_statusrP   r   r  dependencies_keyfetch_dependenciesmultir    FINISHED
set_statusDEFERREDregister_dependencysavecleanupr=   r   r   _status)rx   r   r   orig_statuspipedependencies
dependencys          r6   setup_dependencieszQueue.setup_dependenciesR  sO     s""#a'...7K'389Q9Q9SD  JJs334#&#9#9t#9#TLJJL&2 
'
%000?9CUCUU  NN9+=+=NM33T3BHHdH3KKCGGdKC'/ $#&J
'  
 !NN
 " 
'
 '2  
s    A&E 3A7E +E EE.c                     | j                  ||||||||	|
|t        j                  |||||      }| j                  |||      S )a/  Creates a job to represent the delayed function call and enqueues it.

        It is much like `.enqueue()`, except that it takes the function's args
        and kwargs as explicit arguments.  Any kwargs passed to this function
        contain options for RQ itself.

        Args:
            func (FunctionReferenceType): The reference to the function
            args (Union[Tuple, List, None], optional): THe `*args` to pass to the function. Defaults to None.
            kwargs (Optional[Dict], optional): THe `**kwargs` to pass to the function. Defaults to None.
            timeout (Optional[int], optional): Function timeout. Defaults to None.
            result_ttl (Optional[int], optional): Result time to live. Defaults to None.
            ttl (Optional[int], optional): Time to live. Defaults to None.
            failure_ttl (Optional[int], optional): Failure time to live. Defaults to None.
            description (Optional[str], optional): The job description. Defaults to None.
            depends_on (Optional[JobDependencyType], optional): The job dependencies. Defaults to None.
            job_id (Optional[str], optional): The job ID. Defaults to None.
            at_front (bool, optional): Whether to enqueue the job at the front. Defaults to False.
            meta (Optional[Dict], optional): Metadata to attach to the job. Defaults to None.
            retry (Optional[Retry], optional): Retry object. Defaults to None.
            on_success (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on success. Defaults to
                None. Callable is deprecated.
            on_failure (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on failure. Defaults to
                None. Callable is deprecated.
            on_stopped (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on stopped. Defaults to
                None. Callable is deprecated.
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.

        Returns:
            Job: The enqueued Job
        )r9   r:   r<   r=   r>   r?   r@   rA   rC   r   r;   rD   rE   rF   rG   r   rB   )r   r    QUEUEDenqueue_job)rx   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   r   s                      r6   enqueue_callzQueue.enqueue_call  sh    h oo!##!##!!!!  
$ hJJr5   c                 6    t        | |||||||||	|
|||||      S )am  Need this till support dropped for python_version < 3.7, where defaults can be specified for named tuples
        And can keep this logic within EnqueueData

        Args:
            func (FunctionReferenceType): The reference to the function
            args (Union[Tuple, List, None], optional): THe `*args` to pass to the function. Defaults to None.
            kwargs (Optional[Dict], optional): THe `**kwargs` to pass to the function. Defaults to None.
            timeout (Optional[int], optional): Function timeout. Defaults to None.
            result_ttl (Optional[int], optional): Result time to live. Defaults to None.
            ttl (Optional[int], optional): Time to live. Defaults to None.
            failure_ttl (Optional[int], optional): Failure time to live. Defaults to None.
            description (Optional[str], optional): The job description. Defaults to None.
            depends_on (Optional[JobDependencyType], optional): The job dependencies. Defaults to None.
            job_id (Optional[str], optional): The job ID. Defaults to None.
            at_front (bool, optional): Whether to enqueue the job at the front. Defaults to False.
            meta (Optional[Dict], optional): Metadata to attach to the job. Defaults to None.
            retry (Optional[Retry], optional): Retry object. Defaults to None.
            on_success (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on success. Defaults to
                None. Callable is deprecated.
            on_failure (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on failure. Defaults to
                None. Callable is deprecated.
            on_stopped (Optional[Union[Callback, Callable[..., Any]]], optional): Callback for on stopped. Defaults to
                None. Callable is deprecated.

        Returns:
            EnqueueData: The EnqueueData
        )r.   r7   s                   r6   prepare_datazQueue.prepare_data  sB    \ !
 	
r5   	job_datasr.   c                 @   ||n| j                   j                         }g }g }g }fd}|D 	cg c]  }	|	j                  r|	 }
}	|
rb|
D 	cg c]E  }	| j                   | j                  di  ||	t
        j                        ||	j                        G }}	||j                          |D 	cg c]  }	|	j                  s|	 }}	|r|D 	cg c])  }	 | j                  di  ||	t
        j                        + }}	|D ]  }|j                  |        ||j                          t        j                  ||      \  }}|D cg c]   }| j                  |||j                        " }}||j                          ||z   |z   S c c}	w c c}	w c c}	w c c}	w c c}w )au  Creates multiple jobs (created via `Queue.prepare_data` calls)
        to represent the delayed function calls and enqueues them.

        Args:
            job_datas (List['EnqueueData']): A List of job data
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.

        Returns:
            List[Job]: A list of enqueued jobs
        c                    i d| j                   d| j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d	| j                  d
| j                  d|d| j                  d| j                  d| j                  d| j                  d| j                  dS )Nr8   r9   r:   r<   r=   r>   r?   r@   rA   rC   r   r;   rD   rE   rF   rG   r   )r8   r9   r:   r<   r=   r>   r?   r@   rA   rC   r;   rD   rE   rF   rG   )job_datainitial_statusr   s     r6   get_job_kwargsz*Queue.enqueue_many.<locals>.get_job_kwargs&  s!    (// h11	
 x|| x33 x33 h11 (//  . 8++  h11 h11  h11!" H# r5   r  r  r/   )rP   r   r@   _enqueue_jobr   r    r  rB   r   r  r  r   get_jobs_with_met_dependenciesenqueue_at_front)rx   r  r   r   r  jobs_without_dependenciesjobs_with_unmet_dependenciesjobs_with_met_dependenciesr$  r"  job_datas_without_dependenciesjob_datas_with_dependenciesjobs_with_dependenciesr   s      `          r6   enqueue_manyzQueue.enqueue_many  s    $/xT__5M5M5O$&!')$%'"	, DM)hxT\TgTg()h&)h) !?)  !!#DOOQnXy?O?O&PQ!%.. " )% ) @I&aHXM`M`x&a#&a& !<&  O.9;M;M"NO&" & . ($'( HRGpGp&HD&(D
 6* !!#s?S?S!T*& * (+GGJdddI *i) 'b&*s*   FFA
F/FF.F%Fc                 <   |j                          t               |_        |j                  t              }| j
                  j                         5 }|j                  ||       |j                  ||       |j                          ddd       |S # 1 sw Y   |S xY w)zzRun the job

        Args:
            job (Job): The job to run

        Returns:
            Job: _description_
        )default_ttl)r<   r   r  N)
performr+   ended_atget_result_ttlr   rP   r   _handle_successr  r   )rx   r   r<   r   s       r6   run_jobzQueue.run_jobb  s     	u''4F'G
__%%' 	8:IKK
XK6	 
		 
s   7BBc                    t        |t              s|j                  dk(  rt        d      |j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  d	d      }	|j	                  d
d      }
|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }|j	                  dd      }d|v sd|v r0|dk(  sJ d       |j	                  dd      }|j	                  dd      }|||||||	|
|||||||||fS )a  
        Parses arguments passed to `queue.enqueue()` and `queue.enqueue_at()`

        The function argument `f` may be any of the following:

        * A reference to a function
        * A reference to an object's instance method
        * A string, representing the location of a function (must be
          meaningful to the import context of the workers)

        Args:
            f (FunctionReferenceType): The function reference
            args (*args): function args
            kwargs (*kwargs): function kargs
        __main__zAFunctions from the __main__ module cannot be processed by workersjob_timeoutNr?   r<   r=   r>   r@   rA   rB   FrC   rD   rE   rF   rG   r   r9   r:   r/   zMExtra positional arguments cannot be used when using explicit args and kwargs)rv   r[   r1   rc   pop)rX   fr9   r:   r;   r?   r<   r=   r>   r@   rA   rB   rC   rD   rE   rF   rG   r   s                     r6   
parse_argszQueue.parse_argst  s   " !S!allj&@`aa **]D1jj5ZZd3
jj%jj5ZZd3
Hd+::j%0zz&$'

7D)ZZd3
ZZd3
ZZd3
::j$/Vx612:nnn:::fd+DZZ$/F #
 	
r5   c                     t        j                  |g|i |\  }}}}}}}	}
}}}}}}}}}| j                  |||||||||	|
|||||||      S )au  Creates a job to represent the delayed function call and enqueues it.
        Receives the same parameters accepted by the `enqueue_call` method.

        Args:
            f (FunctionReferenceType): The function reference
            args (*args): function args
            kwargs (*kwargs): function kargs

        Returns:
            job (Job): The created Job
        )r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   )rI   r;  r  )rx   r:  r9   r:   r;   r?   r<   r=   r>   r@   rA   rB   rC   rD   rE   rF   rG   r   s                     r6   enqueuezQueue.enqueue  s    < Q000%	
   !##!!!!# ! 
 	
r5   r   c                     t        j                  |g|i |\  }}}}}}	}
}}}}}}}}}}| j                  |t        j                  ||||||	||
||||||      }|rd|_        | j                  |||      S )zSchedules a job to be enqueued at specified time

        Args:
            datetime (datetime): _description_
            f (_type_): _description_

        Returns:
            _type_: _description_
        )r   r9   r:   r;   r<   r=   r>   r?   r@   rA   rC   rD   rE   rF   rG   Tr  )rI   r;  r   r    	SCHEDULEDr'  schedule_job)rx   r   r:  r9   r:   r;   r?   r<   r=   r>   r@   rA   rB   rC   rD   rE   rF   rG   r   r   s                       r6   
enqueue_atzQueue.enqueue_at  s    8 Q000%	
oo&&!##!!!!!  
$ #'C   h BBr5   c                    ddl m}  ||       }||n| j                  j                         }|j	                  | j
                  | j                         |j                  |       |j                  |||       ||j                          |S )a  Puts job on ScheduledJobRegistry

        Args:
            job (Job): _description_
            datetime (datetime): _description_
            pipeline (Optional[Pipeline], optional): _description_. Defaults to None.

        Returns:
            _type_: _description_
        r   r   )r   r  )
registryr   rP   r   saddrO   r   r  scheduler   )rx   r   r   r   r   rC  r  s          r6   r@  zQueue.schedule_job  s{     	3'd3#/xT__5M5M5O 			$(($((3$#x$7LLN
r5   
time_deltac                 x     | j                   t        j                  t        j                        |z   |g|i |S )a  Schedules a job to be executed in a given `timedelta` object

        Args:
            time_delta (timedelta): The timedelta object
            func (FunctionReferenceType): The function reference

        Returns:
            job (Job): The enqueued Job
        )rA  r   r+   r   utc)rx   rF  r8   r9   r:   s        r6   
enqueue_inzQueue.enqueue_in2  s4     tx||HLL9JF^t^W]^^r5   c                 h   | j                   |_        | j                  ||      }||n| j                  j	                         }|j                  | j                  | j                         ||j                          |j                  d      t        j                  k7  r| j                  |||      S |S )a}  Enqueues a job for delayed execution checking dependencies.

        Args:
            job (Job): The job to enqueue
            pipeline (Optional[Pipeline], optional): The Redis pipeline to use. Defaults to None.
            at_front (bool, optional): Whether should enqueue at the front of the queue. Defaults to False.

        Returns:
            Job: The enqued job
        r  Fr  r  )rg   r   r  rP   r   rD  rO   r   r   r  r    r  r%  rx   r   r   rB   r  s        r6   r  zQueue.enqueue_job>  s     YY
%%cH%=#/xT__5M5M5O		$(($((3LLN>>%>(I,>,>>$$S8h$OO
r5   c                 8   ||n| j                   j                         }| j                         |_        |j	                  t
        j                  |       | j                  |_        t               |_
        |j                  | j                  |_        |j                  |       |j                  |j                  |       | j                   r| j#                  |j$                  ||       ||j'                          | j                   s| j)                  |      }|S )a  Enqueues a job for delayed execution without checking dependencies.

        If Queue is instantiated with is_async=False, job is executed immediately.

        Args:
            job (Job): The job to enqueue
            pipeline (Optional[Pipeline], optional): The Redis pipeline to use. Defaults to None.
            at_front (bool, optional): Whether should enqueue at the front of the queue. Defaults to False.

        Returns:
            Job: The enqued job
        r  r  r  )rP   r   r   rw   r  r    r  rg   r   r+   enqueued_atr;   ro   r  r  r=   rp   r   r   r   run_syncrK  s        r6   r%  zQueue._enqueue_jobU  s     $/xT__5M5M5O#'#@#@#B y''$7YY
%;;//CK$$/>>SVVdXFLLN~~--$C
r5   c                    | j                   j                         5 }|j                  d|       ddd       	 | j                  |      }|j                  r+|j	                  || j                   |j                                |S # 1 sw Y   TxY w#  | j                   j                         5 }|j                  t        j                  |       dj                  t        j                  t        j                                }|j                  ||       |j                          ddd       n# 1 sw Y   nxY w|j                   r0 |j                   || j                   gt        j                           Y |S xY w)zRun a job synchronously, meaning on the same process the method was called.

        Args:
            job (Job): The job to run

        Returns:
            Job: The job instance
        syncNr   )rP   r   prepare_for_executionr5  success_callbackreturn_valuer  r    FAILEDjoin	tracebackformat_exceptionsysexc_info_handle_failurer   failure_callback)rx   r   r   
exc_strings       r6   rN  zQueue.run_syncz  s1    __%%' 	88%%fh7	8	O,,s#C ##$$S$//3;K;K;MN
%	8 	8
	L))+ #xy//(CWWY%?%?%PQ
##J9  "	# # # ##$$$S$//KCLLNK
 
s0   BB B
E4)A7D) 	E4)D2	.AE4exclude_job_idc                    ddl m} ||n| j                  j                         }|j                  }	 	 ||j                  |       |j                  |      D ch c]  }t        |       }}|sy| j                  j                  || j                  | j                        D 	cg c]=  }	|	r9|	j                  ||      r%|	j                  d      t        j                  k7  r|	? }
}	|j                          |
sy|
D ]  }|j                   xs d} ||j"                  | j                  | j                  | j                        }|j%                  ||	       |j"                  | j&                  k(  r| j)                  |||
       | j+                  |j"                  | j                        }|j)                  |||
        t-        |
      t-        |      k(  r|j/                  |       n,|
D cg c]  }|j0                   }} |j2                  |g|  ||j5                          yc c}w c c}	w c c}w # t6        $ r |Y  w xY w)a  Enqueues all jobs in the given job's dependents set and clears it.

        When called without a pipeline, this method uses WATCH/MULTI/EXEC.
        If you pass a pipeline, only MULTI is called. The rest is up to the
        caller.

        Args:
            job (Job): The Job to enqueue the dependents
            pipeline (Optional[Pipeline], optional): The Redis Pipeline. Defaults to None.
            exclude_job_id (Optional[str], optional): Whether to exclude the job id. Defaults to None.
        r   r   Nr   )
parent_jobr   r^  Fr  )rJ   rV   r  r  )rg   rP   )rC  r   rP   r   dependents_keyr  r\   r&   rJ   
fetch_manyrV   dependencies_are_metr  r    CANCELEDr  r'  r   r   rg   r%  	__class__re   r   r   r   r   r   )rx   r   r   r^  r   r  ra  _iddependent_job_idsdependent_jobjobs_to_enqueue	dependentr'  rC  r   enqueued_job_idss                   r6   enqueue_dependentszQueue.enqueue_dependents  sP    	2#/xT__5M5M5O++> #JJ~.=A]]>=Z$[cWS\$[!$[ ) *.)B)B)dooRVRaRa *C *#% %%::#&!%'5 ; 
 &000?9CUCUU "# # 

&!0 `I'0'A'A'JU$2!(($//T^^`d`o`o H OOIO= ''4994)))dM])^ $I4D4DQUQ`Q` a**9tN^*_` '3/@+AAKK/:I'J3'J$'JDIIn@/?@#LLNa %\#J (K  #
 sI   &H9 H*+H9 04H9 $AH/&H9 ;C4H9 /H4'H9 *H9 9I	I	c                 ^    t        | j                  j                  | j                              S )zjPops a given job ID from this Redis queue.

        Returns:
            job_id (str): The job id
        )r&   rP   r   r   r~   s    r6   
pop_job_idzQueue.pop_job_id  s"     t++DHH566r5   
queue_keysc                    ||dk(  rt        d      dj                  t        t        |D cg c]  }t	        t        |             c}            }t
        j                  d| d|        |sJ |j                  ||      }|$t
        j                  d|        t        ||      |\  }}||fS |D ]  }|sJ |j                  |      }	|	||	fc S  yc c}w )a  Helper method to abstract away from some Redis API details
        where LPOP accepts only a single key, whereas BLPOP
        accepts multiple.  So if we want the non-blocking LPOP, we need to
        iterate over all queues, do individual LPOPs, and return the result.

        Until Redis receives a specific method for this, we'll have to wrap it
        this way.

        The timeout parameter is interpreted as follows:
            None - non-blocking (return immediately)
             > 0 - maximum number of seconds to block

        Args:
            queue_keys (_type_): _description_
            timeout (Optional[int]): _description_
            connection (Optional[Redis], optional): _description_. Defaults to None.

        Raises:
            ValueError: If timeout of 0 was passed
            DequeueTimeout: BLPOP Timeout

        Returns:
            _type_: _description_
        Nr   HRQ does not support indefinite timeouts. Please pick a timeout value > 0z, z$Starting BLPOP operation for queues  with timeout of z'BLPOP timeout, no jobs found on queues )
rc   rV  mapr[   r"   rq   r   blpopr   r   )
rX   ro  r;   rP   r   colored_queuesr   rS   rA   blobs
             r6   r   z
Queue.lpop  s    4 !| !kll!YYs3PZ0[us5z1B0['\]NLL??OO`ah`ijk:%%j':F~F~FVWX$Wj99 &Ivf$$' +	!!z!y1#$d?*	+
  1\s   Cc                    t        ||      }|}|dk(  rt        d      t        |      }t        j	                  d| d|        |j                  ||j                  |      }|$t        j	                  d|        t        ||      ||fS t        t        t           |j                  ||j                              }|||fS y)zSimilar to lpop, but accepts only a single queue key and immediately pushes
        the result to an intermediate queue.
        Nr   rq  zStarting BLMOVE operation for rr  z!BLMOVE timeout, no jobs found on )r   rc   r"   rq   r   blmover   r   r   r   r	   lmove)rX   rP   rS   r;   r   colored_queuer   s          r6   ry  zQueue.lmove!  s    
 /y*E!| !kll!),MLL9-HYZaYbcd$.$5$5iASAWAWY`$aF~@PQ$Wi88f$$(3-)9)9)EWE[E[)\]F! &((r5   queues)r   rI   c                    t        | d|      }	 |D cg c]  }|j                   }	}t        |	      dk(  r%t        |      dk\  r| j	                  ||	d   |      }
n| j                  |	||      }
|
yt        t        |
      \  }}| j                  |||||      }	 |j                  |||	      }||fS c c}w # t        $ r Y t        $ r}||_        ||_        |d}~ww xY w)
aO  Class method returning the job_class instance at the front of the given
        set of Queues, where the order of the queues is important.

        When all of the Queues are empty, depending on the `timeout` argument,
        either blocks execution of this function for the duration of the
        timeout or until new messages arrive on any of the queues, or returns
        None.

        See the documentation of cls.lpop for the interpretation of timeout.

        Args:
            queues (List[Queue]): List of queue objects
            timeout (Optional[int]): Timeout for the LPOP
            connection (Optional[Redis], optional): Redis Connection. Defaults to None.
            job_class (Optional[Type[Job]], optional): The job class. Defaults to None.
            serializer (Any, optional): Serializer to use. Defaults to None.
            death_penalty_class (Optional[Type[BaseDeathPenalty]], optional): The death penalty class. Defaults to None.

        Raises:
            e: Any exception

        Returns:
            job, queue (Tuple[Job, Queue]): A tuple of Job, Queue
        rJ   )overrider   )r      r   r   r   NrU   r   )r'   r   re   r)   ry  r   rs  r&   rW   r   r   	ExceptionrA   r   )rX   r{  r;   rP   rJ   rV   rK   job_clsqro  r   rS   rA   r   r   es                   r6   dequeue_anyzQueue.dequeue_any7  s
   D +3iP)/0A!%%0J0:!#J(?9(L:z!}gF*g*M~ #GV 4Iv&&%!%$7 ' EmmFzjmY :7 1" "    "s#   B5B: :	C"C"CC"c                 j    t        |t              st        d      | j                  |j                  k(  S Nz&Cannot compare queues to other objectsrv   rI   rm   rg   rx   others     r6   __eq__zQueue.__eq__{  s+    %'DEEyyEJJ&&r5   c                 j    t        |t              st        d      | j                  |j                  k  S r  r  r  s     r6   __lt__zQueue.__lt__  s+    %'DEEyy5::%%r5   c                 ,    t        | j                        S r{   )hashrg   r~   s    r6   __hash__zQueue.__hash__  s    DIIr5   c                 b    dj                  | j                  j                  | j                        S )Nz
{0}({1!r})rd   re  r0   rg   r~   s    r6   __repr__zQueue.__repr__  s#    ""4>>#:#:DIIFFr5   c                 b    dj                  | j                  j                  | j                        S )Nz	<{0} {1}>r  r~   s    r6   __str__zQueue.__str__  s#    !!$.."9"9499EEr5   )NNN)T)r   r{   )NF)NNNNNNNNNFNNNNNN)NNNNNNNNNFNNNNN)NN)r:  r$   )r:  r$   rQ   r   )r   r   rQ   r   )[r0   r1   r2   r   rJ   r   __annotations__r   rK   r   rM   r   rN   r[   rO   classmethodr   r   r`   r	   rW   r   r   ry   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   rj   r   r   r   r   r   r   r}   r   r   r   r   r   r   r   r(   r   r    r  r   r   r
   r   r  r  staticmethodr.   r  r.  r5  r;  r=  r   rA  r@  r   rI  r  r%  rN  rl  rn  r   ry  r  r  r  r  r  r  r/   r5   r6   rI   rI   @   s    ItE{ 2H./HOS(3 #3(s( ,0@D DK(
 &d+;&<= 
g @ 
 ,0@D#
#
 #
 DK(	#

 #
 &d+;&<=#
 
#
 #
N (,)-7;@V0I0I W%0I "#	0I
 0I E#tE{"2340I 0I &d+;&<=0Id)%S#*> )   G G G$5 G G 4 4 >x} > >
$ 
;'<$ $  $$ $ $  *%s
*;  6# 3 S	 &Gs G GT%[ G "c " " d5k   .s . . c c d d e e e e f f e e9eSj 1 9Xj=Q 9"8T# T*1E TX\ T* *.!%!%$(!%)%)48 $#%,,#']  ;?:>:>"&']%] E4%&] 	]
 #] SM] c]] c]] c]] 01] ] tn] ]  ]  U8X#567!]" U8X#567#]$ U8X#567%]& 3-']( 
)]~7e 7x
7K 7W\ 7x *.!%!%$(!%)%)48 $##'DHDHDH)-%FK%FK E4%&FK 	FK
 #FK SMFK c]FK c]FK c]FK 01FK FK FK tnFK  FK U8Xc3h-?#?@AFK  U8Xc3h-?#?@A!FK" U8Xc3h-?#?@A#FK$ :&%FK& 
'FKP  *.!%!%$(!%)%)%) $##':>:>:>!>
%>
E4%&>
 >
 #	>

 SM>
 c]>
 c]>
 c]>
 TN>
 >
 >
 tn>
  >
 U8X#567>
 U8X#567>
  U8X#567!>
" 
#>
 >
B ptLem,Le8@8LLe_ghk_lLe	cLe\5 S $ ;
 ;
z2
h1C8 1Cf  XjEY 2
_Y 
_6M 
_ch 
_u 0D W[ hk .# #*1E #X\ #il #J< bfRR$,Z$8RQYZ]Q^Rh7HSM 7 +d3i +(3- +XV]M^ + +Z w 3 #  *  ,0@D?W? #? 	?
 DK(? ? &d+;&<=? 
%'	(? ?F'
&
GFr5   rI   )AloggingrY  rW  r   rs   collectionsr   r   r   r   	functoolsr   typingr   r	   r
   r   r   r   r   r   r   r   redisr   timeoutsr   r   r   redis.clientr   r   r   defaultsr   r  r   
exceptionsr   r   r   r   r   r   r    logutilsr!   r"   serializersr#   typesr$   r%   utilsr&   r'   r(   r)   r*   r+   r,   	getLoggerrq   r.   rI   r/   r5   r6   <module>r     s     
    " 2 2 $ _ _ _  >% ( " 6 1 ) ) ! + ; e e e			:	&	
< KF KF KFr5   