about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/apscheduler/executors/base.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/apscheduler/executors/base.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-master.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to '.venv/lib/python3.12/site-packages/apscheduler/executors/base.py')
-rw-r--r--.venv/lib/python3.12/site-packages/apscheduler/executors/base.py205
1 files changed, 205 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/apscheduler/executors/base.py b/.venv/lib/python3.12/site-packages/apscheduler/executors/base.py
new file mode 100644
index 00000000..d690a38d
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/apscheduler/executors/base.py
@@ -0,0 +1,205 @@
+import logging
+import sys
+import traceback
+from abc import ABCMeta, abstractmethod
+from collections import defaultdict
+from datetime import datetime, timedelta, timezone
+from traceback import format_tb
+
+from apscheduler.events import (
+    EVENT_JOB_ERROR,
+    EVENT_JOB_EXECUTED,
+    EVENT_JOB_MISSED,
+    JobExecutionEvent,
+)
+
+
+class MaxInstancesReachedError(Exception):
+    def __init__(self, job):
+        super().__init__(
+            'Job "%s" has already reached its maximum number of instances (%d)'
+            % (job.id, job.max_instances)
+        )
+
+
+class BaseExecutor(metaclass=ABCMeta):
+    """Abstract base class that defines the interface that every executor must implement."""
+
+    _scheduler = None
+    _lock = None
+    _logger = logging.getLogger("apscheduler.executors")
+
+    def __init__(self):
+        super().__init__()
+        self._instances = defaultdict(lambda: 0)
+
+    def start(self, scheduler, alias):
+        """
+        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
+
+        """
+        self._scheduler = scheduler
+        self._lock = scheduler._create_lock()
+        self._logger = logging.getLogger(f"apscheduler.executors.{alias}")
+
+    def shutdown(self, wait=True):
+        """
+        Shuts down this executor.
+
+        :param bool wait: ``True`` to wait until all submitted jobs
+            have been executed
+        """
+
+    def submit_job(self, job, run_times):
+        """
+        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
+
+        """
+        assert self._lock is not None, "This executor has not been started yet"
+        with self._lock:
+            if self._instances[job.id] >= job.max_instances:
+                raise MaxInstancesReachedError(job)
+
+            self._do_submit_job(job, run_times)
+            self._instances[job.id] += 1
+
+    @abstractmethod
+    def _do_submit_job(self, job, run_times):
+        """Performs the actual task of scheduling `run_job` to be called."""
+
+    def _run_job_success(self, job_id, events):
+        """
+        Called by the executor with the list of generated events when :func:`run_job` has been
+        successfully called.
+
+        """
+        with self._lock:
+            self._instances[job_id] -= 1
+            if self._instances[job_id] == 0:
+                del self._instances[job_id]
+
+        for event in events:
+            self._scheduler._dispatch_event(event)
+
+    def _run_job_error(self, job_id, exc, traceback=None):
+        """Called by the executor with the exception if there is an error  calling `run_job`."""
+        with self._lock:
+            self._instances[job_id] -= 1
+            if self._instances[job_id] == 0:
+                del self._instances[job_id]
+
+        exc_info = (exc.__class__, exc, traceback)
+        self._logger.error("Error running job %s", job_id, exc_info=exc_info)
+
+
+def run_job(job, jobstore_alias, run_times, logger_name):
+    """
+    Called by executors to run the job. Returns a list of scheduler events to be dispatched by the
+    scheduler.
+
+    """
+    events = []
+    logger = logging.getLogger(logger_name)
+    for run_time in run_times:
+        # See if the job missed its run time window, and handle
+        # possible misfires accordingly
+        if job.misfire_grace_time is not None:
+            difference = datetime.now(timezone.utc) - run_time
+            grace_time = timedelta(seconds=job.misfire_grace_time)
+            if difference > grace_time:
+                events.append(
+                    JobExecutionEvent(
+                        EVENT_JOB_MISSED, job.id, jobstore_alias, run_time
+                    )
+                )
+                logger.warning('Run time of job "%s" was missed by %s', job, difference)
+                continue
+
+        logger.info('Running job "%s" (scheduled at %s)', job, run_time)
+        try:
+            retval = job.func(*job.args, **job.kwargs)
+        except BaseException:
+            exc, tb = sys.exc_info()[1:]
+            formatted_tb = "".join(format_tb(tb))
+            events.append(
+                JobExecutionEvent(
+                    EVENT_JOB_ERROR,
+                    job.id,
+                    jobstore_alias,
+                    run_time,
+                    exception=exc,
+                    traceback=formatted_tb,
+                )
+            )
+            logger.exception('Job "%s" raised an exception', job)
+
+            # This is to prevent cyclic references that would lead to memory leaks
+            traceback.clear_frames(tb)
+            del tb
+        else:
+            events.append(
+                JobExecutionEvent(
+                    EVENT_JOB_EXECUTED, job.id, jobstore_alias, run_time, retval=retval
+                )
+            )
+            logger.info('Job "%s" executed successfully', job)
+
+    return events
+
+
+async def run_coroutine_job(job, jobstore_alias, run_times, logger_name):
+    """Coroutine version of run_job()."""
+    events = []
+    logger = logging.getLogger(logger_name)
+    for run_time in run_times:
+        # See if the job missed its run time window, and handle possible misfires accordingly
+        if job.misfire_grace_time is not None:
+            difference = datetime.now(timezone.utc) - run_time
+            grace_time = timedelta(seconds=job.misfire_grace_time)
+            if difference > grace_time:
+                events.append(
+                    JobExecutionEvent(
+                        EVENT_JOB_MISSED, job.id, jobstore_alias, run_time
+                    )
+                )
+                logger.warning('Run time of job "%s" was missed by %s', job, difference)
+                continue
+
+        logger.info('Running job "%s" (scheduled at %s)', job, run_time)
+        try:
+            retval = await job.func(*job.args, **job.kwargs)
+        except BaseException:
+            exc, tb = sys.exc_info()[1:]
+            formatted_tb = "".join(format_tb(tb))
+            events.append(
+                JobExecutionEvent(
+                    EVENT_JOB_ERROR,
+                    job.id,
+                    jobstore_alias,
+                    run_time,
+                    exception=exc,
+                    traceback=formatted_tb,
+                )
+            )
+            logger.exception('Job "%s" raised an exception', job)
+            traceback.clear_frames(tb)
+        else:
+            events.append(
+                JobExecutionEvent(
+                    EVENT_JOB_EXECUTED, job.id, jobstore_alias, run_time, retval=retval
+                )
+            )
+            logger.info('Job "%s" executed successfully', job)
+
+    return events