o
    /i                     @   s   d dl Z d dlZd dlZG dd deZG dd deZG dd deZG dd	 d	ZG d
d deZG dd deZ	de
e fddZdS )    Nc                   @      e Zd ZdZdS )BaseTimeoutExceptionzBase exception for timeouts.N__name__
__module____qualname____doc__ r	   r	   K/var/www/html/flask_server/venv/lib/python3.10/site-packages/rq/timeouts.pyr      s    r   c                   @   r   )JobTimeoutExceptionz[Raised when a job takes longer to complete than the allowed maximum
    timeout value.
    Nr   r	   r	   r	   r
   r          r   c                   @   r   )HorseMonitorTimeoutExceptionz]Raised when waiting for a horse exiting takes longer than the maximum
    timeout value.
    Nr   r	   r	   r	   r
   r      r   r   c                   @   s<   e Zd ZdZefddZdd Zdd Zdd	 Zd
d Z	dS )BaseDeathPenaltyz!Base class to setup job timeouts.c                 K   s   || _ || _d S N)_timeout
_exception)selftimeout	exceptionkwargsr	   r	   r
   __init__   s   
zBaseDeathPenalty.__init__c                 C   s   |    d S r   )setup_death_penaltyr   r	   r	   r
   	__enter__#   s   zBaseDeathPenalty.__enter__c                 C   s$   z|    W dS  ty   Y dS w )NF)cancel_death_penaltyr   )r   typevalue	tracebackr	   r	   r
   __exit__&   s   
zBaseDeathPenalty.__exit__c                 C      t  r   NotImplementedErrorr   r	   r	   r
   r   6      z$BaseDeathPenalty.setup_death_penaltyc                 C   r   r   r    r   r	   r	   r
   r   9   r"   z%BaseDeathPenalty.cancel_death_penaltyN)
r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )UnixSignalDeathPenaltyc                 C   s   |  d| j dNz%Task exceeded maximum timeout value (z	 seconds))r   r   )r   signumframer	   r	   r
   handle_death_penalty>   s   z+UnixSignalDeathPenalty.handle_death_penaltyc                 C   s    t  t j| j t | j dS )zSets up an alarm signal and a signal handler that raises
        an exception after the timeout amount (expressed in seconds).
        N)signalSIGALRMr'   alarmr   r   r	   r	   r
   r   A   s   z*UnixSignalDeathPenalty.setup_death_penaltyc                 C   s   t d t  t jt j dS )zgRemoves the death penalty alarm and puts back the system into
        default signal handling.
        r   N)r(   r*   r)   SIG_DFLr   r	   r	   r
   r   H   s   
z+UnixSignalDeathPenalty.cancel_death_penaltyN)r   r   r   r'   r   r   r	   r	   r	   r
   r#   =   s    r#   c                       s@   e Zd Zef fdd	Zdd Zdd Zdd Zd	d
 Z  Z	S )TimerDeathPenaltyc                    sD   t  j fi | t j| _d | _ fdd}|| j_d S )Nc                    s   t | d d d S r$   )superr   )r   argsr   )	__class__r   r   r	   r
   init_with_messageX   s   z5TimerDeathPenalty.__init__.<locals>.init_with_message)r-   r   	threadingcurrent_threadident_target_thread_id_timerr   )r   r   r   r   r0   r/   )r   r   r
   r   Q   s
   zTimerDeathPenalty.__init__c                 C   s   t | j| jS )z7Returns a new timer since timers can only be used once.)r1   Timerr   r'   r   r	   r	   r
   	new_timer]   s   zTimerDeathPenalty.new_timerc                 C   s`   t jt | jt | j}|dkrtd| j |dkr.t jt | jd tddS )zRaises an asynchronous exception in another thread.

        Reference http://docs.python.org/c-api/init.html#PyThreadState_SetAsyncExc for more info.
        r   zInvalid thread ID    z PyThreadState_SetAsyncExc failedN)	ctypes	pythonapiPyThreadState_SetAsyncExcc_longr4   	py_objectr   
ValueErrorSystemError)r   retr	   r	   r
   r'   a   s   z&TimerDeathPenalty.handle_death_penaltyc                 C   s&   | j dkrdS |  | _| j  dS )zStarts the timer.r   N)r   r8   r5   startr   r	   r	   r
   r   o   s   

z%TimerDeathPenalty.setup_death_penaltyc                 C   s"   | j dkrdS | j  d| _dS )zCancels the timer.r   N)r   r5   cancelr   r	   r	   r
   r   v   s   


z&TimerDeathPenalty.cancel_death_penalty)
r   r   r   r   r   r8   r'   r   r   __classcell__r	   r	   r6   r
   r,   P   s    r,   returnc                   C   s   t tdrtS tS )z>Returns the default death penalty class based on the platform.r)   )hasattrr(   r#   r,   r	   r	   r	   r
   get_default_death_penalty_class~   s   
rG   )r:   r(   r1   	Exceptionr   r   r   r   r#   r,   r   rG   r	   r	   r	   r
   <module>   s    !.