Я пытаюсь построить планировщик, и один из вариантов его использования - проверка зависимостей заданий и задержка выполнения зависимых заданий с разницей в 20 минут.
Вот мой пример,
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import logging
sched = BlockingScheduler()
log_file_path="path\to\log\file"
@sched.scheduled_job('cron', day_of_week='mon-fri', hour=19, minute=53)
def scheduled_job():
sched.add_job(run_job, id='demo_reschedule')
logging.info("Schdeuled job at {}".format(datetime.datetime.now()))
def run_job():
now = datetime.datetime.now()
now_plus_20 = now + datetime.timedelta(minutes = 20)
sched.reschedule_job('demo_reschedule',trigger='date',run_date=now_plus_20)
logging.info("Rescheduled Job demo_reschedule to new time {}".format(now_plus_20))
if __name__ == "__main__":
logging.basicConfig(filename=log_file_path,
filemode='a',
format=('[%(asctime)s] %(levelname)-8s %(name)-12s %(message)s'),
datefmt='%H:%M:%S',
level=logging.INFO)
logging.info("Starting scheduler")
sched.start()
Метод run_job успешно добавляется, но при его выполнении я получаю следующую ошибку
[19:51:14] INFO root Starting scheduler
[19:51:14] INFO apscheduler.scheduler Added job "scheduled_job" to job store "default"
[19:51:14] INFO apscheduler.scheduler Scheduler started
[19:53:00] INFO apscheduler.executors.default Running job "scheduled_job (trigger: cron[day_of_week='mon-fri', hour='19', minute='53'], next run at: 2019-06-07 19:53:00 IST)" (scheduled at 2019-06-07 19:53:00+05:30)
[19:53:00] INFO apscheduler.scheduler Added job "run_job" to job store "default"
[19:53:00] INFO root Schdeuled job at 2019-06-07 19:53:00.024887
[19:53:00] INFO apscheduler.executors.default Job "scheduled_job (trigger: cron[day_of_week='mon-fri', hour='19', minute='53'], next run at: 2019-06-10 19:53:00 IST)" executed successfully
[19:53:00] INFO apscheduler.executors.default Running job "run_job (trigger: date[2019-06-07 19:53:00 IST], next run at: 2019-06-07 19:53:00 IST)" (scheduled at 2019-06-07 19:53:00.023890+05:30)
[19:53:00] INFO apscheduler.scheduler Removed job demo_reschedule
[19:53:00] ERROR apscheduler.executors.default Job "run_job (trigger: date[2019-06-07 19:53:00 IST], next run at: 2019-06-07 19:53:00 IST)" raised an exception
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "path/to/demo_reschedule.py", line 33, in run_job
sched.reschedule_job('demo_reschedule',jobstore=None,trigger='date',run_date=now_plus_20)
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\schedulers\base.py", line 511, in reschedule_job
return self.modify_job(job_id, jobstore, trigger=trigger, next_run_time=next_run_time)
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\schedulers\base.py", line 483, in modify_job
job, jobstore = self._lookup_job(job_id, jobstore)
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\schedulers\base.py", line 816, in _lookup_job
raise JobLookupError(job_id)
apscheduler.jobstores.base.JobLookupError: 'No job by the id of demo_reschedule was found'
Согласно моим наблюдениям, после выполнения задания оно немедленно удаляется из хранилища заданий и может быть из-за этого не может найти этот идентификатор задания, но я не уверен.
Добрый совет, как решить эту проблему.
Ценю помощь:)