Saga Builder
DSL builder for creating standalone saga executors.
SagaBuilder provides a fluent API for defining saga steps, compensation logic, idempotency markers, and monitors. The builder is mutable during construction but produces an immutable SagaExecutor.
Usage Example
val saga = sagaExecutor<OrderContext, OrderResult> {
step("reserve-inventory") { context ->
inventoryService.reserve(context.items) // Can be a suspend call
}
compensate { reservationId ->
inventoryService.release(reservationId) // Can be a suspend call
}
step("charge-payment") { context ->
paymentService.charge(context.amount) // Can be a suspend call
}
compensate { transactionId ->
paymentService.refund(transactionId) // Can be a suspend call
}
idempotent(true)
monitor { event ->
logger.info("Saga event: $event")
}
}
// Execute within a coroutine
val result = saga.execute(OrderContext(...))Step Configuration
Steps are configured using a fluent chain:
step(name) { ... }- Define the step's forward actioncompensate { ... }- (Optional) Define compensation for the stepidempotent(true)- (Optional) Mark the step as idempotent
Each call to step() finalizes the previous step and starts a new one.
Parameters
The type of context passed to saga steps
The type of result produced by saga steps
Properties
Entry point for after.each(step) { … } on SagaBuilder.
Entry point for before.each(step) { … } on SagaBuilder.
Starts the first saga step with "first do 'name' with { ... }" syntax.
Entry point for the keep audit in journal conversational DSL (F006 AC1).
Entry point for tamper-evidence configuration.
Alternative: starts any saga step (not necessarily first).
Starts monitor configuration with "watching" keyword.
Functions
Marks the last step as idempotent using simple syntax.
Marks the last step as non-idempotent.
Build the saga executor.
Define compensation logic for the current step.
Adds compensation to the last defined step using simple syntax (suspend function).
Mark the current step as idempotent (safe to retry).
Register an interceptor to observe and potentially veto saga step execution.
Register an interceptor using a lambda.
Add a monitor to observe saga events.
Add a monitor using a lambda.
Adds a saga monitor using "monitoring with" syntax.
Adds a saga monitor instance.
Set retry policy for current step.
Define a saga step with forward logic and optional timeout.
Define a saga step with string name (convenience overload).