o
    NK&h[                     @   sj   d dl mZ d dlmZmZ d dlmZmZmZ d dl	m
Z
mZ d dlmZ d dlmZ G dd dZd	S )
    )
Connection)environgetpid)AnyCallableOptional)Colorslogger)ProcessState)WorkerStatec                   @   s&  e Zd ZdZdedeeef fddZdd Z						
	d1dede
def deeef dedee dedededd	fddZdedd	fddZdd Z			d2deded efd!d"ZeZ	 d#efd$d%Zd3d&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eef fd/d0Zd	S )4WorkerMultiplexera)  Multiplexer for Sanic workers.

    This is instantiated inside of worker porocesses only. It is used to
    communicate with the monitor process.

    Args:
        monitor_publisher (Connection): The connection to the monitor.
        worker_state (Dict[str, Any]): The state of the worker.
    monitor_publisherworker_statec                 C   s   || _ t|| j| _d S )N)_monitor_publisherr   name_state)selfr   r    r   K/var/www/html/venv/lib/python3.10/site-packages/sanic/worker/multiplexer.py__init__   s   zWorkerMultiplexer.__init__c              
   C   sb   t tj dtj tj dtj dtj | j| j i | j	j	| j dt
jji| j	j	| j< dS )z Acknowledge the worker is ready.zProcess ack: z%s z[%s]stateN)r	   debugr   BLUEBOLDSANICENDr   pidr   r
   ACKEDr   r   r   r   ack   s   zWorkerMultiplexer.ackFNT   identfunc.kwargs	transientrestartabletracked
auto_startworkersreturnc	           
      C   s$   ||||||||f}	| j |	 dS )a  Manages the initiation and monitoring of a worker process.

        Args:
            ident (str): A unique identifier for the worker process.
            func (Callable[..., Any]): The function to be executed in the worker process.
            kwargs (Dict[str, Any]): A dictionary of arguments to be passed to `func`.
            transient (bool, optional): Flag to mark the process as transient. If `True`,
                the Worker Manager will restart the process with any global restart
                (e.g., auto-reload). Defaults to `False`.
            restartable (Optional[bool], optional): Flag to mark the process as restartable. If `True`,
                the Worker Manager can restart the process if prompted. Defaults to `None`.
            tracked (bool, optional): Flag to indicate whether the process should be tracked
                after its completion. Defaults to `False`.
            auto_start (bool, optional): Flag to indicate whether the process should be started
            workers (int, optional): The number of worker processes to run. Defaults to 1.

        This method packages the provided arguments into a bundle and sends them back to the
        main process to be managed by the Worker Manager.
        Nr   send)
r   r!   r"   r#   r$   r%   r&   r'   r(   bundler   r   r   manage*   s   
zWorkerMultiplexer.manageservingc                 C   s(   i | j j | j d|i| j j | j< dS )zmSet the worker to serving.

        Args:
            serving (bool): Whether the worker is serving.
        r.   N)r   r   )r   r.   r   r   r   set_servingT   s
   zWorkerMultiplexer.set_servingc                 C   s2   z	| j j | j= W dS  ty   td Y dS w )zRun cleanup at worker exit.z#Monitor process has already exited.N)r   r   ConnectionRefusedErrorr	   r   r   r   r   r   exit_   s
   zWorkerMultiplexer.exit r   all_workerszero_downtimec                 C   sP   |r|rt d|s|rdn| j}|ds|d7 }|r |d7 }| j| dS )zRestart the worker.

        Args:
            name (str): The name of the process to restart.
            all_workers (bool): Whether to restart all workers.
            zero_downtime (bool): Whether to restart with zero downtime.
        z@Ambiguous restart with both a named process and all_workers=Truez__ALL_PROCESSES__::z:STARTUP_FIRSTN)
ValueErrorr   endswithr   r+   )r   r   r3   r4   r   r   r   restartf   s   
zWorkerMultiplexer.restartnum_workersc                 C   s   d| }| j | dS )zvScale the number of workers.

        Args:
            num_workers (int): The number of workers to scale to.
        z
__SCALE__:Nr*   )r   r9   messager   r   r   scale   s   
zWorkerMultiplexer.scaleearlyc                 C   s   |rdnd}| j | dS )zcTerminate the worker.

        Args:
            early (bool): Whether to terminate early.
        __TERMINATE_EARLY____TERMINATE__Nr*   )r   r<   r:   r   r   r   	terminate   s   zWorkerMultiplexer.terminatec                 C   s   t  S )zThe process ID of the worker.)r   r   r   r   r   r         zWorkerMultiplexer.pidc                 C   s   t ddS )zThe name of the worker.SANIC_WORKER_NAMEr2   )r   getr   r   r   r   r      s   zWorkerMultiplexer.namec                 C   s   | j S )zThe state of the worker.)r   r   r   r   r   r      r@   zWorkerMultiplexer.statec                 C   s
   | j  S )zThe state of all workers.)r   fullr   r   r   r   r(      s   
zWorkerMultiplexer.workers)FNFTr    )r2   FF)F)__name__
__module____qualname____doc__r   dictstrr   r   r   r   boolr   intr-   r/   r1   r8   reloadr;   r?   propertyr   r   r   r(   r   r   r   r   r   
   sp    




	

*	
		
r   N)multiprocessing.connectionr   osr   r   typingr   r   r   	sanic.logr   r	   sanic.worker.processr
   sanic.worker.stater   r   r   r   r   r   <module>   s    