
    s,g                     (   d dl Z d dlZd dl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 d dlmZ dZd	d
dedefdZdeeef   deeef   fdZd	d
defdZd	d
defdZdd	d
defdZdddeeef   fdZddZdddeeef   fdZdddeeef   fdZy)    N)TYPE_CHECKINGAnyDict)Redis   )
BaseWorker)InvalidJobOperation)Jobzrq:pubsub:%s
connectionr   worker_namecommandc                     d|i}|r|j                  |       | j                  t        |z  t        j                  |             y)a#  
    Sends a command to a worker.
    A command is just a string, availble commands are:
        - `shutdown`: Shuts down a worker
        - `kill-horse`: Command for the worker to kill the current working horse
        - `stop-job`: A command for the worker to stop the currently running job

    The command string will be parsed into a dictionary and send to a PubSub Topic.
    Workers listen to the PubSub, and `handle` the specific command.

    Args:
        connection (Redis): A Redis Connection
        worker_name (str): The Job ID
    r   N)updatepublishPUBSUB_CHANNEL_TEMPLATEjsondumps)r   r   r   kwargspayloads        d/var/www/trellinator.diamondhoofcare.com/public_html/venv/lib/python3.12/site-packages/rq/command.pysend_commandr      s<     '"Gv.<djj>QR    r   returnc                 N    t        j                  | d   j                               S )zd
    Returns a dict of command data

    Args:
        payload (dict): Parses the payload dict.
    data)r   loadsdecode)r   s    r   parse_payloadr   &   s      ::gfo,,.//r   c                     t        | |d       y)z
    Sends a command to shutdown a worker.

    Args:
        connection (Redis): A Redis Connection
        worker_name (str): The Job ID
    shutdownNr   r   r   s     r   send_shutdown_commandr#   0   s     [*5r   c                     t        | |d       y)z
    Tell worker to kill it's horse

    Args:
        connection (Redis): A Redis Connection
        worker_name (str): The Job ID
    
kill-horseNr!   r"   s     r   send_kill_horse_commandr&   ;   s     [,7r   job_idc                     t        j                  || |      }|j                  st        d      t	        | |j                  d|       y)z
    Instruct a worker to stop a job

    Args:
        connection (Redis): A Redis Connection
        job_id (str): The Job ID
        serializer (): The serializer
    )r   
serializerzJob is not currently executingstop-job)r'   N)r
   fetchr   r	   r   )r   r'   r)   jobs       r   send_stop_job_commandr-   F   s;     ))Fzj
IC??!"BCCS__jHr   workerr   c                     |d   dk(  rt        | |       y|d   dk(  rt        |        y|d   dk(  rt        | |       yy)zParses payload and routes commands to the worker.

    Args:
        worker (Worker): The worker to use
        payload (Dict[Any, Any]): The Payload
    r   r*   r    r%   N)handle_stop_job_commandhandle_shutdown_commandhandle_kill_worker_commandr.   r   s     r   handle_commandr4   U   sL     yZ'0		z	)'		|	+"673 
,r   c                     | j                   j                  d       t        j                         }t        j                  |t
        j                         y)zUPerform shutdown command.

    Args:
        worker (Worker): The worker to use.
    z1Received shutdown command, sending SIGINT signal.N)loginfoosgetpidkillsignalSIGINT)r.   pids     r   r1   r1   d   s2     JJOOGH
))+CGGCr   c                     | j                   j                  d       | j                  r,| j                   j                  d       | j                          y| j                   j                  d       y)z
    Stops work horse

    Args:
        worker (Worker): The worker to stop
        payload (Dict[Any, Any]): The payload.
    zReceived kill horse command.zKiling horse...z1Worker is not working, kill horse command ignoredN)r6   r7   	horse_pid
kill_horser3   s     r   r2   r2   o   sL     JJOO23

)*

KLr   c                     |j                  d      }| j                  j                  d|       |r+| j                         |k(  r|| _        | j                          y| j                  j                  d|       y)zHandles stop job command.

    Args:
        worker (Worker): The worker to use
        payload (Dict[Any, Any]): The payload.
    r'   zReceived command to stop job %sz'Not working on job %s, command ignored.N)getr6   debugget_current_job_id_stopped_job_idr@   r7   )r.   r   r'   s      r   r0   r0      sc     [["F
JJ6?&++-7 "(

A6Jr   )N)r.   r   )r   r8   r;   typingr   r   r   redisr   r.   r   rq.exceptionsr	   rq.jobr
   r   strr   r   r#   r&   r-   r4   r1   r2   r0    r   r   <module>rL      s    	  + +" - ( SW S3 S S*04S> 0d38n 06g 6C 68 8c 8Ig Is I4< 4$sCx. 4 M| Md38n M"KL K4S> Kr   