o
    پin                     @   sX   d Z ddlZddlZddlmZ ddlZddlmZmZ e	e
ZG dd dejZdS )z
Standard gRPC health check service implementation for Kubernetes probes.

This module implements the grpc.health.v1.Health service protocol, enabling
native Kubernetes gRPC health probes for liveness and readiness checks.
    N)AsyncIterator)
health_pb2health_pb2_grpcc                   @   sv   e Zd ZdZdZdZdefddZdd Zd	d
 Z	de
jdejjde
jfddZde
jdejjdee
j fddZdS )SGLangHealthServicera  
    Standard gRPC health check service implementation for Kubernetes probes.
    Implements grpc.health.v1.Health protocol.

    Supports two service levels:
    1. Overall server health (service="") - for liveness probes
    2. SGLang service health (service="sglang.grpc.scheduler.SglangScheduler") - for readiness probes

    Health status lifecycle:
    - NOT_SERVING: Initial state, model loading, or shutting down
    - SERVING: Model loaded and ready to serve requests
     z%sglang.grpc.scheduler.SglangSchedulerscheduler_infoc                 C   s@   || _ || _i | _tjj| j| j< tjj| j| j< t	d dS )z
        Initialize health servicer.

        Args:
            request_manager: GrpcRequestManager instance for checking server state
            scheduler_info: Dict containing scheduler metadata
        z(Standard gRPC health service initializedN)
request_managerr   _serving_statusr   HealthCheckResponseNOT_SERVINGOVERALL_SERVERSGLANG_SERVICEloggerinfo)selfr   r    r   S/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/grpc/health_servicer.py__init__$   s   

zSGLangHealthServicer.__init__c                 C   .   t jj| j| j< t jj| j| j< td dS )z;Mark services as SERVING - call this after model is loaded.z$Health service status set to SERVINGN)r   r
   SERVINGr	   r   r   r   r   r   r   r   r   set_serving:   
   

z SGLangHealthServicer.set_servingc                 C   r   )z9Mark services as NOT_SERVING - call this during shutdown.z(Health service status set to NOT_SERVINGN)r   r
   r   r	   r   r   r   r   r   r   r   r   set_not_servingD   r   z$SGLangHealthServicer.set_not_servingrequestcontextreturnc                    st  |j }td| d | jjrtd tjtjjdS || jkr@| j	
| jtjj}tdtjj|  tj|dS || jkr| j	
| jtjj}|tjjkr`td tj|dS t | jj }|dkrt| jjdkrtd	|d
dt| jj d tjtjjdS td tjtjjdS td| d |tjj |d|  tjtjjdS )a"  
        Standard health check for Kubernetes probes.

        Args:
            request: Contains service name ("" for overall, or specific service)
            context: gRPC context

        Returns:
            HealthCheckResponse with SERVING/NOT_SERVING/SERVICE_UNKNOWN status
        z#Health check request for service: ''z%Health check: Server is shutting down)statuszOverall health check: z/Service health check: NOT_SERVING (base status)   r   z0Service health check: Scheduler not responsive (z.1fzs since last receive, z pending requests)zService health check: SERVINGz#Health check for unknown service: 'zUnknown service: )servicer   debugr   gracefully_exitr   r
   r   r   r	   getServingStatusNamer   r   timelast_receive_tstamplenrid_to_statewarningset_codegrpc
StatusCode	NOT_FOUNDset_detailsSERVICE_UNKNOWN)r   r   r   service_namer   base_statustime_since_last_receiver   r   r   CheckN   s^   








zSGLangHealthServicer.Checkc                 C  s6   |j }td| d | ||I dH }|V  dS )a  
        Streaming health check - sends updates when status changes.

        For now, just send current status once (Kubernetes doesn't use Watch).
        A full implementation would monitor status changes and stream updates.

        Args:
            request: Contains service name
            context: gRPC context

        Yields:
            HealthCheckResponse messages when status changes
        z#Health watch request for service: 'r   N)r    r   r!   r4   )r   r   r   r1   responser   r   r   Watch   s
   
zSGLangHealthServicer.WatchN)__name__
__module____qualname____doc__r   r   dictr   r   r   r   HealthCheckRequestr,   aioServicerContextr
   r4   r   r6   r   r   r   r   r      s*    


Ur   )r:   loggingr&   typingr   r,   grpc_health.v1r   r   	getLoggerr7   r   HealthServicerr   r   r   r   r   <module>   s    
