
    AҐi                     $    d Z ddlZ G d d      Zy)z
ASGI lifespan protocol manager.

Manages startup and shutdown events for ASGI applications,
enabling frameworks like FastAPI to run initialization and
cleanup code.
    Nc                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
LifespanManageraI  Manages ASGI lifespan events (startup/shutdown).

    The lifespan protocol allows ASGI applications to run code at
    startup and shutdown. This is essential for applications that
    need to initialize database connections, caches, or other
    resources.

    ASGI lifespan messages:
    - Server sends: {"type": "lifespan.startup"}
    - App responds: {"type": "lifespan.startup.complete"} or
                    {"type": "lifespan.startup.failed", "message": "..."}
    - Server sends: {"type": "lifespan.shutdown"}
    - App responds: {"type": "lifespan.shutdown.complete"}
    Nc                    || _         || _        ||ni | _        t        j                         | _        t        j                         | _        d| _        d| _        d| _	        t        j                         | _        d| _        d| _        y)zInitialize the lifespan manager.

        Args:
            app: ASGI application callable
            logger: Logger instance
            state: Shared state dict for the application
        NF)apploggerstateasyncioEvent_startup_complete_shutdown_complete_startup_failed_startup_error_shutdown_errorQueue_receive_queue_task_app_finished)selfr   r   r   s       O/var/www/descvideos/venv/lib/python3.12/site-packages/gunicorn/asgi/lifespan.py__init__zLifespanManager.__init__    sq     #/UR
!(")--/$"#%mmo
"    c                   K   dddd| j                   d}| j                  j                  ddi       d{    t        j                  | j                  |            | _        	 t        j                  | j                  j                         d	
       d{    | j                  rD| j                  r| j                  j                          | j                  xs d}t        d|       | j                  j!                  d       y7 7 r# t        j                  $ r2 | j                  r| j                  j                          t        d      w xY ww)zRun lifespan startup and wait for completion.

        Raises:
            RuntimeError: If startup fails or app doesn't support lifespan
        lifespanz3.0z2.4)versionspec_version)typeasgir   r   zlifespan.startupN      >@timeoutzLifespan startup timed outzUnknown errorzLifespan startup failed: zASGI lifespan startup complete)r   r   putr	   create_task_run_lifespanr   wait_forr   waitTimeoutErrorcancelRuntimeErrorr   r   r   debug)r   scopemsgs      r   startupzLifespanManager.startup5   s1      %u=ZZ
 !!%%v/A&BCCC ((););E)BC
	=""&&++-   zz

!!#%%8C!:3%@AA:;- 	D ## 	=zz

!!#;<<	=s;   4ED-E%2D D
D A-E
D AEEc                    K   | j                   r| j                  j                  d       y| j                  j	                  ddi       d{    	 t        j                  | j                  j                         d       d{    | j                  r&| j                  j                  d| j                         | j                  rI| j                  j                         s/| j                  j                          	 | j                   d{    | j                  j                  d	       y7 7 # t
        j                  $ r | j                  j                  d       Y w xY w7 X# t
        j                   $ r Y kw xY ww)
zjSignal shutdown and wait for completion.

        This should be called during graceful shutdown.
        zASGI lifespan already finishedNr   zlifespan.shutdownr   r   zLifespan shutdown timed outzLifespan shutdown error: %szASGI lifespan shutdown complete)r   r   r)   r   r!   r	   r$   r   r%   r&   warningr   errorr   doner'   CancelledErrorr   s    r   shutdownzLifespanManager.shutdownZ   s@    
 KK>? !!%%v/B&CDDD	?""'',,.   KK;T=Q=QR ::djjoo/JJjj   	;<- 	E ## 	?KK =>	? !)) s   A	E>D+E>2D/ D-D/ A2E>;E% 
E#E% E>-D/ /.E E>E  E>#E% %E;8E>:E;;E>c                   K   	 | j                  || j                  | j                         d{    d| _        | j                  j                         s(d| _
        d| _        | j                  j                          | j                  j                         s| j                  j                          yy7 # t        j                  $ r  t
        $ r}| j                  j                  d|       | j                  j                         s2d| _
        t        |      | _        | j                  j                          nD| j                  j                         s*t        |      | _        | j                  j                          Y d}~Od}~ww xY w# d| _        | j                  j                         s(d| _
        d| _        | j                  j                          | j                  j                         s| j                  j                          w w xY ww)zRun the ASGI lifespan protocol.NzLifespan application raised: %sTz*Application exited before startup complete)r   _receive_sendr	   r1   	Exceptionr   r)   r   is_setr   strr   setr   r   r   )r   r*   es      r   r#   zLifespanManager._run_lifespan|   s    	.((5$--<<< "&D))002'+$&R#&&**,**113''++- 4+ =%% 	 
	.KK?C))002'+$&)!f#&&**,,,335'*1v$''++-
	. "&D))002'+$&R#&&**,**113''++- 4sL   H*B4 B2B4 A?H2B4 4FB,F<F
 FF
 
B H

Hc                 R   K   | j                   j                          d{   S 7 w)z#ASGI receive callable for lifespan.N)r   getr2   s    r   r5   zLifespanManager._receive   s!     ((,,....s   '%'c                   K   |d   }|dk(  r6| j                   j                          | j                  j                  d       y|dk(  r_d| _        |j                  dd      | _        | j                   j                          | j                  j                  d| j                         y|d	k(  r6| j                  j                          | j                  j                  d
       y|dk(  rX|j                  dd      | _        | j                  j                          | j                  j                  d| j                         yyw)z ASGI send callable for lifespan.r   zlifespan.startup.completez"Received lifespan.startup.completezlifespan.startup.failedTmessage z$Received lifespan.startup.failed: %szlifespan.shutdown.completez#Received lifespan.shutdown.completezlifespan.shutdown.failedz%Received lifespan.shutdown.failed: %sN)	r   r:   r   r)   r   r=   r   r   r   )r   r?   msg_types      r   r6   zLifespanManager._send   s    6?22""&&(KKBC22#'D ")++i"<D""&&(KKD"113 55##'')KKCD33#*;;y"#=D ##'')KKE"224 4s   D>E )N)
__name__
__module____qualname____doc__r   r,   r3   r#   r5   r6    r   r   r   r      s'    #*#<J =D.6/4r   r   )rE   r	   r   rF   r   r   <module>rG      s   
 b4 b4r   