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

Job pause functionality #2

Open
wants to merge 4 commits 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
32 changes: 22 additions & 10 deletions schedule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

logger = logging.getLogger('schedule')


class Scheduler(object):
def __init__(self):
self.jobs = []
Expand Down Expand Up @@ -83,6 +82,7 @@ def run(cls):
time.sleep(interval)

continuous_thread = ScheduleThread()
continuous_thread.setDaemon(True)
continuous_thread.start()
return cease_continuous_run

Expand Down Expand Up @@ -111,6 +111,10 @@ def every(self, interval=1):
@property
def next_run(self):
"""Datetime when the next job should run."""
if self.jobs:
date_time = min(self.jobs).next_run
else:
return None
return min(self.jobs).next_run

@property
Expand All @@ -122,18 +126,19 @@ def idle_seconds(self):
class Job(object):
"""A periodic job as used by `Scheduler`."""
def __init__(self, interval):
self.interval = interval # pause interval * unit between runs
self.job_func = None # the job job_func to run
self.unit = None # time units, e.g. 'minutes', 'hours', ...
self.at_time = None # optional time at which this job runs
self.last_run = None # datetime of the last run
self.next_run = None # datetime of the next run
self.period = None # timedelta between runs, only valid for
self.interval = interval # pause interval * unit between runs
self.job_func = None # the job job_func to run
self.unit = None # time units, e.g. 'minutes', 'hours', ...
self.at_time = None # optional time at which this job runs
self.last_run = None # datetime of the last run
self.next_run = None # datetime of the next run
self.period = None # timedelta between runs, only valid for
self.paused = False # job paused state

def __lt__(self, other):
"""PeriodicJobs are sortable based on the scheduled time
they run next."""
return self.next_run < other.next_run
return (self.next_run < other.next_run) and (not self.paused)

def __repr__(self):
def format_time(t):
Expand Down Expand Up @@ -237,7 +242,7 @@ def do(self, job_func, *args, **kwargs):
@property
def should_run(self):
"""True if the job should be run now."""
return datetime.datetime.now() >= self.next_run
return (datetime.datetime.now() >= self.next_run) and (not self.paused)

def run(self):
"""Run the job and immediately reschedule it."""
Expand Down Expand Up @@ -265,6 +270,13 @@ def _schedule_next_run(self):
self.at_time > datetime.datetime.now().time()):
self.next_run = self.next_run - datetime.timedelta(days=1)

def pause(self):
self.paused = True
self.next_run_save = self.next_run - datetime.datetime.now()

def unpause(self):
self.paused = False
self.next_run = datetime.datetime.now() + self.next_run_save

# The following methods are shortcuts for not having to
# create a Scheduler instance:
Expand Down
1 change: 1 addition & 0 deletions test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def now(cls):
datetime.datetime = MockDate

mock_job = make_mock_job()
assert schedule.next_run() is None
assert every().minute.do(mock_job).next_run.minute == 16
assert every(5).minutes.do(mock_job).next_run.minute == 20
assert every().hour.do(mock_job).next_run.hour == 13
Expand Down