o
    1 iW>                  0   @   sH  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mZm	Z	m
Z
mZmZ d dlZd dlm  mZ d dlmZmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZ d d	lm Z  d d
l!m"Z"m#Z# d dl$m%Z% 			dade
e& de'de
e& dee'e&f de#f
ddZ(de
e& de
e	e&ef  fddZ)dd Z*dd Z+de#de&de"fddZ,de#de&de"fdd Z-e.d!d"d# Z/e/0 ej1d$e&ddd%d&ej1d'e&ddd(d&ej1d)e&ddd*d&ej1d+e&ddd,d&ej1d-e&ddd.d&ej1d/e&ddd0d&ej1d1e&ddd2d&ej1d3de2d4d5ej1d6de2d7d5ej1d8de3d9d5ej1d:de&d;d5ej1d<de'dd=d>eeej4d?d@dej5dAe%de
e& de
e& dBe
e& dCe
e& dDe
e& dEe
e& dFe
e& d?ee& dGe
ee3e2f  dHe
ee3e2f  dIe
e3 dJe
e& dKe'dee'e&f de
e& fdLdMZ6e/0 ej1d$e&ddd%d&ej4dNe&dOeee%dPdQde
e& de&de
e& dee'e&f fdRdSZ7e/0 ej1d$e&ddd%d&ej1d<de'ddTd>ej4dNe&dOeee%dPdQde
e& dKe'de&de
e& dee'e&f f
dUdVZ8e/0 ej1d$e&ddd%d&ej4dNe&dOeee%dPdQde
e& de&de
e& dee'e&f fdWdXZ9e/0 ej1d$e&ddd%d&ej4dNe&dOej1dYdZde'dd[d>eee%dPdQde
e& de&d\e'de
e& dee'e&f f
d]d^Z:e/0 ej1d$e&ddd%d&eee%dPdQde
e& de
e& dee'e&f fd_d`Z;dS )b    N)list2cmdline)AnyDictOptionalTupleUnion)get_or_create_event_loop
load_class)parse_metadata_jsonparse_resources_json)add_click_logging_optionscf
cli_logger)parse_runtime_env_args)add_common_job_options)redact_url_password)	JobStatusJobSubmissionClient)	PublicAPIFTaddresscreate_cluster_if_neededheadersverifyreturnc                 C   s0   t | |t||d}| }tdt| |S )Nr   r   zJob submission server address)r   _handle_headersZget_addressr   Zlabeled_valuer   )r   r   r   r   clientclient_address r   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/dashboard/modules/job/cli.py_get_sdk_client   s   r    c              
   C   s^   | d u rdt jv rt jd } | d ur-zt| W S  ty, } ztd| |d }~ww d S )NZRAY_JOB_HEADERSzbFailed to parse headers into JSON.
                Expected format: {{"KEY": "VALUE"}}, got {}, {})osenvironjsonloads	Exception
ValueErrorformat)r   excr   r   r   r   /   s   
r   c                 C   B   t   t dt|   t |  t dt|   t   d S N-)r   newlinesuccesslenZsuccess_msgr   r   r   _log_big_success_msg?   
   
r0   c                 C   r)   r*   )r   r,   errorr.   r/   r   r   r   _log_big_error_msgG   r1   r3   r   job_idc                 C   s   |  |}|jtjkrtd| d |jS |jtjkr(td| d |jS |jtjkrIt	d| d |j
d urFtjd|j
 dd |jS td| d	|j  |j
d uretjd|j
 dd |jS )
NJob 'z' succeededz' was stoppedz' failedzStatus message: T	no_formatzStatus for job 'z': )Zget_job_infostatusr   	SUCCEEDEDr0   STOPPEDr   warningFAILEDr3   messageprint)r   r4   infor   r   r   _log_job_statusO   s    



r@   c                    s2   |  |2 z3 d H W }t|dd q6 t| |S )N )end)Ztail_job_logsr>   r@   )r   r4   linesr   r   r   
_tail_logsa   s
   
rD   Zjobc                   C   s   dS )z'Submit, stop, delete, or list Ray jobs.Nr   r   r   r   r   job_cli_grouph   s   rE   z	--addresszAddress of the Ray cluster to connect to. Can also be specified using the RAY_API_SERVER_ADDRESS environment variable (falls back to RAY_ADDRESS).)typedefaultrequiredhelpz--job-idz*DEPRECATED: Use `--submission-id` instead.z--submission-idzMSubmission ID to specify for the job. If not provided, one will be generated.z--runtime-envz>Path to a local YAML file containing a runtime_env definition.z--runtime-env-jsonz'JSON-serialized runtime_env dictionary.z--working-dirzDirectory containing files that your job will run in. Can be a local directory or a remote URI to a .zip file (S3, GS, HTTP). If specified, this overrides the option in `--runtime-env`.z--metadata-jsonz<JSON-serialized dictionary of metadata to attach to the job.z--entrypoint-num-cpusz|the quantity of CPU cores to reserve for the entrypoint command, separately from any tasks or actors that are launched by it)rH   rF   rI   z--entrypoint-num-gpuszwthe quantity of GPUs to reserve for the entrypoint command, separately from any tasks or actors that are launched by itz--entrypoint-memoryzwthe amount of memory to reserve for the entrypoint command, separately from any tasks or actors that are launched by itz--entrypoint-resourcesza JSON-serialized dictionary mapping resource name to resource quantity describing resources to reserve for the entrypoint command, separately from any tasks or actors that are launched by itz	--no-waitz:If set, will not stream logs and wait for the job to exit.)Zis_flagrF   rG   rI   
entrypoint)nargsrH   rF   submission_idruntime_envruntime_env_jsonmetadata_jsonworking_direntrypoint_num_cpusentrypoint_num_gpusentrypoint_memoryentrypoint_resourcesno_waitc                 C   sN  |rt d |durt|t tdd}|durt|t tdd}|p"|}tjtjv r@t	tjtj | |||||||||	|
||d t
| d||d}t|||d	}|jt||||||	|
|d
}td| d t dt t d t   t td|  W d   n1 sw   Y  t d t   t td|  W d   n1 sw   Y  t d t   t td|  W d   n1 sw   Y  W d   n1 sw   Y  t   t   | }|s%t|dkrt d t t||}|tjkrtd dS dS t d| d dS dS )a  Submits a job to be run on the cluster.

    By default (if --no-wait is not set), streams logs to stdout until the job finishes.
    If the job succeeded, exits with 0. If it failed, exits with 1.

    Example:
        `ray job submit -- python my_script.py --arg=val`
    zB--job-id option is deprecated. Please use --submission-id instead.Nzentrypoint-resources)Zcommand_argzmetadata-json)r   r4   rM   rN   rO   rP   rQ   rJ   rR   rS   rT   rU   rV   T)r   r   r   )rN   rO   rQ   )rJ   rM   rN   metadatarR   rS   rT   rU   r5   z' submitted successfullyz
Next stepszQuery the logs of the job:zray job logs zQuery the status of the job:zray job status zRequest the job to be stopped:zray job stop r   z:Tailing logs until the job exits (disable with --no-wait):   z7Tailing logs is not enabled for job sdk client version z<. Please upgrade Ray to the latest version for this feature.)r   r;   r   r   r
   ray_constantsZRAY_JOB_SUBMIT_HOOKr!   r"   r	   r    r   Z
submit_jobr   r0   groupr>   Zindentedboldr,   flushget_versionintr   run_until_completerD   r   r<   sysexit)r   r4   rM   rN   rO   rP   rQ   rJ   rR   rS   rT   rU   rV   r   r   r   Zfinal_runtime_envsdk_versionZ
job_statusr   r   r   submitn   s   ~





rc   zjob-id)rF   Zstable)Z	stabilityc                 C   s   t | ||d}t|| dS )z`Queries for the current status of a job.

    Example:
        `ray job status <my_job_id>`
    r   N)r    r@   r   r4   r   r   r   r   r   r   r8   H  s   r8   z*If set, will not wait for the job to exit.c                 C   s   t | ||d}td| d || |rdS td| d 	 ||}|tjtjtjhv r9t	|| dS td|  t
d	 q#)
zMAttempts to stop a job.

    Example:
        `ray job stop <my_job_id>`
    r   zAttempting to stop job ''NzWaiting for job 'z#' to exit (disable with --no-wait):Tz Job has not exited yet. Status: rX   )r    r   r>   Zstop_jobZget_job_statusr   r:   r9   r<   r@   timesleep)r   rV   r4   r   r   r   r8   r   r   r   stopf  s    "




rh   c                 C   s.   t | ||d}|| td| d dS )a  Deletes a stopped job and its associated data from memory.

    Only supported for jobs that are already in a terminal state.
    Fails with exit code 1 if the job is not already stopped.
    Does not delete job logs from disk.
    Submitting a job with the same submission ID as a previously
    deleted job is not supported and may lead to unexpected behavior.

    Example:
        ray job delete <my_job_id>
    r   r5   z' deleted successfullyN)r    Z
delete_jobr   r>   rd   r   r   r   delete  s    
ri   z-fz--followz)If set, follow the logs (like `tail -f`).followc                 C   sl   t | ||d}| }|r)t|dkrt t|| dS td| d dS tj|	|ddd dS )	zMGets the logs of a job.

    Example:
        `ray job logs <my_job_id>`
    r   r   z<Tailing logs is not enabled for the Jobs SDK client version z8. Please upgrade Ray to latest version for this feature.rA   T)rB   r7   N)
r    r]   r^   r   r_   rD   r   r;   r>   Zget_job_logs)r   r4   rj   r   r   r   rb   r   r   r   logs  s   #rk   c                 C   s*   t | ||d}tjt| dd dS )zWLists all running jobs and their information.

    Example:
        `ray job list`
    r   Tr6   N)r    r   r>   pprintpformatZ	list_jobs)r   r   r   r   r   r   r   list  s   rn   )FNT)<r#   r!   rl   r`   rf   
subprocessr   typingr   r   r   r   r   ZclickZray._private.ray_constantsZ_privaterY   Zray._common.utilsr   r	   Zray._private.utilsr
   r   Z"ray.autoscaler._private.cli_loggerr   r   r   Z#ray.dashboard.modules.dashboard_sdkr   Z#ray.dashboard.modules.job.cli_utilsr   Zray.dashboard.modules.job.utilsr   Zray.job_submissionr   r   Zray.util.annotationsr   strboolr    r   r0   r3   r@   rD   rZ   rE   commandoptionfloatr^   argumentZUNPROCESSEDrc   r8   rh   ri   rk   rn   r   r   r   r   <module>   s   

"


	

,u



!




4