Skip to content

Instantly share code, notes, and snippets.

@OrangeTux
Created June 4, 2015 09:56
Show Gist options
  • Save OrangeTux/d24b0ca0cd33fe6a52c2 to your computer and use it in GitHub Desktop.
Save OrangeTux/d24b0ca0cd33fe6a52c2 to your computer and use it in GitHub Desktop.
Decorator which executes callback if wrapped Fabric task fails.
def on_fail(func):
""" Return decorator which executes callback if wrapped tasks failes.
Usage:
def log_failure():
print('Some failure.')
@task
@on_fail(log_failure):
def git_fetch():
abort('Failure!')
:param func: A callable that is executed on failure of wrapped task.
:return: A decorator.
"""
def decorator(t):
@wraps(t)
def wrapper(*args, **kwargs):
try:
# warn_only must be False, so Fabric raises a SystemExit when
# tasks failes.
with settings(warn_only=False):
return t(*args, **kwargs)
except SystemExit:
print('Task \'%s\' failed!' % t.func_name)
func()
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment