Compensation Scope
Scope for compensation execution, providing access to both step result and saga state.
This scope is the receiver for compensation lambdas, enabling "smart compensation" where the compensation logic can make decisions based on:
result: The result produced by this specific step's forward action
state: The current saga state, including updates from later steps
Example: Smart Compensation
// Step 1: Take payment
first `do` "take-payment" with { cart ->
updateState { it.copy(paymentAmount = payment.amount) }
createTransaction(cart)
} and undo {
// Smart compensation: behavior depends on later steps
when {
state.escrowCaptured -> {
// Step 3 captured escrow - need to dispense physical refund
cashManager.dispenseCashRefund(state.paymentAmount)
}
state.paymentAmount > 0 -> {
// Escrow exists but wasn't captured - just return it
cashManager.returnEscrow()
}
else -> {
// No payment received
cashManager.disableAcceptors()
}
}
}
// Step 3: Capture escrow (sets escrowCaptured = true)
then `do` "capture-payment" with { cart ->
cashManager.captureEscrow()
updateState { it.copy(escrowCaptured = true) }
result
}Content copied to clipboard
Parameters
R
The type of result produced by this step
S
The type of shared saga state