o
    ;i                     @   s   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mZmZm	Z	 ddl
mZ ddlmZ eeZdZdZee ZG dd	 d	e jZG d
d deZG dd deZdS )    N)OptionalSetCallable	Awaitable   )DeadlineWrapper)Deadline   
   c                   @   sd   e Zd ZejdefddZejdefddZejdej	fddZ
ejdej	dd	fd
dZd	S )	CheckBasereturnc                 C   s   d S N selfr   r   H/home/ubuntu/.local/lib/python3.10/site-packages/grpclib/health/check.py
__status__   s   zCheckBase.__status__c                       d S r   r   r   r   r   r   	__check__      zCheckBase.__check__c                    r   r   r   r   r   r   r   __subscribe__   r   zCheckBase.__subscribe__eventNc                    r   r   r   r   r   r   r   r   __unsubscribe__#   r   zCheckBase.__unsubscribe__)__name__
__module____qualname__abcabstractmethod_Statusr   r   asyncioEventr   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdZdZdZdeedde	g e
e f deej dededdf
d	d
ZdefddZdefddZdddZdejfddZdejddfddZdS )ServiceCheckaG  Performs periodic checks

    Example:

    .. code-block:: python3

        async def db_test():
            # raised exceptions are the same as returning False,
            # except that exceptions will be logged
            await db.execute('SELECT 1;')
            return True

        db_check = ServiceCheck(db_test)
    N)loop	check_ttlcheck_timeoutfuncr#   r$   r%   r   c                C   sN   || _ || _|| _t | _|rtjdtdd t	 | _
| j
  t | _dS )a  
        :param func: callable object which returns awaitable object, where
            result is one of: ``True`` (healthy), ``False`` (unhealthy), or
            ``None`` (unknown)
        :param loop: (deprecated) asyncio-compatible event loop
        :param check_ttl: how long we can cache result of the previous check
        :param check_timeout: timeout for this check
        HThe loop argument is deprecated and scheduled for removal in grpclib 0.5r   
stacklevelN)_func
_check_ttl_check_timeoutset_eventswarningswarnDeprecationWarningr    r!   _check_lockr   _check_wrapper)r   r&   r#   r$   r%   r   r   r   __init__;   s   

zServiceCheck.__init__c                 C      | j S r   _valuer   r   r   r   r   [      zServiceCheck.__status__c                    sf  | j d urt | j  | jk r| jS | j s#| j I d H  | jS | j}| j  zYz8t	
| j}| j| |  I d H }W d    n1 sKw   Y  |d ur`t|ts`td||| _W n tjym     ty}   td d| _Y nw W | j  n| j  w t | _ | j|kr| jrtjntj}|d| j| j | jD ]}|  q| jS )NzInvalid status type: {!r}zHealth check failedFz$Health check %r status changed to %r)_last_checktime	monotonicr+   r7   r2   is_setwaitclearr   from_timeoutr,   r3   startr*   
isinstancebool	TypeErrorformatr    CancelledError	Exceptionlog	exceptionr-   infowarningr.   )r   
prev_valuedeadlinevalue	log_levelr   r   r   r   r   ^   sD   









zServiceCheck.__check__c                    s>   	 |   I d H }|rt| jI d H  n	t| jI d H  qr   )r   r    sleepr+   )r   statusr   r   r   _poll   s   zServiceCheck._pollc                    s<   | j d u rt }||  | _ t }| j| |S r   )
_poll_taskr    get_event_loopcreate_taskrQ   r!   r.   add)r   r#   r   r   r   r   r      s   
zServiceCheck.__subscribe__r   c                    sb   | j | | j s/| jd usJ | j}d | _|  z|I d H  W d S  tjy.   Y d S w d S r   )r.   discardrR   cancelr    rE   )r   r   taskr   r   r   r      s   zServiceCheck.__unsubscribe__)r   N)r   r   r   __doc__r7   rR   r9   DEFAULT_CHECK_TTLDEFAULT_CHECK_TIMEOUTr   r   r   r   r    AbstractEventLoopfloatr4   r   r   rQ   r!   r   r   r   r   r   r   r"   (   s0    
 
'	r"   c                   @   s   e Zd ZdZdddeej ddfddZdeddfd	d
Z	defddZ
defddZdejfddZdejddfddZdS )ServiceStatusa  Contains status of a proactive check

    Example:

    .. code-block:: python3

        redis_status = ServiceStatus()

        # detected that Redis is available
        redis_status.set(True)

        # detected that Redis is unavailable
        redis_status.set(False)
    N)r#   r#   r   c                C   s&   |r
t jdtdd d| _t | _dS )zI
        :param loop: (deprecated) asyncio-compatible event loop
        r'   r   r(   N)r/   r0   r1   r7   r-   r.   )r   r#   r   r   r   r4      s   zServiceStatus.__init__rM   c                 C   s2   | j }|| _ | j |kr| jD ]}|  qdS dS )zSets current status of a check

        :param value: ``True`` (healthy), ``False`` (unhealthy), or ``None``
            (unknown)
        N)r7   r.   r-   )r   rM   rK   r   r   r   r   r-      s   


zServiceStatus.setc                 C   r5   r   r6   r   r   r   r   r      r8   zServiceStatus.__status__c                    s   | j S r   r6   r   r   r   r   r      s   zServiceStatus.__check__c                    s   t  }| j| |S r   )r    r!   r.   rU   r   r   r   r   r      s   zServiceStatus.__subscribe__r   c                    s   | j | d S r   )r.   rV   r   r   r   r   r      s   zServiceStatus.__unsubscribe__)r   r   r   rY   r   r    r\   r4   r   r-   r   r   r!   r   r   r   r   r   r   r^      s    
r^   )r   r:   r    loggingr/   typingr   r   r   r   utilsr   metadatar   	getLoggerr   rG   rZ   r[   rB   r   ABCr   r"   r^   r   r   r   r   <module>   s    
|