Saga Executor
Executes saga steps with forward and compensating logic.
SagaExecutor orchestrates the execution of saga steps, handling forward execution, compensation on failure, and event notification. The executor is coroutine-friendly and uses suspend functions for async operations.
Execution Model
Execute steps sequentially in order (asynchronously using coroutines)
If a step fails, compensate completed steps in reverse order (LIFO)
Notify monitors of all lifecycle events
Return structured result (Completed/Aborted/CompensationFailure)
Coroutine Safety
SagaExecutor uses suspend functions for execution, allowing for non-blocking async operations. Each execution is isolated and maintains its own completion stack. Multiple executions can run concurrently in different coroutines.
Usage Example
val saga = sagaExecutor<OrderContext, OrderResult> {
step("reserve") { context -> /* suspend operation */}
compensate { result -> /* suspend compensation */}
step("charge") { context -> /* suspend operation */}
compensate { result -> /* suspend compensation */}
}
// Execute within a coroutine
val result = saga.execute(OrderContext(...))Parameters
The type of context passed to saga steps
The type of result produced by saga steps
Inheritors
Functions
Add a monitor to observe saga events.
Extension function to convert a SagaExecutor to a FlowBasedSagaExecutor.
Execute the saga with the given context.
Execute the saga with the given context, coroutine context, and optional timeout.
Execute saga and return a Flow that emits all events followed by a terminal result event.
Remove a monitor.