Scheduling confusion
- Dominant language
- Python
- Stars
- 12.3k
- Forks
- 999
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
I found some confusing behaviour in periodic job scheduling algorithm. I do not know if this is bug or feature. I just would like to share my thoughts regarding this.
Let me show you an example:
I have job that needs to be run every 6 seconds. The job itself takes about 2-4 seconds to complete. So, I'm expecting my job to be run every 6 seconds (if possible).
Unfortunately current algorithm doesn't takes into account job run time. So, first run is indeed scheduled after 6 seconds, but next run will be scheduled after job elapsed time plus 6 seconds. For example: job took 4 seconds to complete, so scheduler will scheduler next job run after 4 + 6 = 10 seconds.
Fortunately, if someone is interested I found quiet easy way to remedy my problem. We just need to slightly modify two methods of Job class:
First, method Job.run(self) should look like this (last_run is now time when job was started):
```
class Job(schedule.Job):
def run(self):
logger.info('Running job %s', self)
self.last_run = datetime.now()
ret = self.job_func()
self._schedule_next_run()
return ret
```
Second, we need to modify Job._schedule_next_run(self) by replacing line:
` self.next_run = datetime.datetime.now() + self.period`
with:
` self.next_run = self.last_run + self.period if self.last_run else datetime.now() + self.period`
Now, next job run will be scheduled like this: last_run + period and this is exactly what I was expecting. In case job elapsed time is longer that period, job will run as soon as possible.
I didn't tested this yet with jobs that needs to be run at given time but is should work.
Thanks!
Konrad
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.