我正在尝试构建一个调度程序,其中的一个用例是检查作业依赖项,并将依赖作业的执行延迟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'
根据我的观察,执行作业后,该作业将立即从作业存储中删除,并且可能是由于这个原因,它无法找到该作业ID,但我不确定。
请提供有关如何缓解此问题的建议。
感谢帮助:)
答案 0 :(得分:1)
提交的调度程序发现某个调度程序已运行其课程时,它将删除该作业。这就是这里发生的情况–作业已提交给执行者,但由于其触发器用尽了触发时间,因此它不再存在于作业存储中。尝试添加新作业。