o
    bit                     @   s  d dl Z d dlZd dlZd dl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 d dlmZ d dlmZ eeZdZdd Zded	efd
dZded	efddZde	eeef ef deddd	e	eeef ef fddZde	eeef ef ded	e	eeef ef fddZde	eeef ef ddd	e	eeef ef fddZ ded	ee fddZ!ded	ee fddZ"ded	ee fddZ#dS )     N)AnyCallableDictOptionalUnion)
load_class)
RuntimeEnvzray_runtime_env_func::c                   C   s   t tjtjdS )N60)intosenvirongetray_constants2RAY_WORKER_PROCESS_SETUP_HOOK_LOAD_TIMEOUT_ENV_VAR r   r   W/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/runtime_env/setup_hook.pyget_import_export_timeout   s
   r   keyreturnc                 C   s   t t|   S N)RUNTIME_ENV_FUNC_IDENTIFIERbase64	b64encodedecoder   r   r   r   _decode_function_key   s   r   c                 C   s$   |  tsJ t| ttd  S r   )
startswithr   r   	b64decodelenr   r   r   r   _encode_function_key   s   r   runtime_env
setup_funcworkerz
ray.Workerc              
   C   s   t |tsJ z|jj|t d}W n ty% } ztjd|d }~ww | 	di }t
j|vs:J dt
j dt||t
j< || d< |j| d< | S )Ntimeoutz$Failed to export the setup function.env_varsThe env var, ?, is not permitted because it is reserved for the internal use.worker_process_setup_hook)
isinstancer   function_actor_managerexport_setup_funcr   	Exceptionray
exceptionsRuntimeEnvSetupErrorr   r   !WORKER_PROCESS_SETUP_HOOK_ENV_VARr   __name__)r    r!   r"   r   er%   r   r   r   export_setup_func_callable$   s.   


r3   setup_func_modulec                 C   sL   t |tsJ | di }tj|vsJ dtj d||tj< || d< | S )Nr%   r&   r'   )r)   strr   r   r0   )r    r4   r%   r   r   r   export_setup_func_moduleC   s   
r6   c                 C   sT   |  d}|du r| S t|trt| ||S t|tr t| |S tdt| d)a  Uploads the worker_process_setup_hook to GCS with a key.

    runtime_env["worker_process_setup_hook"] is converted to a decoded key
    that can load the worker setup hook function from GCS.
    i.e., you can use internalKV.Get(runtime_env["worker_process_setup_hook])
    to access the worker setup hook from GCS.

    Args:
        runtime_env: The runtime_env. The value will be modified
            when returned.
        worker: ray.worker instance.
        decoder: GCS requires the function key to be bytes. However,
            we cannot json serialize (which is required to serialize
            runtime env) the bytes. So the key should be decoded to
            a string. The given decoder is used to decode the function
            key.
    r(   Nz2worker_process_setup_hook must be a function, got .)r   r)   r   r3   r5   r6   	TypeErrortype)r    r"   r!   r   r   r   *upload_worker_process_setup_hook_if_neededR   s   



r:   worker_process_setup_hook_keyc                 C   s&   | dusJ |  tst| S t| S )zLoad the setup hook from a given key and execute.

    Args:
        worker_process_setup_hook_key: The key to import the setup hook
            from GCS.
    Returns:
        An error message if it fails. None if it succeeds.
    N)r   r   "load_and_execute_setup_hook_module load_and_execute_setup_hook_func)r;   r   r   r   load_and_execute_setup_hookv   s   
r>   c              
   C   sF   z
t | }|  W d S  ty"   d|  d|  dt  }| Y S w )Nz)Failed to execute the setup hook method, z; from ``ray.init(runtime_env={'worker_process_setup_hook': z})``. Please make sure the given module exists and is available from ray workers. For more details, see the error trace below.
)r   r,   	traceback
format_exc)r;   r!   error_messager   r   r   r<      s   	r<   c                 C   s   t jjj}|js
J |j}z|jt| t d}W n t	y/   dt  dt
  }| Y S w zt|j}W n t	yJ   dt
  }| Y S w z|  W d S  t	yg   d|j dt
  }| Y S w )Nr#   z#Failed to import setup hook within z
 seconds.
z-Failed to deserialize the setup hook method.
z7Failed to execute the setup hook method. Function name:
)r-   _privater"   global_worker	connectedr*   fetch_registered_methodr   r   r,   r?   r@   pickleloadsfunctionfunction_name)r;   r"   func_managerworker_setup_func_inforA   r!   r   r   r   r=      sH   


	r=   )$r   loggingr   r?   typingr   r   r   r   r   r-   ray._private.ray_constantsrC   r   ray.cloudpicklecloudpicklerG   ray._common.utilsr   ray.runtime_envr   	getLoggerr1   loggerr   r   bytesr5   r   r   r3   r6   r:   r>   r<   r=   r   r   r   r   <module>   sj    



$

