Ë
    8ø»j#  ã                  ó  — d dl mZ d dlZ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
mZ d dlmZ d dlmZ e
rd dlZd d	lmZ d d
lmZ d dlmZmZ  G d„ d«      Z ed«       G d„ de«      «       Z ed«       G d„ de«      «       Zy)é    )ÚannotationsN)ÚTask)Ú	timedelta)ÚThread)ÚTYPE_CHECKINGÚSelf)Ú
docs_group)Ú
to_seconds)ÚTracebackType)ÚRun)Ú	RunClientÚRunClientAsyncc                  ó<   — e Zd ZdZdZdZ ed¬«      dœd
d„Zdd„Zy	)ÚStatusMessageWatcherBasez=Base class for polling and logging Actor run status messages.Fé   é   ©Úseconds©Úcheck_periodc               óf   — | j                   rd|_        || _        t        |«      | _        d| _        y )NTÚ )Ú_force_propagateÚ	propagateÚ
_to_loggerr
   Ú_check_periodÚ_last_status_message)ÚselfÚ	to_loggerr   s      ú[/var/www/html/GAP/venv/lib/python3.12/site-packages/apify_client/_status_message_watcher.pyÚ__init__z!StatusMessageWatcherBase.__init__   s/   € Ø× Ò Ø"&ˆIÔØ#ˆŒÜ'¨Ó5ˆÔØ$&ˆÕ!ó    c                óÜ   — |�j|j                   xs d}|j                  xs d}d|› d|› �}|| j                  k7  r"|| _        | j                  j	                  |«       |j
                  xs d S y)a  Get relevant run data, log them if changed and return `True` if more data is expected.

        Args:
            run_data: The Run model that contains the run data.

        Returns:
              `True` if more data is expected, `False` otherwise.
        zUnknown statusr   zStatus: z, Message: FT)ÚstatusÚstatus_messager   r   ÚinfoÚis_status_message_terminal)r   Úrun_datar$   r%   Únew_status_messages        r    Ú_log_run_dataz&StatusMessageWatcherBase._log_run_data&   s}   € ð ÐØ—_‘_Ò8Ð(8ˆFØ%×4Ñ4Ò:¸ˆNØ#+¨F¨8°;¸~Ð>NÐ!OÐà! T×%>Ñ%>Ò>Ø,>�Ô)Ø—‘×$Ñ$Ð%7Ô8à ×;Ñ;ÒD¸uÐEÐEØr"   N)r   úlogging.Loggerr   r   ÚreturnÚNone)r(   z
Run | Noner,   Úbool)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú_final_sleep_time_sr   r!   r*   © r"   r    r   r      s&   „ ÙGàÐð ÐáOXÐabÔOcõ 'ôr"   r   ÚOtherc                  ó~   ‡ — e Zd ZdZ ed¬«      dœ	 	 	 	 	 	 	 dˆ fd„Zdd„Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd	„Z	dd
„Z
ˆ xZS )ÚStatusMessageWatcherAsynca/  Polls and logs Actor run status messages in an asyncio task.

    The status message and status of the Actor run are polled at a fixed interval and forwarded to the provided logger
    whenever they change. There is no guarantee that every intermediate status message will be captured, especially
    when messages change rapidly.

    Can be used as an async context manager, which automatically starts and cancels the polling task. Alternatively,
    call `start` and `stop` manually. Obtain an instance via `RunClientAsync.get_status_message_watcher`.
    é   r   r   c               óD   •— t         ‰| �  ||¬«       || _        d| _        y)a.  Initialize `StatusMessageWatcherAsync`.

        Args:
            run_client: The run client used to poll the Actor run status and status message.
            to_logger: The logger to which the status messages will be forwarded.
            check_period: How often to poll the status message.
        ©r   r   N)Úsuperr!   Ú_run_clientÚ_logging_task©r   Ú
run_clientr   r   Ú	__class__s       €r    r!   z"StatusMessageWatcherAsync.__init__H   s'   ø€ ô 	‰Ñ 9¸<ÐÔHØ%ˆÔØ*.ˆÕr"   c                óÌ   — | j                   r%| j                   j                  «       st        d«      ‚t        j                  | j                  «       «      | _         | j                   S )zwStart the polling task.

        The caller is responsible for cleanup by calling the `stop` method when done.
        zLogging task already active)r=   ÚdoneÚRuntimeErrorÚasyncioÚcreate_taskÚ_log_changed_status_message©r   s    r    ÚstartzStatusMessageWatcherAsync.startV   sR   € ð
 ×Ò d×&8Ñ&8×&=Ñ&=Ô&?ÜÐ<Ó=Ð=Ü$×0Ñ0°×1QÑ1QÓ1SÓTˆÔØ×!Ñ!Ð!r"   c              ƒ  óô   K  — | j                   st        d«      ‚| j                   j                  «        	 | j                   ƒ d{  –—†  d| _         y7 Œ# t        j                  $ r Y Œw xY w# d| _         w xY w­w)zStop the logging task.zLogging task is not activeN)r=   rC   ÚcancelrD   ÚCancelledErrorrG   s    r    ÚstopzStatusMessageWatcherAsync.stop`   sr   è ø€ à×!Ò!ÜÐ;Ó<Ð<à×Ñ×!Ñ!Ô#ð	&Ø×$Ñ$×$Ð$ð "&ˆDÕð	 %ùÜ×%Ñ%ò 	Ùð	ûð "&ˆDÕüsF   ‚2A8µA ÁAÁA Á	A8ÁA ÁA)Á&A, Á(A)Á)A, Á,	A5Á5A8c              ƒ  ó.   K  — | j                  «        | S ­w)z\Start the logging task within the context. Exiting the context will cancel the logging task.©rH   rG   s    r    Ú
__aenter__z$StatusMessageWatcherAsync.__aenter__m   s   è ø€ à�
‰
ŒØˆùs   ‚c              ƒ  ó–   K  — |€'t        j                  | j                  «      ƒ d{  –—†  | j                  «       ƒ d{  –—†  y7 Œ7 Œ­w)zCancel the logging task.N)rD   Úsleepr3   rL   ©r   Úexc_typeÚexc_valÚexc_tbs       r    Ú	__aexit__z#StatusMessageWatcherAsync.__aexit__r   s>   è ø€ ð ÐÜ—-‘- × 8Ñ 8Ó9×9Ð9Ø�i‰i‹k×Ñð :øØús    ‚%A	§A¨A	¿AÁ A	ÁA	c              ƒ  ó¼  K  — 	 	 | j                   j                  «       ƒ d {  –—† }| j                  |«      sy t        j                  | j
                  «      ƒ d {  –—†  Œ\7 Œ>7 Œ# t        $ rk}| j                   j                  j                  |«      r| j                  j                  d«       n | j                  j                  d«       Y d }~y Y d }~y d }~ww xY w­w©NzAStatus message redirection stopped: the status request timed out.z;Status message redirection stopped due to unexpected error:)r<   Úgetr*   rD   rQ   r   Ú	ExceptionÚ_http_clientÚis_timeout_errorr   ÚwarningÚ	exception)r   r(   Úexcs      r    rF   z5StatusMessageWatcherAsync._log_changed_status_messagez   s¹   è ø€ ð	iØØ!%×!1Ñ!1×!5Ñ!5Ó!7×7�Ø×)Ñ)¨(Ô3ØÜ—m‘m D×$6Ñ$6Ó7×7Ð7ð	 Ø7øð 8ùÜò 	iØ×Ñ×,Ñ,×=Ñ=¸cÔBà—‘×'Ñ'Ð(kÕlð —‘×)Ñ)Ð*g×hÑhô mûð	iüsO   ‚C„A% ¢A!£A% ¸C¹"A% ÁA#ÁA% Á#A% Á%	CÁ.ACÃ

CÃCÃC)r?   r   r   r+   r   r   r,   r-   )r,   r   ©r,   r-   ©r,   r   ©rS   ztype[BaseException] | NonerT   zBaseException | NonerU   zTracebackType | Noner,   r-   )r/   r0   r1   r2   r   r!   rH   rL   rO   rV   rF   Ú__classcell__©r@   s   @r    r7   r7   <   st   ø„ ññ clÐtuÔbvñ/Ø+ð/Ø8Fð/ØV_ð/à	õ/ó"ó&óð
Ø2ðØ=QðØ[oðà	ó÷ir"   r7   c                  ó~   ‡ — e Zd ZdZ ed¬«      dœ	 	 	 	 	 	 	 dˆ fd„Zdd„Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd	„Z	dd
„Z
ˆ xZS )ÚStatusMessageWatchera'  Polls and logs Actor run status messages in a background thread.

    The status message and status of the Actor run are polled at a fixed interval and forwarded to the provided logger
    whenever they change. There is no guarantee that every intermediate status message will be captured, especially
    when messages change rapidly.

    Can be used as a context manager, which automatically starts and stops the polling thread. Alternatively,
    call `start` and `stop` manually. Obtain an instance via `RunClient.get_status_message_watcher`.
    r8   r   r   c               óR   •— t         ‰| �  ||¬«       || _        d| _        d| _        y)a)  Initialize `StatusMessageWatcher`.

        Args:
            run_client: The run client used to poll the Actor run status and status message.
            to_logger: The logger to which the status messages will be forwarded.
            check_period: How often to poll the status message.
        r:   NF)r;   r!   r<   Ú_logging_threadÚ_stop_loggingr>   s       €r    r!   zStatusMessageWatcher.__init__–   s/   ø€ ô 	‰Ñ 9¸<ÐÔHØ%ˆÔØ.2ˆÔØ"ˆÕr"   c                ó
  — | j                   r%| j                   j                  «       rt        d«      ‚d| _        t	        j
                  | j                  d¬«      | _         | j                   j                  «        | j                   S )zyStart the polling thread.

        The caller is responsible for cleanup by calling the `stop` method when done.
        zLogging thread already activeFT)ÚtargetÚdaemon)rh   Úis_aliverC   ri   Ú	threadingr   rF   rH   rG   s    r    rH   zStatusMessageWatcher.start¥   sl   € ð
 ×Ò D×$8Ñ$8×$AÑ$AÔ$CÜÐ>Ó?Ð?Ø"ˆÔä(×/Ñ/°t×7WÑ7WÐ`dÔeˆÔØ×Ñ×"Ñ"Ô$Ø×#Ñ#Ð#r"   c                ó�   — | j                   st        d«      ‚d| _        | j                   j                  «        d| _         d| _        y)zDSignal the logging thread to stop logging and wait for it to finish.zLogging thread is not activeTNF)rh   rC   ri   ÚjoinrG   s    r    rL   zStatusMessageWatcher.stop²   sA   € à×#Ò#ÜÐ=Ó>Ð>Ø!ˆÔØ×Ñ×!Ñ!Ô#Ø#ˆÔØ"ˆÕr"   c                ó&   — | j                  «        | S )z^Start the logging thread within the context. Exiting the context will stop the logging thread.rN   rG   s    r    Ú	__enter__zStatusMessageWatcher.__enter__»   s   € à�
‰
ŒØˆr"   c                óf   — |€t        j                  | j                  «       | j                  «        y)zStop the logging thread.N)ÚtimerQ   r3   rL   rR   s       r    Ú__exit__zStatusMessageWatcher.__exit__À   s%   € ð ÐÜ�J‰J�t×/Ñ/Ô0Ø�	‰	�r"   c                ó¢  — 	 	 | j                  | j                  j                  «       «      sy | j                  ry t	        j
                  | j                  «       ŒW# t        $ rk}| j                  j                  j                  |«      r| j                  j                  d«       n | j                  j                  d«       Y d }~y Y d }~y d }~ww xY wrX   )r*   r<   rY   ri   rt   rQ   r   rZ   r[   r\   r   r]   r^   )r   r_   s     r    rF   z0StatusMessageWatcher._log_changed_status_messageÈ   s­   € ð	iØØ×)Ñ)¨$×*:Ñ*:×*>Ñ*>Ó*@ÔAØØ×%Ò%ØÜ—
‘
˜4×-Ñ-Ô.ð øô ò 	iØ×Ñ×,Ñ,×=Ñ=¸cÔBà—‘×'Ñ'Ð(kÕlð —‘×)Ñ)Ð*g×hÑhô mûð	iús"   ‚*A ­A º A Á	CÁ#AC	Ã	C)r?   r   r   r+   r   r   r,   r-   )r,   r   r`   ra   rb   )r/   r0   r1   r2   r   r!   rH   rL   rr   ru   rF   rc   rd   s   @r    rf   rf   Š   st   ø„ ññ ^gÐopÔ]qñ#Ø&ð#Ø3Að#ØQZð#à	õ#ó$ó#óð
Ø2ðØ=QðØ[oðà	ó÷ir"   rf   )Ú
__future__r   rD   rn   rt   r   Údatetimer   r   Útypingr   r   Úapify_client._docsr	   Úapify_client._utils.timer
   ÚloggingÚtypesr   Úapify_client._modelsr   Úapify_client._resource_clientsr   r   r   r7   rf   r4   r"   r    ú<module>r€      sŒ   ðÝ "ã Û Û Ý Ý Ý ß &å )Ý /áÛÝ#å(ßH÷#ñ #ñL ˆGÓôJiÐ 8ó Jió ðJiñZ ˆGÓôKiÐ3ó Kió ñKir"   