-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_decorators.py
42 lines (36 loc) · 1.25 KB
/
custom_decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from emailfunctions import send_email
import traceback
def logerrors(
to_email,
cron_file,
subject='Cron Error',
include_traceback=True,
):
""" Decorator to send an email if a function fails.
args:
to_email: needs to be a list of emails to notify, ie.
cron_file: some string to include in the email noting the file that
you're running, this will also be contained in the traceback but
this is also helpful.
kwargs:
subject: subject of email
include_traceback: whether or not to include traceback in email
example:
@logerrors(['[email protected]'], 'Sales Daily Report')
def main():
# all the functions to run your script
if __name__ == '__main__':
main()
"""
def decorator(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception, e:
message = 'Error runinng cron file: %s.\n\n' % cron_file
if include_traceback:
message += traceback.format_exc()
send_email(to=to_email, subject=subject, message=message)
return wrapper
return decorator