
    h                     <   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
rddlZej        Zn	 ddlZej        Zn# e$ r dZeZY nw xY wdZ G d dej                  Z  G 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                       e Zd Zej        Zej        Zde	de	ddf fdZ
d fdZddZddZdd
Z	 dded	         ddfdZddZdedee         ddfdZdedee         ddfdZede	ddfd            ZdedefdZ xZS )r   argskwreturnNc                 d     t                      j        |i | d | _        d| _        d | _        d S Nr   )super__init___task	exit_code_notify_waiter)selfr   r   	__class__s      P/var/www/html/prod/cognitive/venv/lib/python3.11/site-packages/aiohttp/worker.pyr   zGunicornWebWorker.__init__+   s;    $%"%%%37
>B    c                     t          j                                                     t          j                    | _        t          j        | j                   t                                                       d S N)asyncioget_event_loopclosenew_event_looploopset_event_loopr   init_process)r   r   s    r   r'   zGunicornWebWorker.init_process2   s]      &&(((*,,	ty)))r   c                    | j                             |                                           | _        	 | j                             | j                   n*# t
          $ r | j                            d           Y nw xY w| j                             | j                                                    | j         	                                 t          j        | j                   d S )NzException in gunicorn worker)r%   create_task_runr   run_until_complete	Exceptionlog	exceptionshutdown_asyncgensr#   sysexitr   r   s    r   runzGunicornWebWorker.run;   s    Y**499;;77
	?I((4444 	? 	? 	?H=>>>>>	?	$$TY%A%A%C%CDDD	     s   A $A:9A:c           	        K   d }t          | j        t                    r| j        }nt          j        | j                  s)t
          j        dk     rZt          j        | j                  rA|                                  d {V }t          |t          j	                  r
|}|j
        }n*|}n't          d                    | j                            |p| j        j        r| j        j        nd }t          j	        || j        | j        j        ||                     | j        j                  | j        j        dz  dz            }|                                 d {V  | j        j        r|                     | j                  nd }|}|J |j        }|J | j        D ]3}t          j        |||          }|                                 d {V  4t9          j                    }		 | j        r|                                  |j         }
| j!        r.|
| j!        k    r#d| _        | j        "                    d|            np|	t9          j                    k    r?| j#        t9          j$                    k    r#d| _        | j        "                    d	|            n| %                                 d {V  | j        n# tL          $ r Y nw xY w|'                                 d {V  d S )
N)      zUwsgi app should be either Application or async function returning Application, got {}d   _   )loggerkeepalive_timeout
access_logaccess_log_formatshutdown_timeout)ssl_contextFzMax requests, shutting down: %sz!Parent changed, shutting down: %s)(
isinstancewsgir   inspectiscoroutinefunctionr0   version_infor!   r	   	AppRunnerappRuntimeErrorformatcfg	accesslogr-   r;   	keepalive_get_valid_log_formatr<   graceful_timeoutsetupis_ssl_create_ssl_contextserversocketsSockSitestartosgetpidalivenotifyrequests_countmax_requestsinfoppidgetppid_wait_next_notifyBaseExceptioncleanup)r   runnerrE   r@   r;   ctxrP   socksitepidcnts              r   r*   zGunicornWebWorker._runG   s,     di-- 	)CC(33 	w&&7+Fty+Q+Q&$$$$$$D$.. j??Evdi?P?P  
 >040BL,,J]x"&("4%"&"<"<H.# # "&!:S!@2!E	 	 	F llnn48HOMd&&tx000!!!!!!L 	 	D<  D
 **,, ikk	* 3+$ 3t/@)@)@!&DJHMM"CTJJJJBIKK''DI,E,E!&DJHMM"EtLLLL00222222222 * 3  	 	 	D	 nns   %CJ4 4
K Kasyncio.Future[bool]c                     |                                   | j        }|J |                                x| _        }| j                            d| j         |           |S )Ng      ?)_notify_waiter_doner%   create_futurer   
call_later)r   r%   waiters      r   r]   z#GunicornWebWorker._wait_next_notify   sb      """y'+'9'9';';;f	S$":FCCCr   rk   c                 `    || j         }|t          |d           || j         u r	d | _         d S d S )NT)r   r   )r   rk   s     r   rh   z%GunicornWebWorker._notify_waiter_done   sK     >(Fvt$$$T((("&D )(r   c                 
   | 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          j        t          j        d           t          j        t          j
        d           d S NF)r%   add_signal_handlersignalSIGQUIThandle_quitSIGTERMhandle_exitSIGINTSIGWINCHhandle_winchSIGUSR1handle_usr1SIGABRThandle_abortsiginterruptr2   s    r   init_signalszGunicornWebWorker.init_signals   s<    		$$ND,fnd	
 	
 	
 		$$ND,fnd	
 	
 	
 		$$M4+V]D	
 	
 	
 		$$OT.	
 	
 	
 		$$ND,fnd	
 	
 	
 		$$ND-v~t	
 	
 	
 	FNE222FNE22222r   sigframec                 p    d| _         | j                            |            |                                  d S rn   )rV   rH   
worker_intrh   r   r~   r   s      r   rr   zGunicornWebWorker.handle_quit   s;    
 	D!!! 	  """""r   c                 ~    d| _         d| _        | j                            |            t	          j        d           d S )NFr
   )rV   r   rH   worker_abortr0   r1   r   s      r   r{   zGunicornWebWorker.handle_abort   s7    
d###r   rH   
SSLContextc                 @   t           t          d          t          j        | 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.)sslrF   r   ssl_versionload_cert_chaincertfilekeyfile	cert_reqsverify_modeca_certsload_verify_locationsciphersset_ciphers)rH   ra   s     r   rO   z%GunicornWebWorker._create_ssl_context   s     ;6777nS_--CL#+666-< 	4%%cl333; 	)OOCK(((
r   source_formatc                 r    || j         k    r| j        S t          j        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   s     r   rK   z'GunicornWebWorker._get_valid_log_format   sL    D<<<22Y~}55 		!(   ! r   r   N)r   rf   r    )__name__
__module____qualname__r   
LOG_FORMATr   GunicornAccessLogFormatdefaultr   r   r   r'   r3   r*   r]   r   rh   r}   intr   rr   r{   staticmethodrO   strrK   __classcell__r   s   @r   r   r   &   s       !-!8"9"ACc C C C C C C C C     
! 
! 
! 
!B B B BH    :>	' 	'56	'		' 	' 	' 	'3 3 3 3D#s #8I+> #4 # # # # HY,? D          \"!3 !3 ! ! ! ! ! ! ! !r   r   c                         e Zd Zd fdZ xZS )r   r   Nc                     dd l }t          j                                                     t          j        |                                           t                                                       d S r   )uvloopr!   r"   r#   set_event_loop_policyEventLoopPolicyr   r'   )r   r   r   s     r   r'   z$GunicornUVLoopWebWorker.init_process   sd     	  &&(((
 	%f&<&<&>&>???r   r   )r   r   r   r'   r   r   s   @r   r   r      s=                 r   r   )"__doc__r!   rA   rT   r   rp   r0   typesr   typingr   r   r   gunicorn.configr   r   gunicorn.workersr   aiohttpr	   helpersr   web_appr   web_logr   r   r   ImportErrorobject__all__Workerr   r    r   r   <module>r      s   + +   				 				  



       / / / / / / / / / / F F F F F F ! ! ! ! ! !                         ! ! ! ! ! ! JJJJJ


^

   



 ;I! I! I! I! I! I! I! I!X    /     s   A* *	A65A6