Skip to content

Commit

Permalink
Issue dbader#319: be able to skip a job
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Detour committed Aug 6, 2019
1 parent 41f88b3 commit cb6f020
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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.
"""
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()
self._schedule_next_run()
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

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

0 comments on commit cb6f020

Please sign in to comment.