
    &`it                        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c mZ d dlmZ d dlmZ d dlmZ  ej        e          ZdZd Zdedefd	Zdedefd
Zde	eeef         ef         deddde	eeef         ef         fdZde	eeef         ef         dede	eeef         ef         fdZde	eeef         ef         ddde	eeef         ef         fdZ dedee         fdZ!dedee         fdZ"dedee         fdZ#dS )    N)AnyCallableDictOptionalUnion)
load_class)
RuntimeEnvzray_runtime_env_func::c                  p    t          t          j                            t          j        d                    S )N60)intosenvirongetray_constants2RAY_WORKER_PROCESS_SETUP_HOOK_LOAD_TIMEOUT_ENV_VAR     w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/setup_hook.pyget_import_export_timeoutr      s/    

Ld	
 	
  r   keyreturnc                 ^    t           t          j        |                                           z   S N)RUNTIME_ENV_FUNC_IDENTIFIERbase64	b64encodedecoder   s    r   _decode_function_keyr      s$    &)9#)>)>)E)E)G)GGGr   c                     |                      t                    sJ t          j        | t	          t                    d                    S r   )
startswithr   r   	b64decodelenr   s    r   _encode_function_keyr$      s@    >>566666C$? @ @ B BCDDDr   runtime_env
setup_funcworkerz
ray.Workerc                    t          |t                    sJ 	 |j                            |t	                                }n2# t
          $ r%}t          j                            d          |d }~ww xY 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+   s         r   export_setup_func_callabler9   $   s   
 j(++++++== 9 ; ; > 
 
    n112
 
	 z2..H:(JJJ	HG 	H 	H 	H KJJ AUA AH]<= 'K

 0:/BK+,s   (A 
A1 A,,A1setup_func_modulec                     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   r6   )r%   r:   r+   s      r   export_setup_func_moduler=   C   s     '-----z2..H:(JJJ	HG 	H 	H 	H KJJ ARH]<=&K
r   c                 
   |                      d          }|| 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   r9   r<   r=   	TypeErrortype)r%   r'   r&   s      r   *upload_worker_process_setup_hook_if_neededrB   R   s    * !<==J*h'' 
)+z6JJJ	J	$	$ 
'Z@@@WDDTDTWWW
 
 	
r   worker_process_setup_hook_keyc                 z    | 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.
    )r!   r   "load_and_execute_setup_hook_module load_and_execute_setup_hook_func)rC   s    r   load_and_execute_setup_hookrG   v   sF     )444(334OPP O12OPPP/0MNNNr   c                     	 t          |           } |             d S # t          $ r! d|  d|  dt          j                     }|cY S w xY 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   r2   	traceback
format_exc)rC   r&   error_messages      r   rE   rE      s     =>>

t 
 
 
(,( ( /L( ( #%%( ( 	 
s    (AAc                 "   t           j        j        j        }|j        sJ |j        }	 |                    t          |           t                                }n7# t          $ r* dt                       dt          j                     }|cY S w xY w	 t          j        |j                  }n(# t          $ r dt          j                     }|cY S w xY w	  |             n0# t          $ r# d|j         dt          j                     }|cY S w xY wd S )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:
)r3   _privater'   global_worker	connectedr0   fetch_registered_methodr$   r   r2   rI   rJ   pickleloadsfunctionfunction_name)rC   r'   func_managerworker_setup_func_inforK   r&   s         r   rF   rF      s    \ .F0L!-!E!E !>??-// "F "
 "
    ((**( (#%%( ( 	
 \"8"ABB

   X	@T@V@VXX 	 	
   (%3( (#%%( ( 	
  4s5   0A 1BBB+ +"CC
C *DD)$r   loggingr   rI   typingr   r   r   r   r   r3   ray._private.ray_constantsrN   r   ray.cloudpicklecloudpicklerR   ray._common.utilsr   ray.runtime_envr	   	getLoggerr7   loggerr   r   bytesr<   r   r$   r9   r=   rB   rG   rE   rF   r   r   r   <module>rb      s     				     7 7 7 7 7 7 7 7 7 7 7 7 7 7 



 2 2 2 2 2 2 2 2 2             ( ( ( ( ( ( & & & & & &		8	$	$6   He H H H H H
Ec Ee E E E E
tCH~z12  4S>:%&	   >tCH~z12 4S>:%&   !
tCH~z12!
!
 4S>:%&!
 !
 !
 !
HO#&Oc]O O O O$#&c]   (%#&%c]% % % % % %r   