Skip to content

Commit

Permalink
Add BrigadeSubTaskError for grouped tasks (#131)
Browse files Browse the repository at this point in the history
* Add BrigadeSubTaskError for grouped tasks

Raises BrigadeSubTaskError when a subtask to a grouped task has failed,
this allows for a prettier error message when using print_output()

* Use objects instead of name of task

* Version 1.0.0
  • Loading branch information
ogenstad authored May 4, 2018
1 parent 0998485 commit 28a57e0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
21 changes: 21 additions & 0 deletions brigade/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ def __str__(self):
text += "**** {}\n".format(sub_r.name)
text += "{}\n".format(sub_r)
return text


class BrigadeSubTaskError(Exception):
"""
Raised by brigade when a sub task managed by :meth:`brigade.core.Task.run`
has failed
Arguments:
task (:obj:`brigade.core.task.Task`): The subtask that failed
result (:obj:`brigade.core.task.Result`): The result of the failed task
Attributes:
task (:obj:`brigade.core.task.Task`): The subtask that failed
result (:obj:`brigade.core.task.Result`): The result of the failed task
"""

def __init__(self, task, result):
self.task = task
self.result = result

def __str__(self):
return "Subtask: {} (failed)\n".format(self.task)
16 changes: 13 additions & 3 deletions brigade/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from builtins import super

from brigade.core.exceptions import BrigadeExecutionError
from brigade.core.exceptions import BrigadeSubTaskError


class Task(object):
Expand Down Expand Up @@ -61,10 +62,17 @@ def start(self, host, brigade):
r = self.task(self, **self.params)
if not isinstance(r, Result):
r = Result(host=host, result=r)

except BrigadeSubTaskError as e:
tb = traceback.format_exc()
logger.error("{}: {}".format(self.host, tb))
r = Result(host, exception=e, result=str(e), failed=True)

except Exception as e:
tb = traceback.format_exc()
logger.error("{}: {}".format(self.host, tb))
r = Result(host, exception=e, result=tb, failed=True)

r.name = self.name
r.severity_level = logging.ERROR if r.failed else self.severity_level

Expand Down Expand Up @@ -92,12 +100,14 @@ def grouped_tasks(task):

if "severity_level" not in kwargs:
kwargs["severity_level"] = self.severity_level
r = Task(task, **kwargs).start(self.host, self.brigade)
task = Task(task, **kwargs)
r = task.start(self.host, self.brigade)
self.results.append(r[0] if len(r) == 1 else r)

if r.failed:
# Without this we will keep running the grouped task
raise r.exception
raise BrigadeSubTaskError(task=task, result=r)

self.results.append(r[0] if len(r) == 1 else r)
return r

def is_dry_run(self, override=None):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
__author__ = "[email protected]"
__license__ = "Apache License, version 2"

__version__ = "0.0.7"
__version__ = "1.0.0"

setup(
name="brigade",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Exception('Unknown Error -> Contact your system administrator',)
BrigadeSubTaskError()
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : False --------------------------------------------- ERROR
Exception('Unknown Error -> Contact your system administrator',)
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Hello from CRITICAL
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : False **********************************************
vvvv read_data ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Exception('Unknown Error -> Contact your system administrator',)
BrigadeSubTaskError()
---- echo_task ** changed : False ---------------------------------------------- CRITICAL
Hello from CRITICAL
---- parse_data ** changed : False --------------------------------------------- ERROR
Exception('Unknown Error -> Contact your system administrator',)
^^^^ END read_data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 comments on commit 28a57e0

Please sign in to comment.