Skip to content

Commit

Permalink
log the missing handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Jan 9, 2025
1 parent bd61e21 commit b0a7a77
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
21 changes: 13 additions & 8 deletions ansibleplaybookgrapher/graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,25 +580,30 @@ def handlers(self) -> list["HandlerNode"]:
"""
return self.get_nodes("handlers")

def get_handlers(self, notify: list[str]) -> list["HandlerNode"]:
def get_notified_handlers(
self, notify: list[str]
) -> tuple[list["HandlerNode"], list[str]]:
"""Return the handlers notified by the given names if they exist
You must calculate the indices before calling this method.
:param notify: The name of the handler to get.
:return:
:param notify: The names of the handler to get from the play.
:return: A tuple of the notified handlers and the names of the handlers that were not found.
"""
# TODO: add a warning when a notified handler is not found
result = []
notified_handlers: list[HandlerNode] = []
found: set[str] = set()

for h in reversed(self.handlers):
if h in result:
if h in notified_handlers:
continue

for n in notify:
if h.matches_handler(n):
result.append(h)
notified_handlers.append(h)
found.add(n)

return sorted(result, key=lambda x: x.index)
not_found = set(notify) - found
return sorted(notified_handlers, key=lambda x: x.index), list(not_found)

def to_dict(
self,
Expand Down
16 changes: 16 additions & 0 deletions ansibleplaybookgrapher/renderer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,19 @@ def get_node_url(self, node: Node) -> str | None:
return url

return None


def log_handlers_not_found(
play_node: PlayNode, task_node: TaskNode, handlers_not_found: list[str]
) -> None:
"""Log the handlers that have not been found.
:param play_node: The play node
:param task_node: The task node
:param handlers_not_found: The handlers that have not been found.
:return:
"""
for handler in handlers_not_found:
display.warning(
f"The handler '{handler}' notified by the task '{task_node.display_name()}' has not been found in the play '{play_node.display_name()}'."
)
11 changes: 9 additions & 2 deletions ansibleplaybookgrapher/renderer/graphviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
RoleNode,
TaskNode,
)
from ansibleplaybookgrapher.renderer import PlaybookBuilder, Renderer
from ansibleplaybookgrapher.renderer import (
PlaybookBuilder,
Renderer,
log_handlers_not_found,
)
from ansibleplaybookgrapher.renderer.graphviz.postprocessor import GraphvizPostProcessor

display = Display()
Expand Down Expand Up @@ -206,7 +210,10 @@ def build_task(

# Build the edge from the task to the handlers it notifies
if self.show_handlers:
notified_handlers = play_node.get_handlers(task_node.notify)
notified_handlers, not_found = play_node.get_notified_handlers(
task_node.notify
)
log_handlers_not_found(play_node, task_node, not_found)

for counter, handler in enumerate(notified_handlers, 1):
digraph.edge(
Expand Down
11 changes: 9 additions & 2 deletions ansibleplaybookgrapher/renderer/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
RoleNode,
TaskNode,
)
from ansibleplaybookgrapher.renderer import PlaybookBuilder, Renderer
from ansibleplaybookgrapher.renderer import (
PlaybookBuilder,
Renderer,
log_handlers_not_found,
)

display = Display()

Expand Down Expand Up @@ -288,7 +292,10 @@ def build_task(
)

if self.show_handlers:
notified_handlers = play_node.get_handlers(task_node.notify)
notified_handlers, not_found = play_node.get_notified_handlers(
task_node.notify
)
log_handlers_not_found(play_node, task_node, not_found)

for counter, handler in enumerate(notified_handlers, 1):
self.add_link(
Expand Down
13 changes: 8 additions & 5 deletions tests/test_graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,16 @@ def test_get_handlers_from_play():

play.calculate_indices()

assert play.get_handlers(["fake handler"]) == []
assert play.get_handlers(["restart nginx"]) == [restart_nginx]
assert play.get_handlers(["restart postgres"]) == [restart_postgres]
assert play.get_notified_handlers(["fake handler"]) == ([], ["fake handler"])
assert play.get_notified_handlers(["restart nginx"]) == ([restart_nginx], [])
assert play.get_notified_handlers(["restart postgres"]) == ([restart_postgres], [])

mysql_handlers = play.get_handlers(["restart mysql"])
mysql_handlers, _ = play.get_notified_handlers(["restart mysql"])
assert len(mysql_handlers) == 1
assert mysql_handlers[0].id == restart_mysql_2.id

# When multiple handlers have the same listen, we return them in the order they are defined
assert play.get_handlers(["restart dbs"]) == [restart_postgres, restart_mysql_2]
assert play.get_notified_handlers(["restart dbs"]) == (
[restart_postgres, restart_mysql_2],
[],
)

0 comments on commit b0a7a77

Please sign in to comment.