
    tni%                     v    d Z ddlZddlmZmZmZ ddlmZmZ ddl	m
Z
  G d d          Z G d d	          ZdS )
z,Module for running endpoints asynchronously.    N)AnyDictOptional)FINAL_STATESis_completed)ClientSessionc                       e Zd ZdZdedededefdZdded	eee	f         fd
Z
d	efdZd Zdded	e	fdZd	e	fdZd	efdZdS )Jobz5Class representing a job for an asynchronous endpointendpoint_idjob_idsessionheadersc                 t    ddl m} || _        || _        || _        || _        || _        d| _        d| _        dS )a  
        Initialize a Job instance.
        
        Args:
            endpoint_id: The identifier for the endpoint.
            job_id: The identifier for the job.
            session: The aiohttp ClientSession.
            headers: Headers to use for requests.
        r   )endpoint_url_baseN)runpodr   r   r   r   r   
job_status
job_output)selfr   r   r   r   r   s         c/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/endpoint/asyncio/asyncio_runner.py__init__zJob.__init__   sY    	
 	
 	
 	
 	
 	
 '!2    statussourcereturnc                 >  K   | j          d| j         d| d| j         }| j                            || j                   d{V }|                                 d{V }t          |d                   r(|d         | _        |                    dd          | _	        |S )zReturns the raw json of the status, raises an exception if invalid.

        Args:
            source: The URL source path of the job status.
        /r   Nr   output)
r   r   r   r   getr   jsonr   r   r   )r   r   
status_url	job_states       r   
_fetch_jobzJob._fetch_job&   s       %QQ(8QQ6QQDKQQ 	 ,**:t|*LLLLLLLL	#..********		(+,, 	<'1DO'mmHd;;DOr   c                 f   K   | j         | j         S |                                  d{V }|d         S )zYGets jobs' status

        Returns:
            COMPLETED, FAILED or IN_PROGRESS
        Nr   )r   r#   )r   r"   s     r   r   z
Job.status8   sD       ?&?"//++++++++	""r   c                    K   t          |                                  d {V           sCt          j        d           d {V  t          |                                  d {V           Ad S d S )N   )r   r   asynciosleep)r   s    r   _wait_for_completionzJob._wait_for_completionD   s      T[[]]22222233 	#-""""""""" T[[]]22222233 	# 	# 	# 	# 	#r   r   timeoutc                 .  K   | j         | j         S 	 t          j        |                                 |           d{V  n'# t          j        $ r}t	          d          |d}~ww xY w|                                  d{V }|                    dd          S )zWaits for serverless API job to complete or fail

        Returns:
            Output of job
        Raises:
            KeyError if job Failed
        NzJob timed out.r   )r   r'   wait_forr)   TimeoutErrorr#   r   )r   r*   excjob_datas       r   r   z
Job.outputH   s       ?&?"	:"4#<#<#>#>HHHHHHHHHH# 	: 	: 	:/00c9	: ********||Hd+++s   -A   A$AA$c                N  K   	 t          j        d           d{V  |                     d           d{V }|d         t          vs't	          |                    dg                     dk    r%|                    dg           D ]}|d         W V  n|d         t          v rdS )	z>Returns a generator that yields the output of the job request.Tr&   Nstream)r   r   r   r   )r'   r(   r#   r   lenr   )r   stream_partialchunks      r   r1   z
Job.stream[   s      
	-"""""""""#'??(?#C#CCCCCCCNx(<<~))(B77881<<+//"== * *E/)))))*)\99
	r   c                   K   | j          d| j         d| j         }| j                            || j                  4 d{V }|                                 d{V cddd          d{V  S # 1 d{V swxY w Y   dS )zUCancels current job

        Returns:
            Output of cancel operation
        r   z/cancel/r   N)r   r   r   r   postr   r    )r   
cancel_urlresps      r   cancelz
Job.canceli   s       .XX1AXX4;XX
<$$Z$FF 	% 	% 	% 	% 	% 	% 	%$$$$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   A00
A:=A:N)r   )r   )__name__
__module____qualname____doc__strr   dictr   r   r   r#   r   r)   intr   r1   r9    r   r   r
   r
      s       ??C  } W[    . s $sCx.    $
#c 
# 
# 
# 
## # #, ,C , , , , ,&c    %d % % % % % %r   r
   c                   ^    e Zd ZdZ	 ddededee         fdZdede	fd	Z
defd
ZdefdZdS )EndpointzClass for running endpointNr   r   api_keyc                     ddl m}m} || _        || _        || _        | d| j         d| _        |p|| _        | j        t          d          dd| j         d	| _        dS )
z
        Initialize an async Endpoint instance.
        
        Args:
            endpoint_id: The identifier for the endpoint.
            session: The aiohttp ClientSession.
            api_key: Optional API key for this endpoint instance.
        r   )rD   r   r   z/runNz(API key must be provided or set globallyzapplication/jsonzBearer )zContent-TypeAuthorization)r   rD   r   r   r   endpoint_urlRuntimeErrorr   )r   r   r   rD   global_api_keyr   s         r   r   zEndpoint.__init__w   s    	
 	
 	
 	
 	
 	
 	
 	

 '!20II43CIII 0.<IJJJ /5t|55
 
r   endpoint_inputr   c                 n  K   | j                             | j        | j        d|i          4 d{V 	 }|                                 d{V }ddd          d{V  n# 1 d{V swxY w Y   | j                                        }|d         |d<   t          | j        |d         | j         |          S )z
        Runs endpoint with specified input.

        Args:
            endpoint_input: any dictionary with input

        Returns:
            Newly created job
        input)r   r    NidzX-Request-ID)r   r6   rG   r   r    copyr
   r   )r   rJ   r8   	json_respjob_headerss        r   runzEndpoint.run   sG      <$$t|7N:S % 
 
 	* 	* 	* 	* 	* 	* 	* 	*"iikk))))))I	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* l''))&/oN#4#Yt_dlKPPPs   A  
A*-A*c                    K   | j          d| j         d}| j                            || j                  4 d{V }|                                 d{V cddd          d{V  S # 1 d{V swxY w Y   dS )z\
        Checks health of endpoint

        Returns:
            Health of endpoint
        r   z/healthr   N)r   r   r   r   r   r    )r   
health_urlr8   s      r   healthzEndpoint.health   s      .JJ1AJJJ
<##J#EE 	% 	% 	% 	% 	% 	% 	%$$$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%   A))
A36A3c                    K   | j          d| j         d}| j                            || j                  4 d{V }|                                 d{V cddd          d{V  S # 1 d{V swxY w Y   dS )zU
        Purges queue of endpoint

        Returns:
            Purge status
        r   z/purge-queuer   N)r   r   r   r6   r   r    )r   	purge_urlr8   s      r   purge_queuezEndpoint.purge_queue   s      -NN0@NNN	<$$Y$EE 	% 	% 	% 	% 	% 	% 	%$$$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%rU   )N)r:   r;   r<   r=   r>   r   r   r   r?   r
   rQ   rT   rX   rA   r   r   rC   rC   t   s        $$ +/
 
C 
- 
"3-
 
 
 
>Q Q Q Q Q Q(
%d 
% 
% 
% 
%
%4 
% 
% 
% 
% 
% 
%r   rC   )r=   r'   typingr   r   r   runpod.endpoint.helpersr   r   runpod.http_clientr   r
   rC   rA   r   r   <module>r\      s    2 2  & & & & & & & & & & > > > > > > > > , , , , , ,e% e% e% e% e% e% e% e%PL% L% L% L% L% L% L% L% L% L%r   