a
    dܨ                     @   s  d dl mZ d dlmZmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlZd dlZd dlmZ d dlmZ d dlZd d	lmZm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! d dl"m#Z# d dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> zd dl?m@Z@ W n  eAy|   d dlBm@Z@ Y n0 d ZCdZDdZEG dd deFeZGdS )    )print_function)ABCMetaabstractmethodRLock)datetime	timedelta)	getLoggerN)iter_entry_points)get_localzone)SchedulerAlreadyRunningErrorSchedulerNotRunningError)MaxInstancesReachedErrorBaseExecutorThreadPoolExecutor)ConflictingIdErrorJobLookupErrorBaseJobStoreMemoryJobStore)Job)BaseTrigger)asboolasint
astimezone	maybe_reftimedelta_seconds	undefinedTIMEOUT_MAX)SchedulerEventJobEventJobSubmissionEventEVENT_SCHEDULER_STARTEVENT_SCHEDULER_SHUTDOWNEVENT_JOBSTORE_ADDEDEVENT_JOBSTORE_REMOVED	EVENT_ALLEVENT_JOB_MODIFIEDEVENT_JOB_REMOVEDEVENT_JOB_ADDEDEVENT_EXECUTOR_ADDEDEVENT_EXECUTOR_REMOVEDEVENT_ALL_JOBS_REMOVEDEVENT_JOB_SUBMITTEDEVENT_JOB_MAX_INSTANCESEVENT_SCHEDULER_RESUMEDEVENT_SCHEDULER_PAUSED)MutableMapping      c                       s  e Zd ZdZedd edD Zi Zedd edD Zi Z	edd edD Z
i Zi f fd	d
	Zdd Zi dfddZd\ddZed]ddZdd Zdd Zedd Zd^ddZd_dd Zd`d!d"Zdad#d$Zefd%d&Zd'd( Zd)d)d)d)d)eeeedddfd*d+Zd)d)d)d)eeeeddf
d,d-Zdbd.d/Z dcd0d1Z!ddd2d3Z"ded4d5Z#dfd6d7Z$dgd8d9Z%dhd:d;Z&did<d=Z'djd>d?Z(ed@dA Z)dBdC Z*dDdE Z+dFdG Z,dHdI Z-dJdK Z.dLdM Z/dNdO Z0dPdQ Z1dRdS Z2dTdU Z3dVdW Z4dXdY Z5dZd[ Z6  Z7S )kBaseSchedulera  
    Abstract base class for all schedulers.

    Takes the following keyword arguments:

    :param str|logging.Logger logger: logger to use for the scheduler's logging (defaults to
        apscheduler.scheduler)
    :param str|datetime.tzinfo timezone: the default time zone (defaults to the local timezone)
    :param int|float jobstore_retry_interval: the minimum number of seconds to wait between
        retries in the scheduler's main loop if the job store raises an exception when getting
        the list of due jobs
    :param dict job_defaults: default values for newly added jobs
    :param dict jobstores: a dictionary of job store alias -> job store instance or configuration
        dict
    :param dict executors: a dictionary of executor alias -> executor instance or configuration
        dict

    :ivar int state: current running state of the scheduler (one of the following constants from
        ``apscheduler.schedulers.base``: ``STATE_STOPPED``, ``STATE_RUNNING``, ``STATE_PAUSED``)

    .. seealso:: :ref:`scheduler-config`
    c                 c   s   | ]}|j |fV  qd S Nname.0ep r<   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/apscheduler/schedulers/base.py	<genexpr>B       zBaseScheduler.<genexpr>zapscheduler.triggersc                 c   s   | ]}|j |fV  qd S r6   r7   r9   r<   r<   r=   r>   D   r?   zapscheduler.executorsc                 c   s   | ]}|j |fV  qd S r6   r7   r9   r<   r<   r=   r>   F   r?   zapscheduler.jobstoresc                    s`   t t|   i | _|  | _i | _|  | _g | _|  | _	g | _
t| _| j|fi | d S r6   )superr5   __init__
_executors_create_lock_executors_lock
_jobstores_jobstores_lock
_listeners_listeners_lock_pending_jobsSTATE_STOPPEDstate	configure)selfgconfigoptions	__class__r<   r=   rA   M   s    


zBaseScheduler.__init__c                 C   s   t dd S )NzSchedulers cannot be serialized. Ensure that you are not passing a scheduler instance as an argument to a job, or scheduling an instance method where the instance contains a scheduler as an attribute.)	TypeErrorrM   r<   r<   r=   __getstate__Y   s    zBaseScheduler.__getstate__zapscheduler.c           	         s   | j tkrt r8t t fddt|D }i }t|D ]D\}}|d}|}|d}|r|	|i }|d}qf|||< qF|
| | | dS )a  
        Reconfigures the scheduler with the given options.

        Can only be done when the scheduler isn't running.

        :param dict gconfig: a "global" configuration dictionary whose values can be overridden by
            keyword arguments to this method
        :param str|unicode prefix: pick only those keys from ``gconfig`` that are prefixed with
            this string (pass an empty string or ``None`` to use all keys)
        :raises SchedulerAlreadyRunningError: if the scheduler is already running

        c                 3   s,   | ]$\}}|  r|d  |fV  qd S r6   )
startswithr:   keyvalueprefix	prefixlenr<   r=   r>   r   s   

z*BaseScheduler.configure.<locals>.<genexpr>.r   N)rK   rJ   r   lendictsix	iteritemssplitpop
setdefaultupdate
_configure)	rM   rN   rZ   rO   configrW   rX   partsparentr<   rY   r=   rL   ^   s     




zBaseScheduler.configureFc                 C   sB  | j tkrt|   | jL d| jvr8| |  d t	| jD ]\}}|
| | qDW d   n1 sn0    Y  | jx d| jvr| |  d t	| jD ]\}}|
| | q| jD ]\}}}| ||| q| jdd= W d   n1 s0    Y  |rtnt| _ | jd | tt |s>|   dS )af  
        Start the configured executors and job stores and begin processing scheduled jobs.

        :param bool paused: if ``True``, don't start job processing until :meth:`resume` is called
        :raises SchedulerAlreadyRunningError: if the scheduler is already running
        :raises RuntimeError: if running under uWSGI with threads disabled

        defaultNzScheduler started)rK   rJ   r   _check_uwsgirD   rB   add_executor_create_default_executorr_   r`   startrF   rE   add_jobstore_create_default_jobstorerI   _real_add_jobSTATE_PAUSEDSTATE_RUNNING_loggerinfo_dispatch_eventr    r#   wakeup)rM   pausedaliasexecutorstorejobjobstore_aliasreplace_existingr<   r<   r=   rm      s*    	

,
*zBaseScheduler.startTc              	   C   s   | j tkrtt| _ | jl | jF t| jD ]}|| q0t| j	D ]}|  qLW d   n1 sn0    Y  W d   n1 s0    Y  | j
d | tt dS )a>  
        Shuts down the scheduler, along with its executors and job stores.

        Does not interrupt any currently running jobs.

        :param bool wait: ``True`` to wait until all currently executing jobs have finished
        :raises SchedulerNotRunningError: if the scheduler has not been started yet

        NzScheduler has been shut down)rK   rJ   r   rD   rF   r_   
itervaluesrB   shutdownrE   rs   rt   ru   r    r$   )rM   waitry   jobstorer<   r<   r=   r      s    
FzBaseScheduler.shutdownc                 C   s>   | j tkrtn*| j tkr:t| _ | jd | tt	 dS )z
        Pause job processing in the scheduler.

        This will prevent the scheduler from waking up to do job processing until :meth:`resume`
        is called. It will not however stop any already running job processing.

        zPaused scheduler job processingN)
rK   rJ   r   rr   rq   rs   rt   ru   r    r1   rS   r<   r<   r=   pause   s    

zBaseScheduler.pausec                 C   sF   | j tkrtn2| j tkrBt| _ | jd | tt	 | 
  dS )z'Resume job processing in the scheduler.z Resumed scheduler job processingN)rK   rJ   r   rq   rr   rs   rt   ru   r    r0   rv   rS   r<   r<   r=   resume   s    

zBaseScheduler.resumec                 C   s
   | j tkS )z
        Return ``True`` if the scheduler has been started.

        This is a shortcut for ``scheduler.state != STATE_STOPPED``.

        )rK   rJ   rS   r<   r<   r=   running   s    zBaseScheduler.runningri   c                 K   s   | j  || jv rtd| t|tr4|| j|< n6t|tjrZ| d|| | j|< }ntd|j	j
 | jtkr|| | W d   n1 s0    Y  | tt| dS )a  
        Adds an executor to this scheduler.

        Any extra keyword arguments will be passed to the executor plugin's constructor, assuming
        that the first argument is the name of an executor plugin.

        :param str|unicode|apscheduler.executors.base.BaseExecutor executor: either an executor
            instance or the name of an executor plugin
        :param str|unicode alias: alias for the scheduler
        :raises ValueError: if there is already an executor by the given alias

        z;This scheduler already has an executor by the alias of "%s"ry   z9Expected an executor instance or a string, got %s insteadN)rD   rB   
ValueError
isinstancer   r_   string_types_create_plugin_instancerR   rQ   __name__rK   rJ   rm   ru   r    r+   )rM   ry   rx   Zexecutor_optsr<   r<   r=   rk      s"    


*zBaseScheduler.add_executorc                 C   sX   | j " | |}| j|= W d   n1 s.0    Y  |rD|  | tt| dS )z
        Removes the executor by the given alias from this scheduler.

        :param str|unicode alias: alias of the executor
        :param bool shutdown: ``True`` to shut down the executor after
            removing it

        N)rD   _lookup_executorrB   r   ru   r    r,   )rM   rx   r   ry   r<   r<   r=   remove_executor  s    	
&zBaseScheduler.remove_executorc                 K   s   | j  || jv rtd| t|tr4|| j|< n6t|tjrZ| d|| | j|< }ntd|j	j
 | jtkr|| | W d   n1 s0    Y  | tt| | jtkr|   dS )a  
        Adds a job store to this scheduler.

        Any extra keyword arguments will be passed to the job store plugin's constructor, assuming
        that the first argument is the name of a job store plugin.

        :param str|unicode|apscheduler.jobstores.base.BaseJobStore jobstore: job store to be added
        :param str|unicode alias: alias for the job store
        :raises ValueError: if there is already a job store by the given alias

        z;This scheduler already has a job store by the alias of "%s"r   z9Expected a job store instance or a string, got %s insteadN)rF   rE   r   r   r   r_   r   r   rR   rQ   r   rK   rJ   rm   ru   r    r%   rv   )rM   r   rx   Zjobstore_optsr<   r<   r=   rn   #  s&    


*
zBaseScheduler.add_jobstorec                 C   sX   | j " | |}| j|= W d   n1 s.0    Y  |rD|  | tt| dS )z
        Removes the job store by the given alias from this scheduler.

        :param str|unicode alias: alias of the job store
        :param bool shutdown: ``True`` to shut down the job store after removing it

        N)rF   _lookup_jobstorerE   r   ru   r    r&   )rM   rx   r   r   r<   r<   r=   remove_jobstoreH  s    
&zBaseScheduler.remove_jobstorec                 C   s:   | j   | j||f W d   n1 s,0    Y  dS )a:  
        add_listener(callback, mask=EVENT_ALL)

        Adds a listener for scheduler events.

        When a matching event  occurs, ``callback`` is executed with the event object as its
        sole argument. If the ``mask`` parameter is not provided, the callback will receive events
        of all types.

        :param callback: any callable that takes one argument
        :param int mask: bitmask that indicates which events should be
            listened to

        .. seealso:: :mod:`apscheduler.events`
        .. seealso:: :ref:`scheduler-events`

        N)rH   rG   append)rM   callbackmaskr<   r<   r=   add_listenerY  s    zBaseScheduler.add_listenerc                 C   sR   | j 8 t| jD ]\}\}}||kr| j|= qW d   n1 sD0    Y  dS )z*Removes a previously added event listener.N)rH   	enumeraterG   )rM   r   icb_r<   r<   r=   remove_listenern  s    zBaseScheduler.remove_listenerNc                 K   s   |  |||||durt|nd|dur0t|ni |||||	|
d}tdd t|D }t| fi |}| jH | jtkr| j	
|||f | jd n| ||| W d   n1 s0    Y  |S )a
  
        add_job(func, trigger=None, args=None, kwargs=None, id=None,             name=None, misfire_grace_time=undefined, coalesce=undefined,             max_instances=undefined, next_run_time=undefined,             jobstore='default', executor='default',             replace_existing=False, **trigger_args)

        Adds the given job to the job list and wakes up the scheduler if it's already running.

        Any option that defaults to ``undefined`` will be replaced with the corresponding default
        value when the job is scheduled (which happens when the scheduler is started, or
        immediately if the scheduler is already running).

        The ``func`` argument can be given either as a callable object or a textual reference in
        the ``package.module:some.object`` format, where the first half (separated by ``:``) is an
        importable module and the second half is a reference to the callable object, relative to
        the module.

        The ``trigger`` argument can either be:
          #. the alias name of the trigger (e.g. ``date``, ``interval`` or ``cron``), in which case
            any extra keyword arguments to this method are passed on to the trigger's constructor
          #. an instance of a trigger class

        :param func: callable (or a textual reference to one) to run at the given time
        :param str|apscheduler.triggers.base.BaseTrigger trigger: trigger that determines when
            ``func`` is called
        :param list|tuple args: list of positional arguments to call func with
        :param dict kwargs: dict of keyword arguments to call func with
        :param str|unicode id: explicit identifier for the job (for modifying it later)
        :param str|unicode name: textual description of the job
        :param int misfire_grace_time: seconds after the designated runtime that the job is still
            allowed to be run (or ``None`` to allow the job to run no matter how late it is)
        :param bool coalesce: run once instead of many times if the scheduler determines that the
            job should be run more than once in succession
        :param int max_instances: maximum number of concurrently running instances allowed for this
            job
        :param datetime next_run_time: when to first run the job, regardless of the trigger (pass
            ``None`` to add the job as paused)
        :param str|unicode jobstore: alias of the job store to store the job in
        :param str|unicode executor: alias of the executor to run the job with
        :param bool replace_existing: ``True`` to replace an existing job with the same ``id``
            (but retain the number of runs from the existing one)
        :rtype: Job

        Nr<   )triggerry   funcargskwargsidr8   misfire_grace_timecoalescemax_instancesnext_run_timec                 s   s"   | ]\}}|t ur||fV  qd S r6   )r   rV   r<   r<   r=   r>     s   
z(BaseScheduler.add_job.<locals>.<genexpr>zQAdding job tentatively -- it will be properly scheduled when the scheduler starts)_create_triggertupler^   r_   r`   r   rF   rK   rJ   rI   r   rs   rt   rp   )rM   r   r   r   r   r   r8   r   r   r   r   r   ry   r}   trigger_argsZ
job_kwargsr{   r<   r<   r=   add_jobv  s(    2

,zBaseScheduler.add_jobc                    s(    	
fdd}|S )a8  
        scheduled_job(trigger, args=None, kwargs=None, id=None,             name=None, misfire_grace_time=undefined,             coalesce=undefined, max_instances=undefined,             next_run_time=undefined, jobstore='default',             executor='default',**trigger_args)

        A decorator version of :meth:`add_job`, except that ``replace_existing`` is always
        ``True``.

        .. important:: The ``id`` argument must be given if scheduling a job in a persistent job
        store. The scheduler cannot, however, enforce this requirement.

        c                    s.   
j |  	dfi  | S )NT)r   )r   r   r   ry   r   r   r   r   r   r8   r   rM   r   r   r<   r=   inner  s    
z*BaseScheduler.scheduled_job.<locals>.innerr<   )rM   r   r   r   r   r8   r   r   r   r   r   ry   r   r   r<   r   r=   scheduled_job  s    $zBaseScheduler.scheduled_jobc                 K   s   | j D | ||\}}|jf i | |r<| || W d   n1 sP0    Y  | tt|| | jt	kr~| 
  |S )aG  
        Modifies the properties of a single job.

        Modifications are passed to this method as extra keyword arguments.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :return Job: the relevant job instance

        N)rF   _lookup_job_modifyr   
update_jobru   r!   r(   rK   rr   rv   )rM   job_idr   changesr{   r<   r<   r=   
modify_job  s    .
zBaseScheduler.modify_jobc                 K   s6   |  ||}t| j}|d|}| j||||dS )a  
        Constructs a new trigger for a job and updates its next run time.

        Extra keyword arguments are passed directly to the trigger's constructor.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :param trigger: alias of the trigger type or a trigger instance
        :return Job: the relevant job instance

        N)r   r   )r   r   nowtimezoneget_next_fire_timer   )rM   r   r   r   r   r   r   r<   r<   r=   reschedule_job  s    zBaseScheduler.reschedule_jobc                 C   s   | j ||ddS )a  
        Causes the given job not to be executed until it is explicitly resumed.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :return Job: the relevant job instance

        Nr   )r   rM   r   r   r<   r<   r=   	pause_job  s    	zBaseScheduler.pause_jobc                 C   s   | j j | ||\}}t| j}|jd|}|rT| j|||dW  d   S | |j	| W d   n1 sv0    Y  dS )a  
        Resumes the schedule of the given job, or removes the job if its schedule is finished.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :return Job|None: the relevant job instance if the job was rescheduled, or ``None`` if no
            next run time could be calculated and the job was removed

        Nr   )
rF   r   r   r   r   r   r   r   
remove_jobr   )rM   r   r   r{   r   r   r<   r<   r=   
resume_job  s    
zBaseScheduler.resume_jobc                 C   s   |durt dt | j g }| jtkrX| jD ]$\}}}|du sJ||kr0|| q0n4t	| j
D ]&\}}|du s|||krd||  qd|W  d   S 1 s0    Y  dS )a  
        Returns a list of pending jobs (if the scheduler hasn't been started yet) and scheduled
        jobs, either from a specific job store or from all of them.

        If the scheduler has not been started yet, only pending jobs can be returned because the
        job stores haven't been started yet either.

        :param str|unicode jobstore: alias of the job store
        :param bool pending: **DEPRECATED**
        :rtype: list[Job]

        NzThe "pending" option is deprecated -- get_jobs() always returns scheduled jobs if the scheduler has been started and pending jobs otherwise)warningswarnDeprecationWarningrF   rK   rJ   rI   r   r_   r`   rE   extendget_all_jobs)rM   r   pendingjobsr{   rx   r}   rz   r<   r<   r=   get_jobs#  s    
zBaseScheduler.get_jobsc              	   C   sl   | j R z | ||d W W  d   S  tyH   Y W d   dS 0 W d   n1 s^0    Y  dS )a>  
        Returns the Job that matches the given ``job_id``.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that most likely contains the job
        :return: the Job by the given ID, or ``None`` if it wasn't found
        :rtype: Job

        r   N)rF   r   r   r   r<   r<   r=   get_jobB  s
    
 zBaseScheduler.get_jobc           
   
   C   s   d}| j  | jtkrXt| jD ]4\}\}}}|j|kr |d|fv r | j|= |} qq nRt| jD ]D\}}|d|fv rdz|	| |}W  qW qd t
y   Y qdY qd0 qdW d   n1 s0    Y  |du rt
|tt||}	| |	 | jd| dS )a  
        Removes a job, preventing it from being run any more.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :raises JobLookupError: if the job was not found

        NzRemoved job %s)rF   rK   rJ   r   rI   r   r_   r`   rE   r   r   r!   r)   ru   rs   rt   )
rM   r   r   r|   r   r{   rx   r}   rz   eventr<   r<   r=   r   R  s*    	


*
zBaseScheduler.remove_jobc                    s   | j h | jtkr6 r. fdd| jD | _q`g | _n*t| jD ]\}} d|fv rB|  qBW d   n1 st0    Y  | t	t
  dS )z
        Removes all jobs from the specified job store, or all job stores if none is given.

        :param str|unicode jobstore: alias of the job store

        c                    s   g | ]}|d   kr|qS )r3   r<   )r:   r   r   r<   r=   
<listcomp>  s   z1BaseScheduler.remove_all_jobs.<locals>.<listcomp>N)rF   rK   rJ   rI   r_   r`   rE   remove_all_jobsru   r    r-   )rM   r   rx   rz   r<   r   r=   r   y  s    
(zBaseScheduler.remove_all_jobsc           	      C   s   |pt j}| j | jtkrltd|d | jr^| jD ]&\}}}|d|fv r4td| |d q4qtd|d njtt	| j
D ]X\}}|d|fv r|td| |d | }|r|D ]}td| |d qq|td|d q|W d   n1 s0    Y  dS )a  
        print_jobs(jobstore=None, out=sys.stdout)

        Prints out a textual listing of all jobs currently scheduled on either all job stores or
        just a specific one.

        :param str|unicode jobstore: alias of the job store, ``None`` to list jobs from all stores
        :param file out: a file-like object to print to (defaults to  **sys.stdout** if nothing is
            given)

        zPending jobs:)fileNz    %sz    No pending jobszJobstore %s:z    No scheduled jobs)sysstdoutrF   rK   rJ   printrI   sortedr_   r`   rE   r   )	rM   r   outr{   r|   r}   rx   rz   r   r<   r<   r=   
print_jobs  s"    

zBaseScheduler.print_jobsc                 C   s   dS )z
        Notifies the scheduler that there may be jobs due for execution.
        Triggers :meth:`_process_jobs` to be run in an implementation specific manner.
        Nr<   rS   r<   r<   r=   rv     s    zBaseScheduler.wakeupc                 C   s  t |dd ptd| _t|dd p.t | _t|dd| _|	di }t
|	ddt|	d	d
t
|	ddd| _| j  t|	di D ]\}}t|tr| || qt|tr.|dd }|dd }|r| d||}n*|rt |}|f i |}ntd| | || qtd||jjf q| j  t|	di D ]\}}t|tr| || nt|tr|dd }	|dd }|r| d||}
n*|	rt |	}|f i |}
ntd| | |
| ntd||jjf q`d S )Nloggerzapscheduler.schedulerr   jobstore_retry_interval
   job_defaultsr   r3   r   Tr   )r   r   r   Z	executorsclasstypery   zGCannot create executor "%s" -- either "type" or "class" must be definedzFExpected executor instance or dict for executors['%s'], got %s insteadZ	jobstoresr   zHCannot create job store "%s" -- either "type" or "class" must be definedzGExpected job store instance or dict for jobstores['%s'], got %s instead)r   rb   r	   rs   r   r   r   floatr   getr   r   _job_defaultsrB   clearr_   r`   r   r   rk   r2   r   r   rR   rQ   r   rE   r   rn   )rM   rf   r   rx   rX   Zexecutor_classpluginry   clsZjobstore_classr   r<   r<   r=   re     sl    




zBaseScheduler._configurec                 C   s   t  S )zLCreates a default executor store, specific to the particular scheduler type.r   rS   r<   r<   r=   rl     s    z&BaseScheduler._create_default_executorc                 C   s   t  S )zGCreates a default job store, specific to the particular scheduler type.r   rS   r<   r<   r=   ro     s    z&BaseScheduler._create_default_jobstorec                 C   s0   z| j | W S  ty*   td| Y n0 dS )z
        Returns the executor instance by the given name from the list of executors that were added
        to this scheduler.

        :type alias: str
        :raises KeyError: if no executor by the given alias is not found

        zNo such executor: %sN)rB   KeyErrorrM   rx   r<   r<   r=   r      s    	zBaseScheduler._lookup_executorc                 C   s0   z| j | W S  ty*   td| Y n0 dS )z
        Returns the job store instance by the given name from the list of job stores that were
        added to this scheduler.

        :type alias: str
        :raises KeyError: if no job store by the given alias is not found

        zNo such job store: %sN)rE   r   r   r<   r<   r=   r     s    	zBaseScheduler._lookup_jobstorec                 C   s   | j tkr4| jD ] \}}}|j|kr|df  S qn@t| jD ]2\}}|d|fv r@||}|dur@||f  S q@t|dS )aI  
        Finds a job by its ID.

        :type job_id: str
        :param str jobstore_alias: alias of a job store to look in
        :return tuple[Job, str]: a tuple of job, jobstore alias (jobstore alias is None in case of
            a pending job)
        :raises JobLookupError: if no job by the given ID is found.

        N)	rK   rJ   rI   r   r_   r`   rE   Z
lookup_jobr   )rM   r   r|   r{   rx   r}   rz   r<   r<   r=   r     s    


zBaseScheduler._lookup_jobc              	   C   sx   | j  t| j}W d   n1 s&0    Y  |D ]>\}}|j|@ r4z|| W q4 typ   | jd Y q40 q4dS )z~
        Dispatches the given event to interested listeners.

        :param SchedulerEvent event: the event to send

        NzError notifying listener)rH   r   rG   codeBaseExceptionrs   	exception)rM   r   Z	listenersr   r   r<   r<   r=   ru   6  s    (
zBaseScheduler._dispatch_eventc                 C   s$   t jd}t|dds tddS )z9Check if we're running under uWSGI with threads disabled.ZuwsgiZhas_threadsTzThe scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the scheduler to work.N)r   modulesr   getattrRuntimeError)rM   Zuwsgi_moduler<   r<   r=   rj   G  s    zBaseScheduler._check_uwsgic           
      C   s   i }t | jD ]\}}t||s|||< qt|dsTt| j}|jd||d< |j	f i | | 
|}z|| W n$ ty   |r|| n Y n0 ||_tt|j|}	| |	 | jd|j| | jtkr|   dS )z
        :param Job job: the job to add
        :param bool replace_existing: ``True`` to use update_job() in case the job already exists
            in the store

        r   Nz Added job "%s" to job store "%s")r_   r`   r   hasattrr   r   r   r   r   r   r   r   r   r   Z_jobstore_aliasr!   r*   r   ru   rs   rt   r8   rK   rr   rv   )
rM   r{   r|   r}   ZreplacementsrW   rX   r   rz   r   r<   r<   r=   rp   O  s*    





zBaseScheduler._real_add_jobc                 C   s   | j | jtf| j| jtf| j| jtfd| \}}}z|| }W nX t	y   ||v r|| 
  }||< t||std|ntd||Y n0 |f i |S )zTCreates an instance of the given plugin type, loading the plugin first if necessary.)r   r   ry   z1The {0} entry point does not point to a {0} classz"No {0} by the name "{1}" was found)_trigger_plugins_trigger_classesr   _jobstore_plugins_jobstore_classesr   _executor_plugins_executor_classesr   r   load
issubclassrR   formatLookupError)rM   type_rx   Zconstructor_kwargsZplugin_containerZclass_containerZ
base_classZ
plugin_clsr<   r<   r=   r   {  s"    

z%BaseScheduler._create_plugin_instancec                 C   sT   t |tr|S |d u rd}nt |tjs8td|jj |d| j | 	d||S )Ndatez5Expected a trigger instance or string, got %s insteadr   r   )
r   r   r_   r   rR   rQ   r   rc   r   r   )rM   r   r   r<   r<   r=   r     s    
zBaseScheduler._create_triggerc                 C   s   t  S )z Creates a reentrant lock object.r   rS   r<   r<   r=   rC     s    zBaseScheduler._create_lockc                 C   s  | j tkr| jd dS | jd t| j}d}g }| j t	| j
D ]\}}z||}W n` ty } zH| jd|| |t| jd }|r||kr|}W Y d}~qPW Y d}~n
d}~0 0 |D ]H}	z| |	j}
W n8 ty   | jd|	j|	 | |	j| Y qY n0 |	|}|rB|	jrB|dd n|}|rz|
|	| W nd ty   | jd|	|	j tt|	j||}|| Y n@ ty   | jd	|	|	j Y n0 tt|	j||}|| |	j !|d |}|r
|	j"|d
 |#|	 q| |	j| q|$ }|rP|du s8||k rP|%| j}qPW d   n1 s\0    Y  |D ]}| &| qj| j tkrd}| jd nD|du rd}| jd n(t't(t)|| dt*}| jd|| |S )a!  
        Iterates through jobs in every jobstore, starts jobs that are due and figures out how long
        to wait for the next round.

        If the ``get_due_jobs()`` call raises an exception, a new wakeup is scheduled in at least
        ``jobstore_retry_interval`` seconds.

        z*Scheduler is paused -- not processing jobsNzLooking for jobs to runz,Error getting due jobs from job store %r: %s)secondszLExecutor lookup ("%s") failed for job "%s" -- removing it from the job storezOExecution of job "%s" skipped: maximum number of running instances reached (%d)z*Error submitting job "%s" to executor "%s"r   z5Scheduler is paused; waiting until resume() is calledz%No jobs; waiting until a job is addedr   z(Next wakeup is due at %s (in %f seconds))+rK   rq   rs   debugr   r   r   rF   r_   r`   rE   Zget_due_jobs	Exceptionwarningr   r   r   ry   r   errorr   r   Z_get_run_timesr   Z
submit_jobr   r   r"   r/   r   r   r.   r   r   r   r   Zget_next_run_timer   ru   minmaxr   r   )rM   r   Znext_wakeup_timeeventsr|   r   Zdue_jobseZretry_wakeup_timer{   ry   Z	run_timesr   Zjob_next_runZjobstore_next_run_timeZwait_secondsr<   r<   r=   _process_jobs  s    	

"





.

zBaseScheduler._process_jobs)F)T)ri   )T)ri   )T)N)NN)N)N)NN)N)N)N)NN)8r   
__module____qualname____doc__r^   r
   r   r   r   r   r   r   rA   rT   rL   rm   r   r   r   r   propertyr   rk   r   rn   r   r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   re   rl   ro   r   r   r   ru   rj   rp   r   r   rC   r   __classcell__r<   r<   rP   r=   r5   *   sp   '
,

	
!

%


M







'

!

>,r5   )H
__future__r   abcr   r   	threadingr   r   r   loggingr	   r   r   pkg_resourcesr
   Ztzlocalr   r_   Zapscheduler.schedulersr   r   Zapscheduler.executors.baser   r   Zapscheduler.executors.poolr   Zapscheduler.jobstores.baser   r   r   Zapscheduler.jobstores.memoryr   Zapscheduler.jobr   Zapscheduler.triggers.baser   Zapscheduler.utilr   r   r   r   r   r   r   Zapscheduler.eventsr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   collections.abcr2   ImportErrorcollectionsrJ   rr   rq   with_metaclassr5   r<   r<   r<   r=   <module>   s4   $P