o
    1 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Zd dlZd dlZd dlm	Z	 d dl
Z
d dl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mZmZ eeZe	G d	d
 d
ZG dd de jZdee dede dej!j"fddZ#dee dede dej!j"fddZ$dS )    N)	dataclass)ray_constants)logging_utils)
GcsChannel)setup_component_logger)	GcsClient)get_named_pipe_pathget_socket_pathmodule_logging_filenamec                   @   sr   e Zd ZU dZeed< eed< eed< eed< eed< eed< eed< eed	< eed
< eed< eed< eed< dS )SubprocessModuleConfigz?
    Configuration for a SubprocessModule.
    Pickleable.
    cluster_id_hexgcs_addresssession_nametemp_dirsession_dirlogging_levellogging_formatlog_dirlogging_filenamelogging_rotate_byteslogging_rotate_backup_count
socket_dirN)__name__
__module____qualname____doc__str__annotations__int r   r   m/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/dashboard/subprocesses/module.pyr      s   
 r   c                   @   s   e Zd ZdZdefddZdd Zedd Zd	d
 Z	e
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Zdd ZdS )SubprocessModulez[
    A Dashboard Head Module that runs in a subprocess as a standalone aiohttp server.
    configc                 C   s,   || _ t | _d| _d| _d| _d| _dS )z
        Initialize current module when DashboardHead loading modules.
        :param dashboard_head: The DashboardHead instance.
        N)_configmultiprocessingZparent_process_parent_process_gcs_client_aiogrpc_gcs_channel$_parent_process_death_detection_task_http_session)selfr"   r   r   r    __init__<   s   

zSubprocessModule.__init__c                    s:   	 | j  std| j j d dS tdI dH  q)z[
        Detect parent process liveness. Only returns when parent process is dead.
        TzParent process z died. Exiting...N   )r%   is_aliveloggerwarningpidasynciosleepr*   r   r   r    _detect_parent_process_deathL   s   
z-SubprocessModule._detect_parent_process_deathc                   C   s   dS )a  
        Currently all SubprocessModule classes should be non-minimal.

        We require this because SubprocessModuleHandle tracks aiohttp requests and
        responses. To ease this, we can define another SubprocessModuleMinimalHandle
        that doesn't track requests and responses, but still provides Queue interface
        and health check.
        TODO(ryw): If needed, create SubprocessModuleMinimalHandle.
        Fr   r   r   r   r    is_minimal_moduleX   s   z"SubprocessModule.is_minimal_modulec                    s  t jjtjd}t jd| jg}t| dd }|D ]\}}|	t j
|j|j| q|| t jj|dd}| I dH  | jj}tjdkrct|| jj}t j||}	td| d	 nt| jj|}
t j||
}	td|
 d	 |	 I dH  dS )
z
        Start running the module.
        This method should be called first before the module starts receiving requests.
        )Zclient_max_sizez/api/healthzc                 S   s   t | ot| dot| dS )N__route_method____route_path__)inspectismethodhasattr)xr   r   r    <lambda>r   s   

z&SubprocessModule.run.<locals>.<lambda>N)Z
access_logwin32zStarted aiohttp server over .)aiohttpwebApplicationr   ZDASHBOARD_CLIENT_MAX_SIZEget_internal_module_health_checkr8   
getmembersappendZrouter6   r7   Z
add_routesZ	AppRunnersetup	__class__r   sysplatformr   r#   r   ZNamedPipeSiter.   infor	   r   ZUnixSitestart)r*   appZrouteshandlers_handlerrunnermodule_nameZnamed_pipe_pathsiteZsocket_pathr   r   r    rune   s@   

zSubprocessModule.runc                 C   sL   | j d u r#tjj st| jj| jjd}tjj	| tjj
 | _ | j S )N)addressZ
cluster_id)r&   rayZexperimentalZinternal_kvZ_internal_kv_initializedr   r#   r   r   Z_initialize_internal_kvZinternal_kv_get_gcs_client)r*   
gcs_clientr   r   r    rV      s   
zSubprocessModule.gcs_clientc                 C   s2   | j d u rt| jjdd}|  | | _ | j S )NT)r   Zaio)r'   r   r#   r   connectZchannel)r*   Zgcs_channelr   r   r    aiogrpc_gcs_channel   s
   

z$SubprocessModule.aiogrpc_gcs_channelc                 C      | j jS )zW
        Return the Ray session name. It's not related to the aiohttp session.
        )r#   r   r3   r   r   r    r      s   zSubprocessModule.session_namec                 C   rY   N)r#   r   r3   r   r   r    r         zSubprocessModule.temp_dirc                 C   rY   rZ   )r#   r   r3   r   r   r    r      r[   zSubprocessModule.session_dirc                 C   rY   rZ   )r#   r   r3   r   r   r    r      r[   zSubprocessModule.log_dirc                 C   s   | j d u r
t | _ | j S rZ   )r)   r?   ZClientSessionr3   r   r   r    http_session   s   

zSubprocessModule.http_sessionc                 C   rY   rZ   )r#   r   r3   r   r   r    r      r[   zSubprocessModule.gcs_addressc                    s   t jjdddS )Nsuccesszapplication/text)textcontent_type)r?   r@   Response)r*   requestr   r   r    rC      s
   z.SubprocessModule._internal_module_health_checkN)r   r   r   r   r   r+   r4   staticmethodr5   rS   propertyrV   rX   r   r   r   r   r\   r   rC   r   r   r   r    r!   7   s4    

,







r!   clsr"   incarnation
child_connc              
      s   | j }td| d| d|  z0| |}t| |_|jdd  | I d H  |	d  |
  td| d W d S  tyX } z
td|  |d }~ww )	NzStarting module z with incarnation z and config c                 S   s   t  S rZ   )rH   exit)rN   r   r   r    r<      s    z"run_module_inner.<locals>.<lambda>zModule z# initialized, receiving messages...zError creating module )r   r.   rJ   r1   create_taskr4   r(   add_done_callbackrS   sendclose	Exception	exception)rd   r"   re   rf   rQ   moduleer   r   r    run_module_inner   s,   
rp   c              	   C   s   | j }tj }tjd| d| d| d t||j}t|j|j	|j
||j|jd |jrVt||jdd}t||jdd}ttj|j
|tj|j
||j|j t }	|	t| |||}
d	d
 }tjj| |	|
 |	  dS )z-
    Entrypoint for a subprocess module.
    zray-dashboard--z ())r   r   r   filenameZ	max_bytesZbackup_countz.out)	extensionz.errc                 S   s    t d|  d t|  d S )NzExiting with signal z immediately...)r.   r/   rH   rg   )signumframer   r   r    sigterm_handler  s   z#run_module.<locals>.sigterm_handlerN)r   rU   Z_rayletZgetproctitleZsetproctitler
   r   r   r   r   r   r   r   r   Z redirect_stdout_stderr_if_neededospathjoinr1   new_event_looprh   rp   Z_privateutilsZset_sigterm_handlerrun_until_completerun_forever)rd   r"   re   rf   rQ   Zcurrent_proctitler   Zstdout_filenameZstderr_filenamelooptaskrw   r   r   r    
run_module   sP   	
	
r   )%abcr1   r8   loggingr$   Zmultiprocessing.connectionrx   rH   dataclassesr   r?   rU   r   Zray._privater   Zray._private.gcs_utilsr   Zray._private.ray_loggingr   Zray._rayletr   Z ray.dashboard.subprocesses.utilsr   r	   r
   	getLoggerr   r.   r   ABCr!   typer   
connection
Connectionrp   r   r   r   r   r    <module>   sP    
 
