o
    NK&hO                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ G dd dZdS )    )annotations)Mapping)datetime)isawaitable)
Connection)environ)Path)AnyUnion)Unauthorized)Default)logger)Request)jsonc                   @  s   e Zd ZdZd7ddZd8d9ddZdd Zd:ddZd;ddZd<d d!Z	d=d$d%Z
d>d&d'Zed?d)d*Zd@dAd.d/ZdBd2d3ZdCd4d5Zd6S )D	Inspectora  Inspector for Sanic workers.

    This class is used to create an inspector for Sanic workers. It is
    instantiated by the worker class and is used to create a Sanic app
    that can be used to inspect and control the workers and the server.

    It is not intended to be used directly by the user.

    See [Inspector](/en/guide/deployment/inspector) for more information.

    Args:
        publisher (Connection): The connection to the worker.
        app_info (Dict[str, Any]): Information about the app.
        worker_state (Mapping[str, Any]): The state of the worker.
        host (str): The host to bind the inspector to.
        port (int): The port to bind the inspector to.
        api_key (str): The API key to use for authentication.
        tls_key (Union[Path, str, Default]): The path to the TLS key file.
        tls_cert (Union[Path, str, Default]): The path to the TLS cert file.
    	publisherr   app_infodict[str, Any]worker_stateMapping[str, Any]hoststrportintapi_keytls_keyUnion[Path, str, Default]tls_certc	           	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _d S N)
_publisherr   r   r   r   r   r   r   )	selfr   r   r   r   r   r   r   r    r!   I/var/www/html/venv/lib/python3.10/site-packages/sanic/worker/inspector.py__init__(   s   
zInspector.__init__Treturnc                 K  sd   ddl m} |d| _|   |r0| jj| j| jdt| jt	s,t| j
t	s,| j| j
dnd d | S )Nr   )Sanicr   T)keycert)r   r   single_processssl)sanicr%   app_setuprunr   r   
isinstancer   r   r   )r    r-   _r%   r!   r!   r"   __call__<   s   


	zInspector.__call__c                 C  sD   | j d| j | j d| j | jr| j | j dtd< d S )N/z/<action:str>trueSANIC_IGNORE_PRODUCTION_WARNING)	r+   get_infopost_actionr   
on_request_authenticationr   )r    r!   r!   r"   r,   M   s
   zInspector._setuprequestr   Nonec                 C  s   |j | jkr
tdd S )NzBad API key)tokenr   r   r    r:   r!   r!   r"   r9   T   s   zInspector._authenticationactionc                   sp   t d| d }t| |d }|r/i }|jr|j}|dd}||i |}t|r/|I d H }| ||I d H S )NzIncoming inspector action: %sargsr!   )r   infogetattrbodyr   popr   _respond)r    r:   r>   outputmethodkwargsr?   r!   r!   r"   r7   X   s   
zInspector._actionc                   s   |  ||  I d H S r   )rD   _state_to_jsonr=   r!   r!   r"   r5   g   s   zInspector._inforE   r	   c                   s"   |j dd}td|i|dS )Nr>   r@   )metaresult)
match_infor4   r   )r    r:   rE   namer!   r!   r"   rD   j   s   zInspector._respondc                 C  s"   d| j i}| t| j|d< |S )Nr@   workers)r   
_make_safedictr   )r    rE   r!   r!   r"   rH   n   s   
zInspector._state_to_jsonobjc                 C  sF   |   D ]\}}t|trt|| |< qt|tr | | |< q| S r   )itemsr.   rO   r   rN   r   	isoformat)rP   r&   valuer!   r!   r"   rN   s   s   

zInspector._make_safeFzero_downtimeboolc                 C  s    d}|r|d7 }| j | dS )zReload the workers

        Args:
            zero_downtime (bool, optional): Whether to use zero downtime
                reload. Defaults to `False`.
        z__ALL_PROCESSES__:z:STARTUP_FIRSTNr   send)r    rT   messager!   r!   r"   reload|   s   zInspector.reloadreplicasUnion[str, int]c                 C  s>   d}|rt |}d| }t| d| }| j| |S )zScale the number of workers

        Args:
            replicas (Union[str, int]): The number of workers to scale to.

        Returns:
            str: A log message.
           zScaling to z
__SCALE__:)r   r   r@   r   rW   )r    rZ   num_workerslog_msgrX   r!   r!   r"   scale   s   	


zInspector.scalec                 C  s   d}| j | dS )zShutdown the workers__TERMINATE__NrV   )r    rX   r!   r!   r"   shutdown   s   zInspector.shutdownN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )T)r$   r   )r:   r   r$   r;   )r:   r   r>   r   )r:   r   )r:   r   rE   r	   )r$   r   )rP   r   r$   r   )F)rT   rU   r$   r;   )rZ   r[   r$   r   )r$   r;   )__name__
__module____qualname____doc__r#   r0   r,   r9   r7   r5   rD   rH   staticmethodrN   rY   r_   ra   r!   r!   r!   r"   r      s    






r   N)
__future__r   collections.abcr   r   inspectr   multiprocessing.connectionr   osr   pathlibr   typingr	   r
   sanic.exceptionsr   sanic.helpersr   	sanic.logr   sanic.requestr   sanic.responser   r   r!   r!   r!   r"   <module>   s    