Saga Event
Events emitted during saga execution for monitoring and logging.
These events provide a structured way to observe saga execution lifecycle, enabling monitoring, logging, and metrics collection. All step-related events use TypedValue to preserve enum type information when enum step names are used.
Event Lifecycle
SagaStarted - Saga execution begins
For each step:
StepStarted - Step execution begins
StepCompleted, StepFailed, or StepSkipped - Step outcome
If a step fails:
CompensationStarted - Compensation begins for a completed step
CompensationCompleted or CompensationFailed - Compensation result
If a step is skipped:
Remaining steps are skipped, no compensation triggered
SagaCompleted or SagaAborted - Saga finishes
Usage Example
enum class OrderSteps { VALIDATE, PAYMENT, SHIP }
enum class SkipReason { ALREADY_PROCESSED }
saga.addMonitor { event ->
when (event) {
is SagaEvent.StepStarted -> {
// Type-safe enum access
when (event.stepAs<OrderSteps>()) {
OrderSteps.VALIDATE -> log.info("Validating order...")
OrderSteps.PAYMENT -> log.info("Processing payment...")
OrderSteps.SHIP -> log.info("Shipping order...")
null -> log.info("Starting: ${event.stepName}")
}
}
is SagaEvent.StepSkipped -> {
if (event.reasonAs<SkipReason>() == SkipReason.ALREADY_PROCESSED) {
metrics.recordDuplicate()
}
}
is SagaEvent.StepFailed -> log.error("Step failed: ${event.stepName}", event.error)
else -> log.debug("Saga event: $event")
}
}Inheritors
Types
Emitted when compensation completes successfully.
Emitted when compensation fails.
Emitted when compensation starts for a completed step.
Emitted when the saga is aborted due to a failure.
Emitted when the saga completes successfully.
Emitted when saga execution starts.
Emitted when a saga step completes successfully.
Emitted when a saga step fails.
Emitted when a saga step is skipped.
Emitted when a saga step starts execution.