Source code for superdjango.contrib.scheduler.decorators

# Imports

import functools
import schedule

# Exports

__all__ = (
    "catch_exceptions",
)

# Functions


[docs]def catch_exceptions(cancel_on_failure=False): """A decorator to be applied to a scheduled job callback. Straight from the python-schedule documentation. :param cancel_on_failure: Indicates the job should be canceled when an exception is raised. :type cancel_on_failure: bool .. code-block:: python from superdjango.contrib.scheduler.decorators import catch_exceptions @catch_exceptions(cancel_on_failure=True) def bad_task(): return 1 / 0 schedule.every(5).minutes.do(bad_task) """ def catch_exceptions_decorator(job_func): @functools.wraps(job_func) def wrapper(*args, **kwargs): try: return job_func(*args, **kwargs) except: import traceback print(traceback.format_exc()) if cancel_on_failure: return schedule.CancelJob return wrapper return catch_exceptions_decorator