-
Notifications
You must be signed in to change notification settings - Fork 961
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
base: master
Are you sure you want to change the base?
Skip a job #320
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -73,6 +73,13 @@ class CancelJob(object): | |||||||||||
pass | ||||||||||||
|
||||||||||||
|
||||||||||||
class SkipJob(object): | ||||||||||||
""" | ||||||||||||
Can be returned from a job to skip a run. | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
|
||||||||||||
|
||||||||||||
class Scheduler(object): | ||||||||||||
""" | ||||||||||||
Objects instantiated by the :class:`Scheduler <Scheduler>` are | ||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I would invert the if statement like so:
Suggested change
(untested suggestion) That would make it more clear that returning |
||||||||||||
self._schedule_next_run() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||||||||||||
return ret | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, {}) | ||
|
There was a problem hiding this comment.
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 byuntil
is reached).