|
| 1 | +Saga / Compensating Rollback |
| 2 | +============================ |
| 3 | + |
| 4 | +Some automations span several irreversible-looking steps — create a record, send |
| 5 | +an email, move a file. If a later step fails, the already-completed steps should |
| 6 | +be **undone**, but the executor's ``AC_try`` only does try/catch/finally for one |
| 7 | +block; nothing tracked "what to undo" across N completed steps. A ``Saga`` |
| 8 | +records a compensating action per step and, on any failure, runs the |
| 9 | +compensations for the completed steps in **LIFO** order. |
| 10 | + |
| 11 | +Forward actions and compensations are plain callables (or, via the executor, JSON |
| 12 | +action lists), so the orchestration is fully unit-testable with no real side |
| 13 | +effects. Compensation is best-effort: a failing compensation is logged and the |
| 14 | +rollback continues. Imports no ``PySide6``. |
| 15 | + |
| 16 | +Headless API |
| 17 | +------------ |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + from je_auto_control import Saga |
| 22 | +
|
| 23 | + result = (Saga() |
| 24 | + .step("create", create_record, delete_record) |
| 25 | + .step("notify", send_email, None) # nothing to undo |
| 26 | + .step("move", move_file, restore_file) |
| 27 | + .run()) |
| 28 | +
|
| 29 | + if not result.ok: |
| 30 | + result.failed_step # which step raised |
| 31 | + result.completed # steps that ran forward |
| 32 | + result.compensated # steps undone (LIFO over completed) |
| 33 | +
|
| 34 | +``run()`` returns a ``SagaResult`` (``ok`` / ``completed`` / ``compensated`` / |
| 35 | +``failed_step`` / ``error``). A step "fails" when its action raises; steps with no |
| 36 | +compensation are simply skipped during rollback. |
| 37 | + |
| 38 | +Executor command |
| 39 | +---------------- |
| 40 | + |
| 41 | +``AC_run_saga`` takes ``steps`` — a list (or JSON string) of ``{name, action: |
| 42 | +[...], compensation: [...]}`` where each ``action`` / ``compensation`` is an |
| 43 | +AutoControl action list. It returns ``{ok, completed, compensated, failed_step, |
| 44 | +error}``. The same operation is exposed as the MCP tool ``ac_run_saga`` and as a |
| 45 | +Script Builder command under **Flow**. |
0 commit comments