o
    rN)j7                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZ d dlmZmZmZ G dd dejZG d	d
 d
ZdS )    )annotationsN)TracebackType)Any)LifespanFailureLifespanUnsupportedUnexpectedMessage)ASGILifespanModeMessagec                   @  s8   e Zd ZdZe Ze Ze Ze Z	e Z
dS )LifespanCycleStatea  
    The state of the ASGI `lifespan` connection.

    * **CONNECTING** - Initial state. The ASGI application instance will be run with
    the connection scope containing the `lifespan` type.
    * **STARTUP** - The lifespan startup event has been pushed to the queue to be
    received by the application.
    * **SHUTDOWN** - The lifespan shutdown event has been pushed to the queue to be
    received by the application.
    * **FAILED** - A lifespan failure has been detected, and the connection will be
    closed with an error.
    * **UNSUPPORTED** - An application attempted to send a message before receiving
    the lifespan startup event. If the lifespan argument is "on", then the connection
    will be closed with an error.
    N)__name__
__module____qualname____doc__enumauto
CONNECTINGSTARTUPSHUTDOWNFAILEDUNSUPPORTED r   r   z/var/www/html/finance.cargoinsureonline.com/_shared/backend-venv/lib/python3.10/site-packages/mangum/protocols/lifespan.pyr      s    r   c                   @  s`   e Zd ZdZd!dd	Zd"d
dZd#ddZd"ddZd$ddZd%ddZ	d"ddZ
d"ddZd S )&LifespanCyclea  
    Manages the application cycle for an ASGI `lifespan` connection.

    * **app** - An asynchronous callable that conforms to version 3.0 of the ASGI
    specification. This will usually be an ASGI framework application instance.
    * **lifespan** - A string to configure lifespan support. Choices are `auto`, `on`,
    and `off`. Default is `auto`.
    * **state** - An enumerated `LifespanCycleState` type that indicates the state of
    the ASGI connection.
    * **exception** - An exception raised while handling the ASGI event. This may or
    may not be raised depending on the state.
    * **app_queue** - An asyncio queue (FIFO) containing messages to be received by the
    application.
    * **startup_event** - An asyncio event object used to control the application
    startup flow.
    * **shutdown_event** - An asyncio event object used to control the application
    shutdown flow.
    appr   lifespanr	   returnNonec                 C  sX   || _ || _tj| _d | _t | _t	 | _
t | _t | _td| _i | _d S )Nzmangum.lifespan)r   r   r   r   state	exceptionasyncioget_event_looploopQueue	app_queueEventstartup_eventshutdown_eventlogging	getLoggerloggerlifespan_state)selfr   r   r   r   r   __init__9   s   




zLifespanCycle.__init__c                 C  s$   | j |   | j |   dS )z,Runs the event loop for application startup.N)r"   create_taskrunrun_until_completestartupr,   r   r   r   	__enter__E   s   zLifespanCycle.__enter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                 C  s   | j |   dS )z-Runs the event loop for application shutdown.N)r"   r0   shutdown)r,   r4   r6   r8   r   r   r   __exit__J   s   zLifespanCycle.__exit__c              
     s,  zz|  dddd| jd| j| jI dH  W n> ty'   | jd Y n= ttfy= } z	|| _	W Y d}~n8d}~w t
yV } z| jjd|d	 W Y d}~n,d}~ww W | j  | j  dS W | j  | j  dS W | j  | j  dS W | j  | j  dS | j  | j  w )
z;Calls the application with the `lifespan` connection scope.r   z2.0z3.0)spec_versionversion)typeasgir   Nz-ASGI 'lifespan' protocol appears unsupported.z!Exception in 'lifespan' protocol.)exc_info)r   r+   receivesendr   r*   infor   r   r   BaseExceptionerrorr&   setr'   )r,   excr   r   r   r/   S   s<   




zLifespanCycle.runr
   c                   s<   | j tju rtj| _ n
| j tju rtj| _ | j I dH S )z=Awaited by the application to receive ASGI `lifespan` events.N)r   r   r   r   r   r$   getr2   r   r   r   rA   e   s   
zLifespanCycle.receivemessagec                   s"  |d }| j d| j| | jtju r%| jdkrtdtj| _td|dvr5tj	| _t
d| d| jtju rb|d	krF| j  dS |d
kr`tj	| _| j  |dd}td| dS | jtju r|dkrs| j  dS |dkrtj	| _| j  |dd}td| dS dS )z:Awaited by the application to send ASGI `lifespan` events.r>   z*%s:  '%s' event received from application.onz?Lifespan connection failed during startup and lifespan is 'on'.z&Lifespan protocol appears unsupported.)lifespan.startup.completelifespan.shutdown.completelifespan.startup.failedlifespan.shutdown.failedzUnexpected 'z' event received.rK   rM   rI    zLifespan startup failure. rL   rN   zLifespan shutdown failure. N)r*   rC   r   r   r   r   r   r   r   r   r   r   r&   rF   rH   r   r'   )r,   rI   message_typemessage_valuer   r   r   rB   u   s<   


zLifespanCycle.sendc                   sp   | j d | jddiI dH  | j I dH  | jtju r%t	| j
| j
s0| j d dS | j d dS )zDPushes the `lifespan` startup event to the queue and handles errors.z Waiting for application startup.r>   zlifespan.startupNzApplication startup complete.zApplication startup failed.)r*   rC   r$   putr&   waitr   r   r   r   r   r2   r   r   r   r1      s   
zLifespanCycle.startupc                   sN   | j d | jddiI dH  | j I dH  | jtju r%t	| j
dS )zEPushes the `lifespan` shutdown event to the queue and handles errors.z!Waiting for application shutdown.r>   zlifespan.shutdownN)r*   rC   r$   rR   r'   rS   r   r   r   r   r   r2   r   r   r   r:      s   
zLifespanCycle.shutdownN)r   r   r   r	   r   r   )r   r   )r4   r5   r6   r7   r8   r9   r   r   )r   r
   )rI   r
   r   r   )r   r   r   r   r-   r3   r;   r/   rA   rB   r1   r:   r   r   r   r   r   %   s    



	


)r   )
__future__r   r    r   r(   typesr   typingr   mangum.exceptionsr   r   r   mangum.typesr   r	   r
   Enumr   r   r   r   r   r   <module>   s    