I am trying to build a scheduler and one of the use case in it is to check for job dependencies and delay the execution of dependent job by a delta say 20 minutes.
Following is my example,
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()
The run_job method gets added successfully but when it executes, i receive the following error,
[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'
As per my observations, after executing the job it is immediately removed from the job store and may be due to this it is not able to find that job id but I am not sure.
Kindly advice on how to mitigate this issue.
Appreciate the help :)
When the scheduler submits finds that a schedule has run its course, it will delete the job. This is what happens here – the job has been submitted to the executor but it no longer exists in the job store because its trigger has run out of fire times. Try adding a new job instead.