Stateful Saga Executor
Executes stateful saga steps with forward and compensating logic.
StatefulSagaExecutor orchestrates the execution of saga steps that share typed state, enabling "smart compensation" where compensation logic can make decisions based on the execution progress of later steps.
Execution Model
Execute steps sequentially in order with shared state access
If a step fails, compensate completed steps in reverse order (LIFO)
Compensation receives both step result and current saga state
Notify monitors of all lifecycle events
Return structured result with final state
State Isolation
Each execution creates an isolated copy of the initial state. Multiple concurrent executions do not share state. The final state is included in the result for observability and debugging.
Usage Example
data class PaymentState(val escrowCaptured: Boolean = false)
val saga = sagaExecutor<Cart, Order>(PaymentState()) {
first `do` "take-payment" with { cart ->
updateState { it.copy(amount = payment.amount) }
createOrder(cart)
} and undo {
if (state.escrowCaptured) refund() else returnEscrow()
}
}
when (val result = saga.execute(cart)) {
is StatefulSagaResult.Completed -> {
println("Order: ${result.value}, Final state: ${result.finalState}")
}
is StatefulSagaResult.Aborted -> {
println("Failed: ${result.error}, State at failure: ${result.finalState}")
}
}Parameters
The type of context passed to saga steps
The type of result produced by saga steps
The type of shared saga state
Functions
Add an interceptor to observe and potentially veto saga step execution.
Add a monitor to observe saga events.
Execute the saga with the given context.
Execute the saga with the given context, coroutine context, and optional timeout.
Execute the saga with an explicit runId for journal-aware runs.
Remove a previously registered interceptor.
Remove a monitor.
First half of the saga resume "id" with ctx DSL chain.
Resumes a stateful saga from its journal using the executor's pre-wired journal configuration.
Resumes a stateful saga from its journal.
Resumes a stateful saga from its journal when state reconstruction and Terminal decoding are required.