-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
""" | ||
import time | ||
import logging | ||
import functools | ||
import threading | ||
import schedule | ||
|
||
|
||
def catch_exceptions(cancel_on_failure=False): | ||
def decorator(job_func): | ||
@functools.wraps(job_func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return job_func(*args, **kwargs) | ||
except Exception as error: | ||
logging.error(f"task {job_func.__name__} failed with ({type(error).__name__}", exc_info=error) | ||
if cancel_on_failure: | ||
return schedule.CancelJob | ||
return wrapper | ||
return decorator | ||
|
||
|
||
def run_continuously(scheduler: schedule.Scheduler, interval: int = 1): | ||
"""Continuously run, while executing pending jobs at each | ||
elapsed time interval. | ||
@return cease_continuous_run: threading. Event which can | ||
be set to cease continuous run. Please note that it is | ||
*intended behavior that run_continuously() does not run | ||
missed jobs*. For example, if you've registered a job that | ||
should run every minute, and you set a continuous run | ||
interval of one hour then your job won't be run 60 times | ||
at each interval but only once. | ||
""" | ||
cease_continuous_run = threading.Event() | ||
|
||
def runner(): | ||
while not cease_continuous_run.is_set(): | ||
logging.debug("running pending jobs") | ||
scheduler.run_pending() | ||
logging.debug("waiting till next job run") | ||
time.sleep(interval) | ||
|
||
continuous_thread = threading.Thread(target=runner, name="scheduler") | ||
continuous_thread.start() | ||
return cease_continuous_run, continuous_thread |