o
    /i                     @   s   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 dZG dd deZdS )    )datetimetimezone)TYPE_CHECKINGAnyOptional)uuid4)Redis)Pipeline   )Job)BaseRegistryStartedJobRegistry)as_textcurrent_timestampnowparse_composite_keyc                	   @   s  e Zd ZdZdededefddZdedefd	d
Z	e
defddZe
defddZe
dd Zededededd fddZdd Zedededd fddZedededddd fddZd(deded fdd Zdeddfd!d"Zdefd#d$Zd%ededdfd&d'ZdS ))	Executionz)Class to represent an execution of a job.idjob_id
connectionc                 C   s.   || _ || _|| _t }|| _|| _d | _d S N)r   r   r   r   
created_atlast_heartbeat_job)selfr   r   r   	right_now r   M/var/www/html/flask_server/venv/lib/python3.10/site-packages/rq/executions.py__init__   s   
zExecution.__init__otherreturnc                 C   s   t |tsdS | j|jkS )NF)
isinstancer   r   )r   r   r   r   r   __eq__   s   
zExecution.__eq__c                 C   s   d| j  S )Nzrq:execution:)composite_keyr   r   r   r   key!   s   zExecution.keyc                 C   s&   | j r| j S tj| j| jd| _ | j S )N)r   r   )r   r   fetchr   r   r$   r   r   r   job%   s   zExecution.jobc                 C   s   | j  d| j S )N:)r   r   r$   r   r   r   r#   ,   s   zExecution.composite_keyc                 C   s   | |||d}|   |S )zFetch an execution from Redis.r   r   r   )refresh)clsr   r   r   	executionr   r   r   r&   0   s   zExecution.fetchc                 C   s\   | j | j}|std| j dtjt|d tj	d| _
tjt|d tj	d| _dS )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   datar   r   r   r*   7   s
   zExecution.refreshr#   c                 C   s   t |\}}| |||dS )z>A combination of job_id and execution_id separated by a colon.r)   )r   )r+   r#   r   r   execution_idr   r   r   from_composite_key?   s   zExecution.from_composite_keyr'   ttlpipeliner	   c                 C   sZ   t  j}| ||j|jd}|j||d t|j|dj|||d |jj|||dd |S )zSave execution data to Redis.r)   )r6   r7   r   r   r,   r6   r7   F)r7   r6   xx)	r   hexr   r   saveExecutionRegistryaddstarted_job_registryadd_execution)r+   r'   r6   r7   r   r,   r   r   r   createE   s   zExecution.createNc                 C   s8   |dur|n| j }|j| j|  d || j| dS )z6Save execution data to Redis and JobExecutionRegistry.N)mapping)r   hsetr%   	serializeexpire)r   r6   r7   r   r   r   r   r<   O   s   zExecution.savec                 C   s:   | | j |jj| |d t| j| jdj| |d dS )zDelete an execution from Redis.)r,   r7   r8   N)deleter%   r?   remove_executionr=   r   r   remove)r   r'   r7   r   r   r   rF   V   s   zExecution.deletec                 C   s   | j | j | j dS )N)r   r   r   )r   r   	timestampr   r$   r   r   r   rD   \   s   zExecution.serializer?   c                 C   s\   t  | _|| jd| j  || j| |j| ||dd t| j|dj	| ||d dS )zUpdate execution heartbeat.r   T)r6   r7   r:   r8   r9   N)
r   r   rC   r%   rI   rE   r@   r=   r   r>   )r   r?   r6   r7   r   r   r   	heartbeatc   s
   zExecution.heartbeatr   )__name__
__module____qualname____doc__strr   r   objectboolr"   propertyr%   r   r'   r#   classmethodr&   r*   r5   intrA   r   r<   rF   dictrD   r   rJ   r   r   r   r   r      s*    	
	r   c                   @   s   e Zd ZdZdZdedefddZd dee	 d	ee
 fd
dZdededddefddZdedddefddZd!dedede
e fddZd!dedede
e fddZdeddfddZdS )"r=   zbClass to represent a registry of job executions.
    Each job has its own execution registry.
    zrq:executions:{0}r   r   c                 C   s   || _ || _| j|| _d S r   )r   r   key_templateformatr%   )r   r   r   r   r   r   r   t   s   zExecutionRegistry.__init__NrI   exception_handlersc                 C   s(   |dur|nt  }| j| jd| dS )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   rI   rX   scorer   r   r   cleanupy   s   zExecutionRegistry.cleanupr,   r6   r7   r	   r    c                 C   s8   t  | }|| j|j|d i || j|d  dS )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   rE   )r   r,   r6   r7   rZ   r   r   r   r>      s   
zExecutionRegistry.addc                 C   s   | | j|jS )z"Remove an execution from registry.)zremr%   r   )r   r,   r7   r   r   r   rH      s   zExecutionRegistry.remover   startendc                 C   s$   |    dd | j| j||D S )&Returns all executions IDs in registryc                 S   s   g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>   s    z7ExecutionRegistry.get_execution_ids.<locals>.<listcomp>)r[   r   zranger%   )r   r`   ra   r   r   r   get_execution_ids   s   z#ExecutionRegistry.get_execution_idsc                 C   s8   |  ||}g }|D ]}|tj|| j| jd q
|S )rb   r)   )rf   appendr   r&   r   r   )r   r`   ra   execution_ids
executionsr4   r   r   r   get_executions   s
   z ExecutionRegistry.get_executionsr'   c                 C   s0   |   }|D ]	}|j||d q|| j dS )zDelete the registry.)r7   r'   N)rj   rF   r%   )r   r'   r7   ri   r,   r   r   r   rF      s   zExecutionRegistry.delete)NN)r   r_   )rK   rL   rM   rN   rV   rO   r   r   r   r1   listr[   r   rT   r   r>   rH   rf   rj   r   rF   r   r   r   r   r=   m   s    
	r=   N)r   r   typingr   r   r   uuidr   redisr   redis.clientr	   r'   r   registryr   r   utilsr   r   r   r   r   r=   r   r   r   r   <module>   s    ]