
    s,g^                         d dl m Z mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZ erd dlmZ ddlmZ ddlmZmZ dd	lmZmZmZ  G d
 d      Z G d de      Zy)    )datetimetimezone)TYPE_CHECKINGAnyDictListOptional)uuid4)Redis)Pipeline   )Job)BaseRegistryStartedJobRegistry)as_textcurrent_timestampnowc            	           e Zd ZdZdededefdZdedefdZ	e
defd	       Ze
defd
       Ze
d        Zededededd fd       Zd Zedededd fd       Zedededddd fd       Zddeded   fdZdeddfdZdefdZdededdfdZy)	Executionz)Class to represent an execution of a job.idjob_id
connectionc                 l    || _         || _        || _        t               }|| _        || _        d | _        y N)r   r   r   r   
created_atlast_heartbeat_job)selfr   r   r   	right_nows        g/var/www/trellinator.diamondhoofcare.com/public_html/venv/lib/python3.12/site-packages/rq/executions.py__init__zExecution.__init__   s4    $E	#'#'	    otherreturnc                 V    t        |t              sy| j                  |j                  k(  S )NF)
isinstancer   r   )r   r#   s     r    __eq__zExecution.__eq__   s"    %+ww%((""r"   c                      d| j                    S )Nzrq:execution:)composite_keyr   s    r    keyzExecution.key!   s    t11233r"   c                     | j                   r| j                   S t        j                  | j                  | j                        | _         | j                   S )N)r   r   )r   r   fetchr   r   r*   s    r    jobzExecution.job%   s7    9999III	yyr"   c                 8    | j                    d| j                   S )N:)r   r   r*   s    r    r)   zExecution.composite_key,   s    ++ay))r"   c                 <     | |||      }|j                          |S )zFetch an execution from Redis.r   r   r   )refresh)clsr   r   r   	executions        r    r-   zExecution.fetch0   s$     2fD	r"   c                 \   | j                   j                  | j                        }|st        d| j                   d      t        j                  t        |d         t        j                        | _
        t        j                  t        |d         t        j                        | _        y)z"Refresh execution data from Redis.z
Execution z not found in Rediss
   created_at)tzs   last_heartbeatN)r   hgetallr+   
ValueErrorr   r   fromtimestampfloatr   utcr   r   )r   datas     r    r3   zExecution.refresh7   s    &&txx0z$''2EFGG"00tM7J1KPXP\P\]&44U4@Q;R5SX`XdXder"   r)   c                 @    |j                  d      \  }} | |||      S )z>A combination of job_id and execution_id separated by a colon.r0   r2   )split)r4   r)   r   r   r   s        r    from_composite_keyzExecution.from_composite_key?   s'     #((-
bJ??r"   r.   ttlpipeliner   c                 "   t               j                  } | ||j                  |j                        }|j	                  ||       t        |j                  |      j                  |||       |j                  j                  |||d       |S )zSave execution data to Redis.r2   )rA   rB   r   r   r5   rA   rB   F)rB   rA   xx)	r
   hexr   r   saveExecutionRegistryaddstarted_job_registryadd_execution)r4   r.   rA   rB   r   r5   s         r    createzExecution.createE   s|     W[[2cffH	32H=AAI[^iqAr  ..y8QTY^._r"   Nc                     ||n| j                   }|j                  | j                  | j                                |j	                  | j                  |       y)z6Save execution data to Redis and JobExecutionRegistry.N)mapping)r   hsetr+   	serializeexpire)r   rA   rB   r   s       r    rH   zExecution.saveO   sA    !)!5X4??
$..*:;$((C(r"   c                     |j                  | j                         |j                  j                  | ||       t	        | j
                  | j                        j                  | |       y)zDelete an execution from Redis.)r5   r.   rB   rD   )r5   rB   N)deleter+   rK   remove_executionrI   r   r   remove)r   r.   rB   s      r    rT   zExecution.deleteV   sR    !  11DcT\1]IPP[_jrPsr"   c                     | j                   | j                  j                         | j                  j                         dS )N)r   r   r   )r   r   	timestampr   r*   s    r    rQ   zExecution.serialize\   s5    ''//335"11;;=
 	
r"   rK   c                 @   t               | _        |j                  | j                  d| j                  j	                                |j                  | j                  |       |j                  | ||d       t        | j                  |      j                  | ||       y)zUpdate execution heartbeat.r   T)rA   rB   rF   rD   rE   N)
r   r   rP   r+   rX   rR   rL   rI   r   rJ   )r   rK   rA   rB   s       r    	heartbeatzExecution.heartbeatc   s     "edhh 0$2E2E2O2O2QR#&**4S8PT*UBFFQU[^iqFrr"   r   )__name__
__module____qualname____doc__strr   r!   objectboolr'   propertyr+   r   r.   r)   classmethodr-   r3   r@   intrM   r	   rH   rT   r   rQ   r   rZ    r"   r    r   r      sj   3(3 ( ( (#F #t #
 4S 4 4 S   * * s C U {  f @s @ @+ @ @
  3 *   ) )x
'; )t# t t
4 
s.@ ss sV` sr"   r   c                       e Zd ZdZdZdedefdZddee	   fdZ
d	ed
edddefdZd	edddefdZddededee   fdZddededee   fdZdeddfdZy)rI   zbClass to represent a registry of job executions.
    Each job has its own execution registry.
    zrq:executions:{0}r   r   c                 `    || _         || _        | j                  j                  |      | _        y r   )r   r   key_templateformatr+   )r   r   r   s      r    r!   zExecutionRegistry.__init__t   s(    $$$++F3r"   NrX   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   zremrangebyscorer+   )r   rX   scores      r    cleanupzExecutionRegistry.cleanupy   s.     '2	8I8K((1e<r"   r5   rA   rB   r   r$   c                     t               |z   }|j                  | j                  |j                  |dz   i       |j	                  | j                  |dz          y)a  Register an execution to 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.

        Returns:
            result (int): The ZADD command result
        <   N)r   zaddr+   r   rR   )r   r5   rA   rB   rl   s        r    rJ   zExecutionRegistry.add   sI     "#c)dhhurz :;#(+r"   c                 N    |j                  | j                  |j                        S )z"Remove an execution from registry.)zremr+   r   )r   r5   rB   s      r    rV   zExecutionRegistry.remove   s    }}TXXy||44r"   startendc                     | j                          | j                  j                  | j                  ||      D cg c]  }t	        |       c}S c c}w )&Returns all executions IDs in registry)rm   r   zranger+   r   )r   rs   rt   r   s       r    get_execution_idsz#ExecutionRegistry.get_execution_ids   s;    .2oo.D.DTXXuVY.Z[F[[[s   Ac                     | j                  ||      }g }|D ]=  }|j                  t        j                  || j                  | j
                               ? |S )rv   r2   )rx   appendr   r-   r   r   )r   rs   rt   execution_ids
executionsexecution_ids         r    get_executionsz ExecutionRegistry.get_executions   sX    ..uc:
) 	pLioodkk^b^m^mono	pr"   r.   c                     | j                         }|D ]  }|j                  ||        |j                  | j                         y)zDelete the registry.)rB   r.   N)r~   rT   r+   )r   r.   rB   r|   r5   s        r    rT   zExecutionRegistry.delete   sB    ((*
# 	9IhC8	9!r"   r   )r   )r[   r\   r]   r^   rh   r_   r   r!   r	   r;   rm   r   rd   r   rJ   rV   r   rx   r~   r   rT   re   r"   r    rI   rI   m   s     'L4s 4 4
=% =Y S J 3 "5	 5Z 5C 5\s \S \$s) \
C # tI "# " "r"   rI   N)r   r   typingr   r   r   r   r	   uuidr
   redisr   redis.clientr   r.   r   registryr   r   utilsr   r   r   r   rI   re   r"   r    <module>r      sA    ' ; ;  %  6 2 2Zs Zsz>" >"r"   