o
    *fj#                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
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 ddlmZ e
rXddlZejZnz	ddlZejZW n eyn   dZeZY nw dZG dd dejZ G dd de Z!dS )z%Async gunicorn worker for aiohttp.web    N)	FrameType)TYPE_CHECKINGAnyOptional)AccessLogFormat)base)web   )
set_result)Application)AccessLogger)GunicornWebWorkerGunicornUVLoopWebWorkerc                       s   e Zd ZejZejZde	de	ddf fddZ
d" fddZd"d	d
Zd"ddZd#ddZ	d$ded ddfddZd"ddZdededB ddfddZdededB ddfddZede	ddfddZdedefd d!Z  ZS )%r   argskwreturnNc                    s(   t  j|i | d | _d| _d | _d S Nr   )super__init___task	exit_code_notify_waiter)selfr   r   	__class__ r/var/www/botplatforma_usr/data/www/botplatforma.ilyaolesya.ru/.venv/lib/python3.10/site-packages/aiohttp/worker.pyr   *   s   
zGunicornWebWorker.__init__c                    sH   zt    W n	 ty   Y nw t  | _t | j t   d S N)	asyncioget_event_loopcloseRuntimeErrornew_event_looploopset_event_loopr   init_processr   r   r   r   r%   1   s   
zGunicornWebWorker.init_processc              
   C   sv   d| _ | j|  | _z| j| j W | j| j  | j  n| j| j  | j  w t	| j
 d S NF)bootedr#   create_task_runr   run_until_completeshutdown_asyncgensr    sysexitr   r&   r   r   r   run>   s   zGunicornWebWorker.runc                    s  d }t | jtr| j}n/t| jstjdk r4t| jr4|  I d H }t |tj	r1|}|j
}n|}ntd| j |d u rc| jjrH| jjnd }tj	|| j| jj|| | jj| jjd d d}| I d H  | jjrt| | jnd }|}|d us~J |j}|d usJ | jD ]}tj|||d}| I d H  qd| _t }	zA| jr|   |j }
| j!r|
| j!krd| _| j"d	|  n|	t kr| j#t$ krd| _| j"d
|  n| % I d H  | jsW n	 t&y   Y nw |' I d H  d S )N)      zSwsgi app should be either Application or async function returning Application, got d   _   )loggerkeepalive_timeout
access_logaccess_log_formatshutdown_timeout)ssl_contextTFzMax requests, shutting down: %sz!Parent changed, shutting down: %s)(
isinstancewsgir   inspectiscoroutinefunctionr-   version_infor   r   	AppRunnerappr!   cfg	accessloglogr6   	keepalive_get_valid_log_formatr7   graceful_timeoutsetupis_ssl_create_ssl_contextserversocketsSockSitestartr(   osgetpidalivenotifyrequests_countmax_requestsinfoppidgetppid_wait_next_notify	Exceptioncleanup)r   runnerr@   r;   r6   ctxrJ   socksitepidcntr   r   r   r*   P   sx   

zGunicornWebWorker._runasyncio.Future[bool]c                 C   s>   |    | j}|d usJ |  | _}| jd| j | |S )Ng      ?)_notify_waiter_doner#   create_futurer   
call_later)r   r#   waiterr   r   r   rW      s   z#GunicornWebWorker._wait_next_notifyrd   c                 C   s8   |d u r| j }|d urt|d || j u rd | _ d S d S )NT)r   r
   )r   rd   r   r   r   ra      s   


z%GunicornWebWorker._notify_waiter_donec                 C   s   | j tj| jtjd  | j tj| jtjd  | j tj| jtjd  | j tj| j	tjd  | j tj
| jtj
d  | j tj| jtjd  ttjd ttj
d ttjtj d S r'   )r#   add_signal_handlersignalSIGQUIThandle_quitSIGTERMhandle_exitSIGINTSIGWINCHhandle_winchSIGUSR1handle_usr1SIGABRThandle_abortsiginterruptSIGCHLDSIG_DFLr&   r   r   r   init_signals   s*   zGunicornWebWorker.init_signalssigframec                 C   s   d| _ | j|  |   d S r'   )rP   rA   
worker_intra   r   rv   rw   r   r   r   rh      s   zGunicornWebWorker.handle_quitc                 C   s&   d| _ d| _| j|  td d S )NFr	   )rP   r   rA   worker_abortr-   r.   ry   r   r   r   rq      s   zGunicornWebWorker.handle_abortrA   
SSLContextc                 C   s\   t du rtdt | j}|| j| j | j|_| j	r#|
| j	 | jr,|| j |S )z~Creates SSLContext instance for usage in asyncio.create_server.

        See ssl.SSLSocket.__init__ for more details.
        NzSSL is not supported.)sslr!   r{   ssl_versionload_cert_chaincertfilekeyfile	cert_reqsverify_modeca_certsload_verify_locationsciphersset_ciphers)rA   r[   r   r   r   rI      s   z%GunicornWebWorker._create_ssl_contextsource_formatc                 C   s(   || j kr| jS td|rtd|S )Nz%\([^\)]+\)zGunicorn's style options in form of `%(name)s` are not supported for the log formatting. Please use aiohttp's format specification to configure access log formatting: http://docs.aiohttp.org/en/stable/logging.html#format-specification)DEFAULT_GUNICORN_LOG_FORMATDEFAULT_AIOHTTP_LOG_FORMATresearch
ValueError)r   r   r   r   r   rE      s   
z'GunicornWebWorker._get_valid_log_formatr   N)r   r`   r   )__name__
__module____qualname__r   
LOG_FORMATr   GunicornAccessLogFormatdefaultr   r   r   r%   r/   r*   rW   r   ra   ru   intr   rh   rq   staticmethodrI   strrE   __classcell__r   r   r   r   r   &   s(    


J

&	r   c                       s   e Zd Zd fddZ  ZS )r   r   Nc                    sH   dd l }zt   W n	 ty   Y nw t|  t   d S r   )	uvloopr   r   r    r!   set_event_loop_policyEventLoopPolicyr   r%   )r   r   r   r   r   r%     s   z$GunicornUVLoopWebWorker.init_processr   )r   r   r   r%   r   r   r   r   r   r     s    r   )"__doc__r   r<   rN   r   rf   r-   typesr   typingr   r   r   gunicorn.configr   r   gunicorn.workersr   aiohttpr   helpersr
   web_appr   web_logr   r|   r{   ImportErrorobject__all__Workerr   r   r   r   r   r   <module>   s:    
 `