Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Imports 

2 

3import functools 

4import schedule 

5 

6# Exports 

7 

8__all__ = ( 

9 "catch_exceptions", 

10) 

11 

12# Functions 

13 

14 

15def catch_exceptions(cancel_on_failure=False): 

16 """A decorator to be applied to a scheduled job callback. Straight from the python-schedule documentation. 

17 

18 :param cancel_on_failure: Indicates the job should be canceled when an exception is raised. 

19 :type cancel_on_failure: bool 

20 

21 .. code-block:: python 

22 

23 from superdjango.contrib.scheduler.decorators import catch_exceptions 

24 

25 @catch_exceptions(cancel_on_failure=True) 

26 def bad_task(): 

27 return 1 / 0 

28 

29 schedule.every(5).minutes.do(bad_task) 

30 

31 """ 

32 def catch_exceptions_decorator(job_func): 

33 @functools.wraps(job_func) 

34 def wrapper(*args, **kwargs): 

35 try: 

36 return job_func(*args, **kwargs) 

37 except: 

38 import traceback 

39 print(traceback.format_exc()) 

40 if cancel_on_failure: 

41 return schedule.CancelJob 

42 return wrapper 

43 return catch_exceptions_decorator