dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.71k stars 960 forks source link

What happens when a job is "missed" at the scheduled time? #554

Closed Noam5 closed 1 year ago

Noam5 commented 1 year ago

For example the code:

import schedule
import time

def job(t):
    print "I'm working...", t
    return

schedule.every().day.at("10:00").do(job,'It is 01:00')

while True:
    schedule.run_pending()
    time.sleep(60) # wait one minute

But the computer was hibernating until 11:00? will the task still be scheduled? what about 23 hours later?

SijmenHuizenga commented 1 year ago

In the example you provided, the job is scheduled to run every day at 10:00. If the computer is hibernating until 11:00, the scheduled task will be run whenever the computer wakes up and the run_pending() function is called.

Every job has an next_run variable with a datetime that indicates when the next job should run. This can be seen when you print the job:

print(repr(schedule.every().day.at("10:00").do(job,'It is 01:00')))
# Every 1 day at 10:00:00 do job('It is 01:00') (last run: [never], next run: 2023-04-11 10:00:00)

The run_pending simply looks at all the jobs' next_run, and whenever the current time is equal or after the next_run, it will run the job and calculate the new next_run. So when the program execution was paused (run_pending was not called for a long time), then whenever the next time run_pending runs, all missed jobs are ran.

Happy scheduling!