diff --git a/schedule/__init__.py b/schedule/__init__.py index e6ebe156..63ab9ab1 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -42,7 +42,6 @@ logger = logging.getLogger('schedule') - class Scheduler(object): def __init__(self): self.jobs = [] @@ -83,6 +82,7 @@ def run(cls): time.sleep(interval) continuous_thread = ScheduleThread() + continuous_thread.setDaemon(True) continuous_thread.start() return cease_continuous_run @@ -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 @@ -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): @@ -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.""" @@ -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: diff --git a/test_schedule.py b/test_schedule.py index 6f86011e..895d7254 100644 --- a/test_schedule.py +++ b/test_schedule.py @@ -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