o
    ;iA                     @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlmZ ddl	m
Z
mZ ddl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mZ ddlmZmZ ddlmZ edeeZ G dd dee  Z!dee  fddZ"G dd dee  Z#G dd dee  Z$ee$Z%dS )    N)GenericOptionalTypeVar)api_pb2   )TaskContextsynchronize_api)stream_from_stdinwrite_to_fd)TaskCommandRouterClient)_Client)logger)ExecTimeoutErrorInteractiveTimeoutErrorInvalidError)_StreamReader_StreamWriter)
StreamTypeTc                   @   sD  e Zd ZU dZee ed< ee ed< ee ed< e	ed< dZ
ee ed< eed< eed< dZee ed	< ejejdd
dfdedededededee dededdfddZdefddZedee fddZedee fddZede	fddZedefdd Zdee fd!d"Zdefd#d$Zdefd%d&Zd'd( ZdS ))_ContainerProcessThroughServerN_process_id_stdout_stderr_stdin_exec_deadline_text_by_line_returncodeTF
process_idtask_idclientstdoutstderrexec_deadlinetextby_linereturnc	           	      C   sz   || _ || _|| _|| _|| _tt tj|d| j|||||d	| _	tt tj
|d| j|||||d	| _t|d| j| _d S )Ncontainer_process)stream_typer$   r%   deadliner   )r   _clientr   r   r   r   r   r   FILE_DESCRIPTOR_STDOUTr   FILE_DESCRIPTOR_STDERRr   r   r   )	selfr   r   r    r!   r"   r#   r$   r%    r.   K/home/ubuntu/.local/lib/python3.10/site-packages/modal/container_process.py__init__   s8   z'_ContainerProcessThroughServer.__init__c                 C      d| j dS NzContainerProcess(process_id=)r   r-   r.   r.   r/   __repr__G      z'_ContainerProcessThroughServer.__repr__c                 C      | j S z7StreamReader for the container process's stdout stream.r   r5   r.   r.   r/   r!   J      z%_ContainerProcessThroughServer.stdoutc                 C   r8   z7StreamReader for the container process's stderr stream.r   r5   r.   r.   r/   r"   O   r;   z%_ContainerProcessThroughServer.stderrc                 C   r8   z6StreamWriter for the container process's stdin stream.r   r5   r.   r.   r/   stdinT   r;   z$_ContainerProcessThroughServer.stdinc                 C      | j d u r	td| j S NzvYou must call wait() before accessing the returncode. To poll for the status of a running process, use poll() instead.r   r   r5   r.   r.   r/   
returncodeY   
   
z)_ContainerProcessThroughServer.returncodec                    sx   | j sJ | jdur| jS | jrt | jkrd| _| jS tj| j dd}| jj	|I dH }|j
r:|j| _| jS dS )Check if the container process has finished running.

        Returns `None` if the process is still running, else returns the exit code.
        Nr   exec_idtimeout)r   r   r   time	monotonicr   ContainerExecWaitRequestr*   stubContainerExecWait	completed	exit_coder-   reqrespr.   r.   r/   pollb   s   

z#_ContainerProcessThroughServer.pollc                    s@   | j sJ 	 tj| j dd}| jj|I d H }|jr|jS q)NT
   rH   )r   r   rM   r*   rN   rO   rP   rQ   rR   r.   r.   r/   _wait_for_completiony   s   
z3_ContainerProcessThroughServer._wait_for_completionc              	      s   | j dur	| j S z"d}| jr| jt  }|dkrt tj|  |dI dH | _ W n tjtfy:   d| _ Y nw t	d| j
 d| j   | j S )HWait for the container process to finish running. Returns the exit code.Nr   rJ   rG   ContainerProcess z  wait completed with returncode )r   r   rK   rL   TimeoutErrorasynciowait_forrW   r   debugr   )r-   rJ   r.   r.   r/   wait   s   

z#_ContainerProcessThroughServer.waitc              
      sx  t  dkrtd dS ddlm} | }|d    t	 dt
f fdd	}d
tdtffdd}t 4 I dH o}||j}||j}z8tj ddI dH  t|dd4 I dH  |I dH  |I dH  W d  I dH  n1 I dH sw   Y  W n tjtfy      |  |  tdw W d  I dH  dS 1 I dH sw   Y  dS )mdmd:hiddenWindows7interactive exec is not currently supported on Windows.Nr   OutputManagerConnecting...streamc                    sn   ddl m} t| j|sJ | j}|jdd2 z3 d H W } s*     t| j	|I d H  q6 d S )Nr   )_StreamReaderThroughServerF)skip_empty_messages)

io_streamsrg   
isinstance_impl	_get_logsis_setstopsetr
   file_descriptor)rf   rg   stream_implchunkconnecting_status
on_connectr.   r/   _write_to_fd_loop   s   z@_ContainerProcessThroughServer.attach.<locals>._write_to_fd_loopdatamessage_indexc                    "    j |   j  I d H  d S Nr@   writedrainrw   rx   r5   r.   r/   _handle_input      z<_ContainerProcessThroughServer.attach.<locals>._handle_input<   rY   Tuse_raw_terminal>Failed to establish connection to container. Please try again.)platformsystemprintoutputrd   getstatusstartr\   Eventr   bytesintr   create_taskr!   r"   r]   r_   r	   r[   rn   cancelr   r-   rd   r   rv   r   tcstdout_taskstderr_taskr.   rt   ru   r-   r/   attach   s:   

(.z%_ContainerProcessThroughServer.attach)__name__
__module____qualname__r   r   str__annotations__r   r   r   r   floatboolr   r   r   PIPEr   r0   r6   propertyr!   r"   r@   rD   rU   rW   r_   r   r.   r.   r.   r/   r      s\   
 	

(r   rf   c                 C  s8   | 2 z3 dH W }t |tr|dV  q|V  q6 dS )zDYield raw bytes from a StreamReader regardless of text mode/backend.Nzutf-8)rj   r   encode)rf   partr.   r.   r/   _iter_stream_as_bytes   s   
r   c                   @   s   e Zd ZdZejejdddddededed	ed
edede	e
 dededdfddZdefddZedee fddZedee fddZedefddZedefddZde	e fddZdefddZd d! ZdS )"%_ContainerProcessThroughCommandRouterz
    Container process implementation that works via direct communication with
    the Modal worker where the container is running.
    NTFr!   r"   r#   r$   r%   r   r    command_router_clientr   r!   r"   r#   r$   r%   r&   c          
      C   s   || _ || _|| _|| _|| _|	| _|| _tt t	j
|d| j |||	|| j| jd
| _tt t	j|d| j |||	|| j| jd
| _t|d| j | j| jd| _d | _d S )Nr'   )r(   r$   r%   r)   r   r   )r   r   )r*   _command_router_clientr   r   r   r   _task_idr   r   r   r+   r   r,   r   r   r   r   )
r-   r   r    r   r   r!   r"   r#   r$   r%   r.   r.   r/   r0      sN   
z._ContainerProcessThroughCommandRouter.__init__c                 C   r1   r2   r4   r5   r.   r.   r/   r6     r7   z._ContainerProcessThroughCommandRouter.__repr__c                 C   r8   rz   r:   r5   r.   r.   r/   r!        z,_ContainerProcessThroughCommandRouter.stdoutc                 C   r8   rz   r=   r5   r.   r.   r/   r"     r   z,_ContainerProcessThroughCommandRouter.stderrc                 C   r8   rz   r?   r5   r.   r.   r/   r@     r   z+_ContainerProcessThroughCommandRouter.stdinc                 C   rA   rB   rC   r5   r.   r.   r/   rD     rE   z0_ContainerProcessThroughCommandRouter.returncodec              
      s  | j d ur	| j S zH| j| j| j| jI d H }|d}|d u r$W d S |dkr2t|j| _ | j W S |dkrBdt|j	 | _ | j W S t
d| j d|  td tyj   t
d| j d	 d
| _ | j  Y S  ty } zt
d| j d|   d }~ww )Nexit_statuscodesignal   rZ     exited with unexpected status: Unexpected exit statuszContainerProcess poll for ! did not complete within deadlinerG   z	 failed: )r   r   	exec_pollr   r   r   
WhichOneofr   r   r   r   r^   r   r   	Exceptionwarning)r-   rT   whicher.   r.   r/   rU   '  s2   


z*_ContainerProcessThroughCommandRouter.pollc                    s   | j d ur	| j S zD| j| j| j| jI d H }|d}|dkr+t|j| _ W | j S |dkr;dt|j	 | _ W | j S t
d| j d|  d| _ td tye   t
d| j d	 d| _ Y | j S w )
Nr   r   r   r   rZ   r   rG   r   r   )r   r   	exec_waitr   r   r   r   r   r   r   r   r^   r   r   )r-   rT   r   r.   r.   r/   r_   D  s(   

z*_ContainerProcessThroughCommandRouter.waitc              
      s|  t  dkrtd d S ddlm} | }|d    t	 dt
t f fdd}d	td
tffdd}t 4 I d H o}||j}||j}z8tj ddI d H  t|dd4 I d H  |I d H  |I d H  W d   I d H  n1 I d H sw   Y  W n tjtfy      |  |  tdw W d   I d H  d S 1 I d H sw   Y  d S )Nra   rb   r   rc   re   rf   c                    sV   t | 2 z"3 d H W }|d u r d S  s     t| j|I d H  q6 d S rz   )r   rm   rn   ro   r
   rp   )rf   rr   rs   r.   r/   rv   g  s   zG_ContainerProcessThroughCommandRouter.attach.<locals>._write_to_fd_looprw   rx   c                    ry   rz   r{   r~   r5   r.   r/   r   r  r   zC_ContainerProcessThroughCommandRouter.attach.<locals>._handle_inputr   rY   Tr   r   )r   r   r   r   rd   r   r   r   r\   r   r   r   r   r   r   r   r!   r"   r]   r_   r	   r[   rn   r   r   r   r.   r   r/   r   [  s:   

(.z,_ContainerProcessThroughCommandRouter.attach)r   r   r   __doc__r   r   r   r   r   r   r   r   r0   r6   r   r   r   r!   r"   r   r@   r   rD   rU   r_   r   r.   r.   r.   r/   r      sP    	

5r   c                   @   s   e Zd ZdZejejddddfdedededed	ed
ee	 de
de
dee ddfddZdefddZedee fddZedee fddZedefddZedefddZdee fddZdefddZdd  ZdS )!_ContainerProcessz,Represents a running process in a container.NTFr   r   r    r!   r"   r#   r$   r%   r   r&   c
           
      C   sF   |	d u rt ||||||||d| _d S t|||	||||||d	| _d S )Nr   )r   rk   r   )
r-   r   r   r    r!   r"   r#   r$   r%   r   r.   r.   r/   r0     s,   z_ContainerProcess.__init__c                 C   s
   | j  S rz   )rk   r6   r5   r.   r.   r/   r6     s   
z_ContainerProcess.__repr__c                 C      | j jS r9   )rk   r!   r5   r.   r.   r/   r!        z_ContainerProcess.stdoutc                 C   r   r<   )rk   r"   r5   r.   r.   r/   r"     r   z_ContainerProcess.stderrc                 C   r   r>   )rk   r@   r5   r.   r.   r/   r@     r   z_ContainerProcess.stdinc                 C   r   rz   )rk   rD   r5   r.   r.   r/   rD     s   z_ContainerProcess.returncodec                       | j  I dH S )rF   N)rk   rU   r5   r.   r.   r/   rU     s   z_ContainerProcess.pollc                    r   )rX   N)rk   r_   r5   r.   r.   r/   r_     s   z_ContainerProcess.waitc                    s   | j  I dH  dS )r`   N)rk   r   r5   r.   r.   r/   r     s   z_ContainerProcess.attach)r   r   r   r   r   r   r   r   r   r   r   r   r0   r6   r   r   r   r!   r"   r   r@   r   rD   rU   r_   r   r.   r.   r.   r/   r     sR    	

$r   )&r\   r   rK   typingr   r   r   modal_protor   _utils.async_utilsr   r   _utils.shell_utilsr	   r
   !_utils.task_command_router_clientr   r    r   configr   	exceptionr   r   r   ri   r   r   r(   r   r   r   r   r   r   r   r   ContainerProcessr.   r.   r.   r/   <module>   s*    7	 6M