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

Stop on Exception Parameter #11

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
7 changes: 4 additions & 3 deletions timeloop/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ def _block_main_thread(self):
self.stop()
break

def _start_jobs(self, block):
def _start_jobs(self, block, stop_on_exception):
for j in self.jobs:
j.daemon = not block
j.stop_on_exception = stop_on_exception
j.start()
self.logger.info("Registered job {}".format(j.execute))

Expand All @@ -56,9 +57,9 @@ def stop(self):
self._stop_jobs()
self.logger.info("Timeloop exited.")

def start(self, block=False):
def start(self, block=False, stop_on_exception=False):
self.logger.info("Starting Timeloop..")
self._start_jobs(block=block)
self._start_jobs(block=block,stop_on_exception=stop_on_exception)

self.logger.info("Timeloop now started. Jobs will run based on the interval set")
if block:
Expand Down
7 changes: 6 additions & 1 deletion timeloop/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ def __init__(self, interval, execute, *args, **kwargs):
self.execute = execute
self.args = args
self.kwargs = kwargs
self.stop_on_exception = False

def stop(self):
self.stopped.set()
self.join()

def run(self):
while not self.stopped.wait(self.interval.total_seconds()):
self.execute(*self.args, **self.kwargs)
try:
self.execute(*self.args, **self.kwargs)
except:
if self.stop_on_exception:
break