
    &`i_#                        d 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Z e
dd          Zdefd	Zefd
ZefdefdZdee         dee         fdZ ej        ddddd           ej        dded           ej        ded           ej        ddej                  d                                                 Zedk    r e             dS dS )zSymmetric Run for Ray.    N)List)env_integer)	GcsClient&RAY_SYMMETRIC_RUN_CLUSTER_WAIT_TIMEOUT   returnc                  b    dd l mc m}  |                                 }t	          |          dk    S )Nr   )ray._private.services_privateservicesfind_gcs_addresseslen)r   running_gcs_addressess     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/scripts/symmetric_run.pycheck_ray_already_startedr      sE    ,,,,,,,,, %7799$%%))    c                 p   t          j                     }d}t          j        d           t          j                     |z
  |k     rqt          j        d           t	          t          j                              }|| k    rdS t          j        d| d|  d           t          j                     |z
  |k     qdS )	z\Wait for all nodes to start.

    Raises an exception if the nodes don't start in time.
       T)ignore_reinit_error   zWaiting for nodes to start... /z nodes startedF)timerayinitsleepr   nodesclickecho)nnodestimeout
start_timecurrent_nodess       r   check_cluster_readyr#      s    
 JMH&&&&
)++

"W
,
,
1CIKK((F""4JWWWWWW   )++

"W
,
, 5r   addressc                 6   t          j                     }t          |           }t          j                     |z
  |k     r[|                    g d          rt          j        d           dS t          j        d           t          j                     |z
  |k     [dS )N)r$   r   )r    zRay cluster is ready!Tr   F)r   r   check_aliver   r   r   )r$   r    r!   
gcs_clients       r   check_head_node_readyr(   1   s    J7+++J
)++

"W
,
,!!"a!00 	J.///4
1	 )++

"W
,
,
 5r   run_and_start_argsc                 N   t                               d| d          }t          |j        d                   }|D ]j}|dk    rt	          j        d          |dk    rt	          j        d          |d	k    rt	          j        d
          |dk    rt	          j        d          k|S )N_T)resilient_parsingray_args_and_entrypoint--headz*Cannot use --head option in symmetric_run.z--node-ip-addressz5Cannot use --node-ip-address option in symmetric_run.z--portz*Cannot use --port option in symmetric_run.--blockz+Cannot use --block option in symmetric_run.)symmetric_runmake_contextlistparamsr   ClickException)r)   ctxcleaned_argsargs       r   "curate_and_validate_ray_start_argsr8   <   s    

$
$S*<PT
$
U
UC
#<=>>L 
V 
V(??&'STTT%%%&G   (??&'STTT)&'TUUU  r   r0   T)ignore_unknown_optionsallow_extra_argsaQ  Command to start Ray across all nodes and execute an entrypoint command.

USAGE:

    ray symmetric-run --address ADDRESS
[--min-nodes NUM_NODES] [RAY_START_OPTIONS] -- [ENTRYPOINT_COMMAND]

DESCRIPTION:

    This command (1) starts a Ray cluster across all nodes,
(2) runs a command on the head node, and (3) stops the Ray cluster.

    The '--' separator is required to distinguish between Ray start arguments
and the entrypoint command. The --min-nodes option is optional and
can be used to wait for a specific number of nodes to start.

EXAMPLES:

    # Start Ray with default settings and run a Python script

    ray symmetric-run --address 127.0.0.1:6379 -- python my_script.py

    # Start Ray with specific head node and run a command

    ray symmetric-run --address 127.0.0.1:6379 --min-nodes 4 -- python train_model.py --epochs=100

    # Start Ray and run a multi-word command

    ray symmetric-run --address 127.0.0.1:6379 --min-nodes 4 --num-cpus=4 -- python -m my_module --config=prod

RAY START OPTIONS:

    Most ray start command options are supported. Arguments that are not
supported are: --head, --node-ip-address, --port, --block.

SEPARATOR REQUIREMENT:

    The '--' separator is mandatory and must appear between Ray start
    arguments and the entrypoint command. This ensures clear separation
    between the two sets of arguments.
)namecontext_settingshelp	--addresszThe address of the Ray cluster.)requiredtyper=   z--min-nodesz4If provided, wait for this number of nodes to start.)r@   r=   r-   )nargsr@   c                 	   t           j        dd          }|r|d         dk    r
|dd          }	 |                    d          }n"# t          $ r t	          j        d          w xY w|d |         ||dz   d          }}t          |          }|dn|}|st	          j        d          t                      rt	          j        d          t          j	        j
                            |           }|t	          j        d|  d	          |\  }	}
	 t          j        |	|
t          j        t          j                  }|d         d
         d         }n*# t          j        $ r t	          j        d|	           w xY wg }t#          j                                                    D ]M\  }}|D ]E}|j        t          j        j        t          j        j        fv r|                    |j                   FN|dk    rd |D             }||v }d }	 |rt	          j        d           dddd| d|
 g|}t7          j        |dd           t	          j        d           t	          j        d           |dk    r#t;          |          st	          j        d          t	          j        d|            t	          j        d           t7          j        |          }t	          j        d           nZt	          j        d|  d           t=          |           st	          j        d          ddd| dg|}t7          j        |d           n# t6          j        $ r}t	          j        d| d            |j         r0t	          j        d!|j         !                                 d            |j"        r0t	          j        d"|j"        !                                 d            Y d }~n)d }~wtF          $ r t	          j        d#d            Y nw xY wt7          j        dd$g           |D|j$        dk    r;t	          j        d%|j$         d            t          j%        |j$                   d S d S d S # t7          j        dd$g           |C|j$        dk    r9t	          j        d%|j$         d            t          j%        |j$                   w w w xY w)&Nr   r   zsymmetric-runz--zqNo separator '--' found in arguments. Please use '--' to separate Ray start arguments and the entrypoint command.zNo entrypoint command provided.z$Ray is already started on this node.zInvalid address format: z, should be `host:port`   zCould not resolve hostname: c                 *    g | ]}|d k    |dk    |S )z	127.0.0.1z::1 ).0ips     r   
<listcomp>z!symmetric_run.<locals>.<listcomp>   s'    KKK{):):rU{{"{{{r   z*On head node. Starting Ray cluster head...r   startr.   z--node-ip-address=z--port=T)checkcapture_outputzHead node started.z=======================z+Timed out waiting for other nodes to start.zRunning command on head node: z-On worker node. Connecting to Ray cluster at z...z)Timed out waiting for head node to start.r>   r/   )rK   zFailed to start Ray: )errzstdout:
zstderr:
zInterrupted by user.stopz Command failed with return code )&sysargvindex
ValueErrorr   r4   r8   r   r   _commonnetwork_utilsparse_addresssocketgetaddrinfo	AF_UNSPECSOCK_STREAMgaierrorpsutilnet_if_addrsitemsfamilyAddressFamilyAF_INETAF_INET6appendr$   r   
subprocessrunr#   r(   CalledProcessErrorstdoutdecodestderrKeyboardInterrupt
returncodeexit)r$   	min_nodesr-   all_args	separatorr)   entrypoint_on_headray_start_argsgcs_host_portgcs_hostgcs_portaddrinforesolved_gcs_hostmy_ipsifaceaddrsaddris_headresultray_start_cmdes                        r   r0   r0   P   s   n x|H  HQK?22ABB<
NN4((		 
 
 
"G
 
 	

 	)Q! +
 88JKKN&II F"#DEEE "" K"#IJJJ K-;;GDDM"GwGGG
 
 	
 'HhN%h 0&2D
 
 %QKN1-? N N N"#L(#L#LMMMN F+--3355 , ,u 	, 	,D{$,$-   dl+++	, 1}} LKvKKK6)GFB( /	6JCDDD 8%688$($$  M N=TJJJJJ+,,,J01111}}%8%C%C}*A   JE1CEE   J0111^$677FJ01111 JSwSSSTTT(11 X*+VWWW   M N=5555( B B B
.1..D99998 	BJ618??#4#466DAAAA8 	BJ618??#4#466DAAAA 5 5 5
)t4444445
 	v''' &"3q"8"8JM&:KMMSWXXXXHV&''''' "8"8 	v''' &"3q"8"8JM&:KMMSWXXXXHV&'''' "8sV   A A#?E 'E5D;M Q& PBOQ& #P Q& PQ& &AS__main__)__doc__rV   rc   rO   r   typingr   r   r   ray._private.ray_constantsr   ray._rayletr   r[   CLUSTER_WAIT_TIMEOUTboolr   r#   strr(   r8   commandoptionintargumentUNPROCESSEDr0   __name__rF   r   r   <module>r      s          



         



 2 2 2 2 2 2 ! ! ! ! ! ! "{#KRPP *4 * * * * )=    * 1E  3    49 c    ( 	04$OO(
, , ,Z $S/P   		?  
 )%:KLLLC( C( ML  [, ,lC(L zMOOOOO r   