agronholm / agronholm/apscheduler

Next wakeup not reset with scheduler.add_job()

Open
#755 7 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
7.6k
Forks
783
Avg merge
4d 8h
Merged PRs (30d)
5

Description

### Things to check first

- [X] I have checked that my issue does not already have a solution in the [FAQ](https://apscheduler.readthedocs.io/en/master/faq.html)

- [X] I have searched the existing issues and didn't find my bug already reported there

- [X] I have checked that my bug is still present in the latest release

### Version

3.9.1

### What happened?

If there is a current job which runs every hour, when run the next wakeup due is set to 3600 seconds from the time of job execution. If another job which is to run every minute is added to the scheduler 1 minute after the original job is executed and the next wakeup set to 3600, the new job will not execute until the next wakeup time which is 59 minutes later. From that point on, the next wakeup will be 300 seconds.
I tried to work around this by calling scheduler.wakeup(), but it did not resolve the problem. For now, my work around is to have a dummy job which runs every 30 seconds.

Logs below are from apscheduler and the flask app which front ends the scheduler. You can see there are two jobs: alerts-processing and Altura_7d_rpt. Initially alerts-processing is running every minute and Altura_7d_rpt is running every hour at the 20m mark. Just prior to execution of the Altura_7d_rpt job, I re-added the alerts-processing job to only run in Nov. scheduler.add_job() with replace_existing=True. Then Altura_7d_rpt executed and the next wakeup was set to 3600 seconds. I then updated alerts-processing to again run every minute. Next wakeup was not refreshed, and no jobs ran until 20 after the next hour.

APSCHEDULER Log:
2023-07-12T18:18:00 : DEBUG : _process_jobs : Looking for jobs to run
2023-07-12T18:18:00 : INFO : run_job : Running job "alerts-processing (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='0'], next run at: 2023-07-12 11:19:00 PDT)" (scheduled at 2023-07-12 11:18:00-07:00)
2023-07-12T18:18:00 : DEBUG : _process_jobs : Next wakeup is due at 2023-07-12 18:19:00+00:00 (in 59.969410 seconds)
2023-07-12T18:18:05 : INFO : run_job : Job "alerts-processing (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='0'], next run at: 2023-07-12 11:19:00 PDT)" executed successfully
2023-07-12T18:19:00 : DEBUG : _process_jobs : Looking for jobs to run
2023-07-12T18:19:00 : INFO : run_job : Running job "alerts-processing (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='0'], next run at: 2023-07-12 11:20:00 PDT)" (scheduled at 2023-07-12 11:19:00-07:00)
2023-07-12T18:19:00 : DEBUG : _process_jobs : Next wakeup is due at 2023-07-12 18:20:00+00:00 (in 59.967274 seconds)
2023-07-12T18:19:05 : INFO : run_job : Job "alerts-processing (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='*', second='0'], next run at: 2023-07-12 11:20:00 PDT)" executed successfully
2023-07-12T18:19:39 : INFO : _real_add_job : Added job "alerts-processing" to job store "default"
2023-07-12T18:19:57 : INFO : _real_add_job : Added job "Altura_7d_rpt" to job store "default"
2023-07-12T18:20:00 : DEBUG : _process_jobs : Looking for jobs to run
2023-07-12T18:20:00 : INFO : run_job : Running job "Altura_7d_rpt (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='20', second='0'], next run at: 2023-07-12 19:20:00 +00)" (scheduled at 2023-07-12 18:20:00+00:00)
2023-07-12T18:20:00 : DEBUG : _process_jobs : Next wakeup is due at 2023-07-12 19:15:00+00:00 (in 3299.974201 seconds)
2023-07-12T18:20:00 : INFO : run_job : Job "Altura_7d_rpt (trigger: cron[year='*', month='*', day='*', week='*', day_of_week='*', hour='*', minute='20', second='0'], next run at: 2023-07-12 19:20:00 +00)" executed successfully
2023-07-12T18:20:05 : INFO : _real_add_job : Added job "Altura_30d_rpt" to job store "default"

FLASK APP Log:
2023-07-12T18:19:39 : DEBUG : add_job : Adding Job: scheduler.add_job(func=exec_job, args=['monitoring-alerting', 'alerts-processing', '/Scripts/alerts_processing/alerts_processing.py', True], id=alerts-processing, max_instances=1, misfire_grace=15, coalesce=True, replace_existing=True, name=alerts-processing, jobstore='default', trigger=cron, second=0, minute=*, hour=*, day_of_week=*, day=*, month=11, week=*, year=*, timezone=America/Los_Angeles)
2023-07-12T18:19:57 : DEBUG : add_job : Adding Job: scheduler.add_job(func=exec_job, args=['reporting', 'Altura_7d_rpt', '/altura7-job-lab.yml', False], id=Altura_7d_rpt, max_instances=1, misfire_grace=15, coalesce=True, replace_existing=True, name=Altura_7d_rpt, jobstore='default', trigger=cron, second=0, minute=20, hour=*, day_of_week=*, day=*, month=*, week=*, year=*, timezone=Atlantic/Azores)
2023-07-12T18:20:00 : DEBUG : exec_job : Running kubectl --kubeconfig /usr/share/nginx/html/k8sapscheduler/.kube/config apply -f /usr/local/bin/scripts/jobs/altura7-job-lab.yml
2023-07-12T18:20:05 : DEBUG : add_job : Adding Job: scheduler.add_job(func=exec_job, args=['reporting', 'Altura_30d_rpt', '/altura30-job-lab.yml', False], id=Altura_30d_rpt, max_instances=1, misfire_grace=15, coalesce=True, replace_existing=True, name=Altura_30d_rpt, jobstore='default', trigger=cron, second=0, minute=22, hour=*, day_of_week=*, day=*, month=*, week=*, year=*, timezone=Atlantic/Azores)

### How can we reproduce the bug?

Using background scheduler:
Add job 1 running every 5 minutes beginning at the 00 minute mark.
Add job 2 running every hour, with first instance beginning at the 03 minute mark

At 00, you should see next wakeup due 60 seconds...
At 01 again you should see next wakeup due 60 seconds

At 02, re-add job 1 to run only in another month other than the present month using scheduler.add_job() with replace_existing=True
At 03, job 2 should execute and the next wakeup due will be 3600 seconds
At 04, re-add job 1 to again run every 5 minutes using scheduler.add_job() with replace_existing=True
Next wakeup is not updated and no jobs run until 03 after the next hour.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.