Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove drift from job scheduling #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions schedule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def idle_seconds(self):
:return: Number of seconds until
:meth:`next_run <Scheduler.next_run>`.
"""
return (self.next_run - datetime.datetime.now()).total_seconds()
seconds = (self.next_run - datetime.datetime.now()).total_seconds()
return seconds if seconds > 0 else 0


class Job(object):
Expand Down Expand Up @@ -483,8 +484,10 @@ def run(self):
:return: The return value returned by the `job_func`
"""
logger.debug('Running job %s', self)
self.last_run = (
self.next_run if self.should_run else datetime.datetime.now()
)
ret = self.job_func()
self.last_run = datetime.datetime.now()
self._schedule_next_run()
return ret

Expand All @@ -503,7 +506,13 @@ def _schedule_next_run(self):
interval = self.interval

self.period = datetime.timedelta(**{self.unit: interval})
self.next_run = datetime.datetime.now() + self.period
self.next_run = (
(self.last_run or datetime.datetime.now()) + self.period
)
# Move the next run to now if it already should have run, in order to
# skip missed jobs.
if self.should_run:
self.next_run = datetime.datetime.now()
if self.start_day is not None:
if self.unit != 'weeks':
raise ScheduleValueError('`unit` should be \'weeks\'')
Expand Down