Complete worked example
python
from graphlib import TopologicalSorter, CycleError
def inspect_example(graph, failed_node):
sorter = TopologicalSorter(graph)
try:
sorter.prepare()
except CycleError as error:
raise ValueError('cycle in the pipeline') from error
states = {}
while sorter.is_active():
for node in sorted(sorter.get_ready()):
if any(states[dependency] != 'success' for dependency in graph.get(node, [])):
states[node] = 'blocked'
elif node == failed_node:
states[node] = 'failed'
else:
states[node] = 'success'
sorter.done(node)
return {'states': {key: states[key] for key in sorted(states)}}- Reasoning
- The graph maps each node to its predecessors. prepare checks for cycles, and each get_ready group is processed in sorted order for deterministic results. A node with any nonsuccessful dependency is blocked; the designated fault node fails; otherwise the node succeeds. Every terminally processed node is then marked done.