o
    _J@h                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZ G dd deZG d	d
 d
edZdd Zdd ZdS )    N)ABCMetaabstractmethod)defaultdict)datetime	timedeltatimezone)	format_tb)EVENT_JOB_ERROREVENT_JOB_EXECUTEDEVENT_JOB_MISSEDJobExecutionEventc                       s   e Zd Z fddZ  ZS )MaxInstancesReachedErrorc                    s   t  d|j|jf  d S )NzAJob "%s" has already reached its maximum number of instances (%d))super__init__idmax_instances)selfjob	__class__ M/var/www/html/venv/lib/python3.10/site-packages/apscheduler/executors/base.pyr      s
   
z!MaxInstancesReachedError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                       sj   e Zd ZdZdZdZedZ fddZ	dd Z
dd	d
Zdd Zedd Zdd ZdddZ  ZS )BaseExecutorzRAbstract base class that defines the interface that every executor must implement.Nzapscheduler.executorsc                    s   t    tdd | _d S )Nc                   S      dS )Nr   r   r   r   r   r   <lambda>"       z'BaseExecutor.__init__.<locals>.<lambda>)r   r   r   
_instances)r   r   r   r   r       s   
zBaseExecutor.__init__c                 C   s&   || _ | | _td| | _dS )av  
        Called by the scheduler when the scheduler is being started or when the executor is being
        added to an already running scheduler.

        :param apscheduler.schedulers.base.BaseScheduler scheduler: the scheduler that is starting
            this executor
        :param str|unicode alias: alias of this executor as it was assigned to the scheduler

        zapscheduler.executors.N)
_schedulerZ_create_lock_locklogging	getLogger_logger)r   Z	scheduleraliasr   r   r   start$   s   

zBaseExecutor.startTc                 C   r   )z
        Shuts down this executor.

        :param bool wait: ``True`` to wait until all submitted jobs
            have been executed
        Nr   )r   waitr   r   r   shutdown2   r   zBaseExecutor.shutdownc                 C   sx   | j dus	J d| j & | j|j |jkrt|| || | j|j  d7  < W d   dS 1 s5w   Y  dS )aH  
        Submits job for execution.

        :param Job job: job to execute
        :param list[datetime] run_times: list of datetimes specifying
            when the job should have been run
        :raises MaxInstancesReachedError: if the maximum number of
            allowed instances for this job has been reached

        Nz&This executor has not been started yet   )r"   r    r   r   r   _do_submit_jobr   r   	run_timesr   r   r   
submit_job:   s   "zBaseExecutor.submit_jobc                 C   r   )z>Performs the actual task of scheduling `run_job` to be called.Nr   r,   r   r   r   r+   M   r   zBaseExecutor._do_submit_jobc                 C   sh   | j  | j|  d8  < | j| dkr| j|= W d   n1 s"w   Y  |D ]}| j| q)dS )z
        Called by the executor with the list of generated events when :func:`run_job` has been
        successfully called.

        r*   r   N)r"   r    r!   Z_dispatch_event)r   job_ideventseventr   r   r   _run_job_successQ   s   zBaseExecutor._run_job_successc                 C   sp   | j  | j|  d8  < | j| dkr| j|= W d   n1 s"w   Y  |j||f}| jjd||d dS )zRCalled by the executor with the exception if there is an error  calling `run_job`.r*   r   NzError running job %s)exc_info)r"   r    r   r%   error)r   r/   exc	tracebackr3   r   r   r   _run_job_error_   s   zBaseExecutor._run_job_error)T)N)r   r   r   __doc__r!   r"   r#   r$   r%   r   r'   r)   r.   r   r+   r2   r7   r   r   r   r   r   r      s    


r   )	metaclassc                 C   s&  g }t |}|D ]}| jdur5ttj| }t| jd}||kr5|t	t
| j|| |d| | q	|d| | z| j| ji | j}	W n4 ty|   t dd \}
}dt|}|t	t| j|||
|d |d|  t| ~Y q	w |t	t| j|||	d	 |d
|  q	|S )zx
    Called by executors to run the job. Returns a list of scheduler events to be dispatched by the
    scheduler.

    Nseconds%Run time of job "%s" was missed by %s"Running job "%s" (scheduled at %s)r*    	exceptionr6   Job "%s" raised an exceptionretvalJob "%s" executed successfullyr#   r$   Zmisfire_grace_timer   nowr   utcr   appendr   r   r   warninginfofuncargskwargsBaseExceptionsysr3   joinr   r	   r@   r6   clear_framesr
   r   Zjobstore_aliasr-   Zlogger_namer0   loggerZrun_time
differenceZ
grace_timerC   r5   tbZformatted_tbr   r   r   run_jobj   sR   




rV   c                    s,  g }t |}|D ]}| jdur6ttj| }t| jd}||kr6|t	t
| j|| |d| | q
|d| | z| j| ji | jI dH }	W n3 ty   t dd \}
}dt|}|t	t| j|||
|d |d|  t| Y q
w |t	t| j|||	d	 |d
|  q
|S )zCoroutine version of run_job().Nr:   r<   r=   r*   r>   r?   rA   rB   rD   rE   rR   r   r   r   run_coroutine_job   sR   



rW   )r#   rO   r6   abcr   r   collectionsr   r   r   r   r   Zapscheduler.eventsr	   r
   r   r   	Exceptionr   r   rV   rW   r   r   r   r   <module>   s    Q7