§
    AxÅjO  ã                   ó|  — d Z 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	 ddl
mZ  e¦   «         Z ej        ¦   «         Zej                             d e ej        ¦   «         ¦  «        ¦  «        ZdZej                             dd¦  «        du Z G d	„ d
¦  «        Z G d„ de	e         ¦  «        Z G d„ d¦  «        ZdS )z]
Handles getting stuff from environment variables and updating the global state like job id.
é    N)ÚAnyÚDictÚOptionalÚSeté   )ÚRunPodLoggerÚRUNPOD_POD_IDi   ÚRUNPOD_WEBHOOK_GET_JOBc            	       ó€   — e Zd ZdZ	 	 ddedeeeef                  dee         ddfd„Zde	de
fd	„Zdefd
„Zdefd„ZdS )ÚJobzÂ
    Represents a job object.

    Args:
        job_id: The id of the job, a unique string.
        job_input: The input to the job.
        webhook: The webhook to send the job output to.
    NÚidÚinputÚwebhookÚreturnc                 ó†   — || _         || _        || _        |                     ¦   «         D ]\  }}t	          | ||¦  «         Œd S ©N)r   r   r   ÚitemsÚsetattr)Úselfr   r   r   ÚkwargsÚkeyÚvalues          úWC:\Projects\Historical\venv\Lib\site-packages\runpod/serverless/modules/worker_state.pyÚ__init__zJob.__init__&   sS   € ð ˆŒØˆŒ
ØˆŒà Ÿ,š,™.œ.ð 	&ð 	&‰JˆC�Ý�D˜#˜uÑ%Ô%Ð%Ð%ð	&ð 	&ó    Úotherc                 óP   — t          |t          ¦  «        r| j        |j        k    S dS )NF)Ú
isinstancer   r   )r   r   s     r   Ú__eq__z
Job.__eq__4   s(   € Ý�e�SÑ!Ô!ð 	'Ø”7˜eœhÒ&Ð&Øˆur   c                 ó*   — t          | j        ¦  «        S r   )Úhashr   ©r   s    r   Ú__hash__zJob.__hash__9   s   € Ý�D”G‰}Œ}Ðr   c                 ó   — | j         S r   ©r   r"   s    r   Ú__str__zJob.__str__<   s	   € ØŒwˆr   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   r   ÚobjectÚboolr   Úintr#   r&   © r   r   r   r      sÎ   € € € € € ðð ð +/Ø!%ð	&ð &àð&ð ˜˜S #˜XœÔ'ð&ð ˜#”ð	&ð 
ð&ð &ð &ð &ð˜Fð  tð ð ð ð ð
˜#ð ð ð ð ð˜ð ð ð ð ð ð r   r   c                   ó°   ‡ — e Zd ZdZdZd„ Zd„ Zdefd„Zdd„Z	dd„Z
dˆ fd	„Zd
efˆ fd„Zd
efˆ fd„Zd
edee         fd„Zdee         fd„Zdefd„Zˆ xZS )ÚJobsProgresszETrack the state of current jobs in progress (in-memory, per process).Nc                 óä   — t           j        €Yt                               | ¦  «        t           _        t                               t           j        ¦  «         d t           j        _        t           j        S r   )r1   Ú	_instanceÚsetÚ__new__r   Ú_mirror)Úclss    r   r5   zJobsProgress.__new__H   sJ   € ÝÔ!Ð)Ý%(§[¢[°Ñ%5Ô%5�LÔ"Ý�LŠL�Ô/Ñ0Ô0Ð0ð .2�LÔ"Ô*ÝÔ%Ð%r   c                 ó   — d S r   r/   r"   s    r   r   zJobsProgress.__init__Q   s   € àˆr   r   c                 óJ   — d| j         j        › d|                      ¦   «         › �S )Nú<z>: )Ú	__class__r'   Úget_job_listr"   s    r   Ú__repr__zJobsProgress.__repr__U   s)   € ØD�4”>Ô*ÐDÐD¨t×/@Ò/@Ñ/BÔ/BÐDÐDÐDr   c                 ó<   — || _         |                      ¦   «          dS )z�Attach a PingJobMirror that mirrors the in-progress job ids to the
        ping process. Every add/remove/clear then pushes the snapshot to it.N)r6   Ú_notify_mirror)r   Úmirrors     r   Ú
set_mirrorzJobsProgress.set_mirrorX   s#   € ð ˆŒØ×ÒÑÔÐÐÐr   c                 óp   — | j         �.| j                              |                      ¦   «         ¦  «         dS dS )z@Push the current job-id snapshot to the attached mirror, if any.N)r6   r4   r<   r"   s    r   r?   zJobsProgress._notify_mirror^   s:   € àŒ<Ð#ØŒL×Ò˜T×.Ò.Ñ0Ô0Ñ1Ô1Ð1Ð1Ð1ð $Ð#r   c                 óp   •— t          ¦   «                              ¦   «          |                      ¦   «          d S r   )ÚsuperÚclearr?   )r   r;   s    €r   rE   zJobsProgress.clearc   s,   ø€ Ý‰Œ�Š‰ŒˆØ×ÒÑÔÐÐÐr   Úelementc                 óF  •— t          |t          ¦  «        rt          |¬¦  «        }t          |t          ¦  «        rt          di |¤Ž}t          |t          ¦  «        st	          d¦  «        ‚t          ¦   «                              |¦  «        }|                      ¦   «          |S )zÃ
        Adds a Job object to the set.

        If the added element is a string, then `Job(id=element)` is added.
        If the added element is a dict, then `Job(**element)` is added.
        r%   z.Only Job objects can be added to JobsProgress.r/   )r   r+   r   ÚdictÚ	TypeErrorrD   Úaddr?   ©r   rF   Úresultr;   s      €r   rJ   zJobsProgress.addg   s“   ø€ õ �g�sÑ#Ô#ð 	&Ý˜W�o‘o”oˆGå�g�tÑ$Ô$ð 	%Ý�n�n˜G�n�nˆGå˜'¥3Ñ'Ô'ð 	NÝÐLÑMÔMÐMå‘”—’˜WÑ%Ô%ˆØ×ÒÑÔÐØˆr   c                 óF  •— t          |t          ¦  «        rt          |¬¦  «        }t          |t          ¦  «        rt          di |¤Ž}t          |t          ¦  «        st	          d¦  «        ‚t          ¦   «                              |¦  «        }|                      ¦   «          |S )zÀ
        Removes a Job object from the set.

        If the element is a string, then `Job(id=element)` is removed.
        If the element is a dict, then `Job(**element)` is removed.
        r%   z2Only Job objects can be removed from JobsProgress.r/   )r   r+   r   rH   rI   rD   Údiscardr?   rK   s      €r   ÚremovezJobsProgress.remove{   s“   ø€ õ �g�sÑ#Ô#ð 	&Ý˜W�o‘o”oˆGå�g�tÑ$Ô$ð 	%Ý�n�n˜G�n�nˆGå˜'¥3Ñ'Ô'ð 	RÝÐPÑQÔQÐQå‘”—’ Ñ)Ô)ˆØ×ÒÑÔÐØˆr   c                 ó¶   — t          |t          ¦  «        rt          |¬¦  «        }t          |t          ¦  «        st          d¦  «        ‚| D ]}||k    r|c S Œd S )Nr%   z4Only Job objects can be retrieved from JobsProgress.)r   r+   r   rI   )r   rF   Újobs      r   ÚgetzJobsProgress.get�   sn   € Ý�g�sÑ#Ô#ð 	&Ý˜W�o‘o”oˆGå˜'¥3Ñ'Ô'ð 	TÝÐRÑSÔSÐSàð 	ð 	ˆCØ�gŠ~ˆ~Ø�
�
�
ð àˆtr   c                 ób   — t          | ¦  «        sdS d                     d„ | D ¦   «         ¦  «        S )z\
        Returns the list of job IDs as a comma-separated string, or None if empty.
        Nú,c              3   ó4   K  — | ]}t          |¦  «        V — Œd S r   )r+   )Ú.0rQ   s     r   ú	<genexpr>z,JobsProgress.get_job_list.<locals>.<genexpr>¢   s(   è è € Ð1Ð1 S�˜C™œÐ1Ð1Ð1Ð1Ð1Ð1r   )ÚlenÚjoinr"   s    r   r<   zJobsProgress.get_job_list›   s8   € õ �4‰yŒyð 	Ø�4à�xŠxÐ1Ð1¨DÐ1Ñ1Ô1Ñ1Ô1Ð1r   c                 ó    — t          | ¦  «        S )z-
        Returns the number of jobs.
        )rX   r"   s    r   Úget_job_countzJobsProgress.get_job_count¤   s   € õ �4‰yŒyÐr   )r   N)r'   r(   r)   r*   r3   r5   r   r+   r=   rA   r?   rE   r   rJ   rO   r   r   rR   r<   r.   r[   Ú__classcell__)r;   s   @r   r1   r1   C   sX  ø€ € € € € ØOÐOà€Ið&ð &ð &ðð ð ðE˜#ð Eð Eð Eð Eðð ð ð ð2ð 2ð 2ð 2ð
ð ð ð ð ð ð˜3ð ð ð ð ð ð ð(˜cð ð ð ð ð ð ð(
˜3ð 
 8¨C¤=ð 
ð 
ð 
ð 
ð2˜h sœmð 2ð 2ð 2ð 2ð˜sð ð ð ð ð ð ð ð r   r1   c                   óX   — e Zd ZdZedfdefd„Zdee         ddfd„Z	dee         fd„Z
dS )	ÚPingJobMirrora  
    One-way snapshot of in-progress job ids from the worker (main) process to
    the separate ping process.

    Backed by a fixed-size shared-memory buffer created in the main process and
    passed to the ping process via ``Process(args=...)``. It lives only in this
    worker's own process tree, so it cannot be shared across workers and never
    touches the filesystem. All operations are best-effort and never raise into
    the caller (a failure here must not break job processing or kill the ping).
    NÚcapacityc                 ó\   — |pt           }|| _        |                     d|¦  «        | _        d S )NÚc)ÚmultiprocessingÚ	_capacityÚArrayÚ_buffer)r   r_   Úctxs      r   r   zPingJobMirror.__init__º   s,   € ØÐ$•_ˆØ!ˆŒØ—y’y  hÑ/Ô/ˆŒˆˆr   Újob_idsr   c                 óð  — 	 |pd                      d¦  «        }| j        dz
  }t          |¦  «        |k    rM|d|…         }|                     d¦  «        }|dk    r
|d|…         }t                               d|› d�¦  «         | j                             ¦   «         5  || j        _        ddd¦  «         dS # 1 swxY w Y   dS # t          $ r(}t           
                    d	|› �¦  «         Y d}~dS d}~ww xY w)
z=Write the current job-id snapshot. Best-effort; never raises.Ú úutf-8r   Nó   ,éÿÿÿÿz(PingJobMirror: job-id snapshot exceeded z bytes; truncatedzPingJobMirror.set failed: )Úencoderc   rX   ÚrfindÚlogÚwarnre   Úget_lockr   Ú	ExceptionÚerror)r   rg   ÚdataÚlimitÚcutÚerrs         r   r4   zPingJobMirror.set¿   sc  € ð	:Ø�M˜r×)Ò)¨'Ñ2Ô2ˆDØ”N QÑ&ˆEÝ�4‰yŒy˜5Ò Ð Ø˜F˜U˜F”|�Ø—j’j Ñ&Ô&�Ø˜"’9�9Ø   œ:�DÝ—’ØW¸uÐWÐWÐWñô ð ð ”×&Ò&Ñ(Ô(ð *ð *Ø%)�”Ô"ð*ð *ð *ñ *ô *ð *ð *ð *ð *ð *ð *ð *øøøð *ð *ð *ð *ð *ð *øåð 	:ð 	:ð 	:Ý�IŠIÐ8°3Ð8Ð8Ñ9Ô9Ð9Ð9Ð9Ð9Ð9Ð9Ð9øøøøð	:øøøs<   ‚BC ÂB6Â)C Â6B:Â:C Â=B:Â>C Ã
C5ÃC0Ã0C5c                 ó  — 	 | j                              ¦   «         5  | j         j        }ddd¦  «         n# 1 swxY w Y   |                     d¦  «        }|pdS # t          $ r(}t
                               d|› �¦  «         Y d}~dS d}~ww xY w)z<Read the current job-id snapshot. Best-effort; never raises.Nrj   zPingJobMirror.get failed: )re   rq   r   Údecoderr   ro   Údebug)r   rt   Útextrw   s       r   rR   zPingJobMirror.getÑ   sÜ   € ð	Ø”×&Ò&Ñ(Ô(ð *ð *Ø”|Ô)�ð*ð *ð *ñ *ô *ð *ð *ð *ð *ð *ð *øøøð *ð *ð *ð *à—;’;˜wÑ'Ô'ˆDØ�<˜4ÐøÝð 	ð 	ð 	Ý�IŠIÐ8°3Ð8Ð8Ñ9Ô9Ð9Ø�4�4�4�4�4øøøøð	øøøs2   ‚A ›4¨A ´8¸A »8¼A Á
B
Á"BÂB
)r'   r(   r)   r*   ÚPING_MIRROR_CAPACITYr.   r   r   r+   r4   rR   r/   r   r   r^   r^   ®   sŠ   € € € € € ð	ð 	ð (<Àð 0ð 0 ð 0ð 0ð 0ð 0ð
:˜8 Cœ=ð :¨Tð :ð :ð :ð :ð$	�X˜c”]ð 	ð 	ð 	ð 	ð 	ð 	r   r^   )r*   rb   ÚosÚtimeÚuuidÚtypingr   r   r   r   Ú	rp_loggerr   ro   Úperf_counterÚREF_COUNT_ZEROÚenvironrR   r+   Úuuid4Ú	WORKER_IDr|   ÚIS_LOCAL_TESTr   r1   r^   r/   r   r   ú<module>rˆ      sg  ððð ð Ð Ð Ð Ø 	€	€	€	Ø €€€Ø €€€Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ð +à #Ð #Ð #Ð #Ð #Ð #ð €l�n„n€à"�Ô"Ñ$Ô$€àŒJ�NŠN˜?¨C¨C°
°´
±´Ñ,=Ô,=Ñ>Ô>€	àÐ ð ”
—’Ð7¸Ñ>Ô>À$ÐF€ð!ð !ð !ð !ð !ñ !ô !ð !ðNeð eð eð eð e�3�s”8ñ eô eð eðV,ð ,ð ,ð ,ð ,ñ ,ô ,ð ,ð ,ð ,r   