o
    $i                     @   sT   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 G dd deZ
dS )    N)aiohttp)BaseRouteTable)SubprocessModuleHandle)ResponseTypec                   @   sJ   e Zd ZdZeeZej	
 Zed
ddZeejfdefddZd	S )SubprocessRouteTablea  
    A route table to bind http route to SubprocessModuleHandle and SubprocessModule.

    This class is used in cls object: all the decorator methods are @classmethod, and
    the routes are binded to the cls object.

    Note we have 2 handlers:
    1. the child side handler, that is `handler` that contains real logic and
        is executed in the child process. It's added with __route_method__ and
        __route_path__ attributes. The child process runs a standalone aiohttp
        server.
    2. the parent side handler, that just sends the request to the
        SubprocessModuleHandle at cls._bind_map[method][path].instance.

    With modifications:
    - __route_method__ and __route_path__ are added to both side's handlers.
    - method and path are added to self._bind_map.

    Lifecycle of a request:
    1. Parent receives an aiohttp request.
    2. Router finds by [method][path] and calls parent_side_handler.
    3. `parent_side_handler` bookkeeps the request with a Future and sends a
            request to the subprocess.
    4. Subprocesses receives the response and sends it back to the parent.
    5. Parent responds to the aiohttp request with the response from the subprocess.
    instancer   c                 C   s<   dd }t |j|}|D ]\}}|| j|j |j _qd S )Nc                 S   s"   t | rt| dot| dS dS )N__route_method____route_path__F)inspect
isfunctionhasattr)o r   ^/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/dashboard/subprocesses/routes.py	predicate-   s   
z,SubprocessRouteTable.bind.<locals>.predicate)r
   
getmembers
module_cls	_bind_mapr   r	   r   )clsr   r   handler_routes_hr   r   r   bind)   s
   zSubprocessRouteTable.bind	resp_typec                    s    fdd}|S )zR
        Register a route to the module and return the decorated handler.
        c                    s    j  v r j   }td d|j d|j  | jj| jjd }| j  < dtj	j
dtj	jf fdd}| _| _|_|_| j|_ j| | S )NzDuplicated route path: z, previous one registered at :requestreturnc                    s(    j   }|j}|| I d H S )N)r   r   proxy_request)r   	bind_infosubprocess_module_handler   methodpathr   r   r   parent_side_handlerM   s   zSSubprocessRouteTable._register_route.<locals>._wrapper.<locals>.parent_side_handler)r   	Exceptionfilenamelineno	_BindInfo__code__co_filenameco_firstlinenor   webRequestResponser   r	   __name___routesroute)handlerr   r#   r    r   r   _wrapper>   s2   z6SubprocessRouteTable._register_route.<locals>._wrapperr   )r   r!   r"   r   kwargsr2   r   r    r   _register_route6   s   "z$SubprocessRouteTable._register_routeN)r   r   )r.   
__module____qualname____doc__collectionsdefaultdictdictr   r   r+   RouteTableDefr/   classmethodr   r   HTTPr4   r   r   r   r   r   
   s    

r   )r8   r
   ray.dashboard.optional_depsr   ray.dashboard.routesr   !ray.dashboard.subprocesses.handler    ray.dashboard.subprocesses.utilsr   r   r   r   r   r   <module>   s    