o
    i                     @   sp   d dl Z d dlmZ d dlmZ d dlmZ G dd dejZG dd deZG d	d
 d
eZ	G dd dZ
dS )    N)forksafe)service)PeriodicThreadc                	       s   e Zd ZdZddedededd	f fd
dZedd Zej	dd Zdd Z
 fddZ		dddZedd Zdd Z  ZS )PeriodicServicez`A service that runs periodically.

    It automatically resumes its execution after a fork.
            FTintervalno_wait_at_startautorestartreturnNc                    s&   t    || _d | _|| _|| _d S N)super__init__	_interval_worker_no_wait_at_start_autorestart)selfr   r   r	   	__class__ M/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/periodic.pyr      s
   

zPeriodicService.__init__c                 C   s   | j S r   )r   r   r   r   r   r      s   zPeriodicService.intervalc                 C   s   || _ | jr|| j_d S d S r   )r   r   r   )r   valuer   r   r   r      s   c                 O   sD   t | j| jd| jj| jjf | j| jd| _| j	| j_
| j  dS )zStart the periodic service.%s:%s)targetnameon_shutdownr   N)r   r   periodicr   
__module____name__r   r   r   r   __autorestart__startr   argskwargsr   r   r   _start_service&   s   
zPeriodicService._start_servicec                    s*   | j r| j   tt| j|i | dS )zStop the periodic collector.N)r   stopr   r   _stop_servicer"   r   r   r   r'   3   s   
zPeriodicService._stop_servicec                 C   s   | j r| j | d S d S r   r   joinr   timeoutr   r   r   r)   :   s   zPeriodicService.joinc                   C      d S r   r   r   r   r   r   r   B      zPeriodicService.on_shutdownc                 C   r,   r   r   r   r   r   r   r   F   r-   zPeriodicService.periodic)r   FTr   )r   r   __qualname____doc__floatboolr   propertyr   setterr%   r'   r)   staticmethodr   r   __classcell__r   r   r   r   r   	   s     


	

r   c                   @   s   e Zd ZdZdd ZdS )AwakeablePeriodicServicezIA service that runs periodically but that can also be awakened on demand.c                 C   s   | j r
| j   d S d S r   )r   awaker   r   r   r   r7   N   s   zAwakeablePeriodicService.awakeN)r   r   r.   r/   r7   r   r   r   r   r6   K   s    r6   c                       sd   e Zd ZdZdddZdddZdejd	ejddf fd
dZdejd	ejddf fddZ	  Z
S ) ForksafeAwakeablePeriodicServicez7An awakeable periodic service that auto-resets on fork.r
   Nc                 C   s   dS )zReset the service on fork.

        Implement this to clear the service state before restarting the thread
        in the child process.
        Nr   r   r   r   r   resetW   s   z&ForksafeAwakeablePeriodicService.resetc                 C   s   |    d S r   )r9   r   r   r   r   _restart_   s   z)ForksafeAwakeablePeriodicService._restartr#   r$   c                    s"   t  j|i | t| j d S r   )r   r%   r   registerr:   r"   r   r   r   r%   b   s   z/ForksafeAwakeablePeriodicService._start_servicec                    s"   t | j t j|i | d S r   )r   
unregisterr:   r   r'   r"   r   r   r   r'   f   s   z.ForksafeAwakeablePeriodicService._stop_servicer
   N)r   r   r.   r/   r9   r:   typingAnyr%   r'   r5   r   r   r   r   r8   T   s    

&r8   c                   @   sv   e Zd ZdZdeddfddZdddZdd	d
ZdddZdddZ	dd Z
dddZddeje ddfddZdS )TimeraF  
    A simple one-shot timer that calls timeout after the specified interval.

    :param interval: The interval in seconds after which to call timeout.

    The timer can be started with start() and stopped with stop(). The timer can
    be reset with reset(), which stops the timer (if running) and starts it
    again.
    r   r
   Nc                 C   s   || _ |   d S r   )r   _set_worker)r   r   r   r   r   r   v   s   zTimer.__init__c                 C   s*   t | j| jd| jj| jjf dd| _d S )Nr   F)r   r   r   )r   r   	_periodicr   r   r   r   r   r   r   r   rA   z   s   zTimer._set_workerc                 C      | j   d S r   )r   r!   r   r   r   r   r!         zTimer.startc                 C   rC   r   )r   r&   r   r   r   r   r&      rD   z
Timer.stopc                 C   s   t  r   )NotImplementedErrorr   r   r   r   r+      s   zTimer.timeoutc                 C   s   |    |   d S r   )r+   r&   r   r   r   r   rB      s   zTimer._periodicc                 C   s   |    |   |   d S r   )r&   rA   r!   r   r   r   r   r9      s   zTimer.resetr+   c                 C   s   | j | d S r   r(   r*   r   r   r   r)      s   z
Timer.joinr=   r   )r   r   r.   r/   r0   r   rA   r!   r&   r+   rB   r9   r>   Optionalr)   r   r   r   r   r@   k   s    





r@   )r>   ddtrace.internalr   r   ddtrace.internal.threadsr   Servicer   r6   r8   r@   r   r   r   r   <module>   s   B	