
    ]j                         d dl m Z mZ d dl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mZ  G d
 d      Z G d de      Zy)    )datetimetimezone)TYPE_CHECKINGAnyOptional)uuid4)Redis)Pipeline   )Job)BaseRegistryStartedJobRegistry)as_textcurrent_timestampnowparse_composite_keyc            	           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        7/root/env/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   r2   zExecution.refresh7   s    &&txx0z$''2EFGG"00tM7J1KPXP\P\]&44U4@Q;R5SX`XdXder!   r(   c                 4    t        |      \  }} | |||      S )z>A combination of job_id and execution_id separated by a colon.r1   )r   )r3   r(   r   r   execution_ids        r   from_composite_keyzExecution.from_composite_key?   s"      3=Al6jIIr!   r-   ttlpipeliner
   c                 "   t               j                  } | ||j                  |j                        }|j	                  ||       t        |j                  |      j                  |||       |j                  j                  |||d       |S )zSave execution data to Redis.r1   )r@   rA   r   r   r4   r@   rA   F)rA   r@   xx)	r   hexr   r   saveExecutionRegistryaddstarted_job_registryadd_execution)r3   r-   r@   rA   r   r4   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   r@   rA   r   s       r   rG   zExecution.saveO   sA    !)!5X4??
$..*:;$((C(r!   c                     |j                  | j                         |j                  j                  | |       t	        | j
                  | j                        j                  | |       y)zDelete an execution from Redis.)r4   rA   rC   N)deleter*   rJ   remove_executionrH   r   r   remove)r   r-   rA   s      r   rS   zExecution.deleteV   sO    !  11D81TIPP[_jrPsr!   c                     | j                   | j                  j                         | j                  j                         dS )N)r   r   r   )r   r   	timestampr   r)   s    r   rP   zExecution.serialize\   s5    ''//335"11;;=
 	
r!   rJ   c                 @   t               | _        |j                  | j                  d| j                  j	                                |j                  | j                  |       |j                  | ||d       t        | j                  |      j                  | ||       y)zUpdate execution heartbeat.r   T)r@   rA   rE   rC   rD   N)
r   r   rO   r*   rW   rQ   rK   rH   r   rI   )r   rJ   r@   rA   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,   r2   r?   intrL   r   rG   rS   dictrP   r   rY    r!   r   r   r      sj   3(3 ( ( (#F #t #
 4S 4 4 S   * * s C U {  f Js J J+ J J
  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	   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)rH   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!   NrW   exception_handlersc                 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   rW   rj   scores       r   cleanupzExecutionRegistry.cleanupy   s.     '2	8I8K((1e<r!   r4   r@   rA   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   rQ   )r   r4   r@   rA   rm   s        r   rI   zExecutionRegistry.add   sI     "#c)dhhurz :;#(+r!   c                 N    |j                  | j                  |j                        S )z"Remove an execution from registry.)zremr*   r   )r   r4   rA   s      r   rU   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)rn   r   zranger*   r   )r   rt   ru   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 )rw   r1   )ry   appendr   r,   r   r   )r   rt   ru   execution_ids
executionsr>   s         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.)rA   r-   N)r~   rS   r*   )r   r-   rA   r}   r4   s        r   rS   zExecutionRegistry.delete   sB    ((*
# 	9IhC8	9!r!   )NN)r   )rZ   r[   r\   r]   rh   r^   r	   r    r   r:   listrn   r   rc   r   rI   rU   ry   r~   r   rS   re   r!   r   rH   rH   m   s     'L4s 4 4
=% =XVZ^ =Y S J 3 "5	 5Z 5C 5\s \S \$s) \
C # tI "# " "r!   rH   N)r   r   typingr   r   r   uuidr   redisr	   redis.clientr
   r-   r   registryr   r   utilsr   r   r   r   r   rH   re   r!   r   <module>r      sA    ' / /  %  6 G GZs Zsz>" >"r!   