Skip to content

Instantly share code, notes, and snippets.

@hardik-vala
Created May 1, 2016 04:42
Show Gist options
  • Save hardik-vala/4f95ee17aae5ce4c323cd1d4cb18e013 to your computer and use it in GitHub Desktop.
Save hardik-vala/4f95ee17aae5ce4c323cd1d4cb18e013 to your computer and use it in GitHub Desktop.
Example illustrating how to use a decorator to notify you via text message if a long running function crashes.
"""
Example illustrating how to use a decorator to notify you via text message if a
long running function crashes.
"""
import os
import time
# Phone number (You may want to change this).
NUMBER = 1234567890
def wrapper(f):
def notifier(*args, **kwargs):
try:
f(args, kwargs)
except Exception as e:
# Check out textbelt.com to learn more about how to use their API.
os.system("curl -X POST http://textbelt.com/canada -d number=%s -d \"message=%s\"" % (NUMBER, str(e)))
return notifier
@wrapper
def my_long_running_functio(arg, kwarg="kwarg"):
time.sleep(5)
raise RuntimeError("MY ERROR")
if __name__ == '__main__':
my_long_running_functio("arg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment