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m  m	Z	 d dl
m  mZ d dlm  mZ d dlmZ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 d dlmZ ee Z!G d	d
 d
Z"e dkre j#ddZ$e$j%dde&dd e$j%dde&dd e$j%dde&dd e$j%dde'dd e$j%dde'dd e$j%dde'dd e$j%dde&ddd e$j%dd e'e	j(d!d e$j%d"de&dd#d e$j%d$d d%d& e	j)e	j*e	j+d' e$j%d(d e&e	j,e	j-d e$j%d)d e&ej.d*/ej.d e$j%d+de'd,d e$j%d-de'd.d e$j%d/de&dd0d e$j%d1de&dd2d e$j%d3de&dd4d e$j%d5d6d7d8 e$j%d9d6d:d8 e$j%d;d e&dd<d e$j%d=d e&d>d?d e$j%d@d e&d>dAd e$0 Z1znej2dBkrfe1j3nd Z4ej2dBkrqe1j5ndCZ6ee1j7e1j8e1j9e1j:e4e6dDZ!e;e1j<e1j=e4e6 e Z>e"e1j?e1j@e1jAe1jBe1jCe1jDe1jEe1j9e1jFe1jGe1jHe1jIe1jJe1jKe1jLdEZMdFdG ZNej2dBkre>OejPeN e>QeMR  W dS  eSy   e!TdH eUdC Y dS w dS )I    N)build_addressis_localhost)get_or_create_event_loop)logging_utils)create_check_raylet_task)AGENT_GRPC_MAX_MESSAGE_LENGTH)setup_component_logger)	GcsClientc                   @   sr   e Zd Zdddejdfdedededededed	efd
dZdd Zdd Z	e
dd ZdefddZdd ZdS )DashboardAgentNFdisable_metrics_collectionobject_store_nameraylet_namelog_dirtemp_dirsession_dirsession_namec                C   s   |dusJ || _ || _|dusJ || _|| _|| _|| _|| _|| _|| _|| _	|| _
|	| _|| _|| _tjd | _|
| _|| _d| _d| _t| j| jd| _| jsZ|   dS dS )z%Initialize the DashboardAgent object.NZRAY_NODE_ID)addressZ
cluster_id)ipminimalgcs_addresscluster_id_hexr   r   r   	grpc_portmetrics_export_portnode_manager_portevents_export_addrlisten_portr   r   osenvironnode_idZmetrics_collection_disabledr   serverhttp_serverr	   
gcs_client_init_non_minimal)selfnode_ip_addressr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r%   _/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/dashboard/agent.py__init__   s8   zDashboardAgent.__init__c                 C   s4  ddl m} ddlm} ddlm} tjjdkrBtjj	dkrBdd l
}|  |jdtd |  W d    n1 s<w   Y  n|  |jd	d
tfdtffd| _z|| jt| j| j t| jso|| jd| j  W n ty   td d | _d | _Y nw tdt| j| j || j| j| _d S )Nr   )aio)add_port_to_grpc_server)HttpServerAgent   
   ignore)category)zgrpc.so_reuseportr   zgrpc.max_send_message_lengthzgrpc.max_receive_message_length)optionsz
127.0.0.1:zVFailed to add port to grpc server. Agent will stay alive but disable the grpc service.z Dashboard agent grpc address: %s)Zgrpcr(   Zray._private.tls_utilsr)   Zray.dashboard.http_server_agentr*   sysversion_infomajorminorwarningscatch_warningssimplefilterDeprecationWarningZinit_grpc_aior   r   r   r   r   r   	Exceptionlogger	exceptioninfor   r    )r#   Zaiogrpcr)   r*   r4   r%   r%   r&   r"   R   sL   




z DashboardAgent._init_non_minimalc                 C   sR   g }t t j}|D ]}tdt jj| || }|| q
tdt| |S )zLoad dashboard agent modules.zLoading %s: %szLoaded %d modules.)dashboard_utilsZget_all_modulesZDashboardAgentModuler9   r;   __name__appendlen)r#   modulesZagent_cls_listclscr%   r%   r&   _load_modules   s   
zDashboardAgent._load_modulesc                 C   s   | j sJ d| j jS )Nz=Accessing unsupported API (HttpServerAgent) in a minimal ray.)r    http_sessionr#   r%   r%   r&   rD      s
   zDashboardAgent.http_sessionreturnc                 C   s   | j S N)r   rE   r%   r%   r&   get_node_id   s   zDashboardAgent.get_node_idc              
      s   j r j  I d H    }d} jr>z j|I d H  W n ty= } ztd| d d}W Y d }~nd }~ww |r jsEdn jj} j sNdn j} j	j
tj  j  t j||g dtjd} j	j
tj  j  t j||g dtjd}t||I d H   fdd|D }tjd	vr fd
d}	t j j	|	t}
||
  j r| j   n
dd }||  tj| I d H   jr߈ j I d H  d S d S )NTz,Failed to start HTTP server with exception: zB. The agent will stay alive but the HTTP service will be disabled.F)	namespacec                    s   g | ]}|  jqS r%   )runr   ).0mrE   r%   r&   
<listcomp>   s    z&DashboardAgent.run.<locals>.<listcomp>)win32cygwinc                    s$   t d j d j d|   d S )NzTerminated Raylet: ip=z
, node_id=z. )r9   r;   r   r   )msgrE   r%   r&   callback   s   z$DashboardAgent.run.<locals>.callbackc                      s   	 t dI d H  q)NTi  )asynciosleepr%   r%   r%   r&   wait_forever   s   z(DashboardAgent.run.<locals>.wait_forever)r   startrC   r    r8   r9   r:   	http_portr   r!   Zasync_internal_kv_putdashboard_constsZ#DASHBOARD_AGENT_ADDR_NODE_ID_PREFIXr   encodejsondumpsr   ray_constantsZKV_NAMESPACE_DASHBOARDZDASHBOARD_AGENT_ADDR_IP_PREFIXrS   gatherr0   platformr   r   loopr>   Zwait_for_terminationcleanup)r#   r@   Zlaunch_http_servererW   r   Zput_by_node_idZ	put_by_iptasksrR   Zcheck_parent_taskrU   r%   rE   r&   rK      s\   


zDashboardAgent.run)r=   
__module____qualname__r\   #DEFAULT_DASHBOARD_AGENT_LISTEN_PORTboolstrr'   r"   rC   propertyrD   rH   rK   r%   r%   r%   r&   r
      s6    
:;
r
   __main__zDashboard agent.)descriptionz--node-ip-addressTzthe IP address of this node.)requiredtypehelpz--gcs-addresszThe address (ip:port) of GCS.z--cluster-id-hexzThe cluster id in hex.z--metrics-export-portz.The port to expose metrics through Prometheus.z--grpc-portz9The port on which the dashboard agent will receive GRPCs.z--node-manager-portz-The port to use for starting the node managerz--object-store-namez#The socket name of the plasma store)rk   rl   defaultrm   z--listen-portFz!Port for HTTP server to listen onz--raylet-namez%The socket path of the raylet processz--logging-levelc                 C   s   t |  S rG   )logginggetLevelNameupper)sr%   r%   r&   <lambda>,  s    rs   )rk   rl   rn   choicesrm   z--logging-formatz--logging-filenamezJSpecify the name of log file, log to stdout if set empty, default is "{}".z--logging-rotate-bytesz,Specify the max bytes for rotating log file.z--logging-rotate-backup-countz-Specify the backup count of rotated log file.z	--log-dirz"Specify the path of log directory.z
--temp-dirz?Specify the path of the temporary directory use by Ray process.z--session-dirz!Specify the path of this session.z	--minimal
store_truezMinimal agent only contains a subset of features that don't require additional dependencies installed when ray is installed by `pip install 'ray[default]'`.)actionrm   z--disable-metrics-collectionzCIf this arg is set, metrics report won't be enabled from the agent.z--session-namezThe current Ray session name.z--stdout-filepath z,The filepath to dump dashboard agent stdout.z--stderr-filepathz,The filepath to dump dashboard agent stderr.rO      )logging_levellogging_formatr   filenameZ	max_bytesZbackup_count)
r   r   r   r   r   r   r   r   r   r   c                   C   s   t d ttj d S )Nz#Exiting with SIGTERM immediately...)r9   warningr   _exitsignalSIGTERMr%   r%   r%   r&   sigterm_handler  s   
r   z6Agent is working abnormally. It will exit immediately.)VargparserS   rZ   ro   r   r~   r0   Zray._private.ray_constantsZ_privater\   Zray.dashboard.constsZ	dashboardZconstsrX   Zray.dashboard.utilsutilsr<   Zray._common.network_utilsr   r   Zray._common.utilsr   Zray._privater   Zray._private.process_watcherr   r   Zray._private.ray_loggingr   Zray._rayletr	   	getLoggerr=   r9   r
   ArgumentParserparseradd_argumentrg   intre   ZLOGGER_LEVELZLOGGER_LEVEL_CHOICESZLOGGER_LEVEL_HELPZLOGGER_FORMATZLOGGER_FORMAT_HELPZDASHBOARD_AGENT_LOG_FILENAMEformat
parse_argsargsr^   Zlogging_rotate_bytesZlogging_rotation_bytesZlogging_rotate_backup_countZlogging_rotation_backup_country   rz   r   Zlogging_filenameZ redirect_stdout_stderr_if_neededZstdout_filepathZstderr_filepathr_   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   Zagentr   add_signal_handlerr   run_until_completerK   r8   r:   exitr%   r%   r%   r&   <module>   s   
 
[
	
	
 