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

Skip a job #320

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion schedule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class CancelJob(object):
pass


class SkipJob(object):
"""
Can be returned from a job to skip a run.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's elaborate a bit more what it means to skip a job. We could mention that the job scheduling is not changed and that the job will run the next time run_pending is called (unless the job's deadline set by until is reached).

"""
pass


class Scheduler(object):
"""
Objects instantiated by the :class:`Scheduler <Scheduler>` are
Expand Down Expand Up @@ -483,7 +490,8 @@ def run(self):
"""
logger.info('Running job %s', self)
ret = self.job_func()
self.last_run = datetime.datetime.now()
if not isinstance(ret, SkipJob) and ret is not SkipJob:
self.last_run = datetime.datetime.now()
Comment on lines +493 to +494
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would invert the if statement like so:

Suggested change
if not isinstance(ret, SkipJob) and ret is not SkipJob:
self.last_run = datetime.datetime.now()
if isinstance(ret, SkipJob) or ret is SkipJob:
return ret
self.last_run = datetime.datetime.now()

(untested suggestion)

That would make it more clear that returning SkipJob is a special flow. And the normal flow would continue on the same level.

self._schedule_next_run()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should self._schedule_next_run() also be included in the if statements body?

return ret

Expand Down
25 changes: 25 additions & 0 deletions test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,31 @@ def stop_job():
schedule.run_all()
assert len(schedule.jobs) == 0

def test_skip_job(self):
skip = True

def skip_job():
if skip:
return schedule.SkipJob
else:
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests! I like them 👍


job = every().second.do(skip_job)

assert len(schedule.jobs) == 1
assert job.last_run is None

schedule.run_all()

assert len(schedule.jobs) == 1
assert job.last_run is None

skip = False
schedule.run_all()

assert len(schedule.jobs) == 1
assert job.last_run is not None

def test_tag_type_enforcement(self):
job1 = every().second.do(make_mock_job(name='job1'))
self.assertRaises(TypeError, job1.tag, {})
Expand Down