SagaEvent

sealed interface SagaEvent

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

  1. SagaStarted - Saga execution begins

  2. For each step:

    • StepStarted - Step execution begins

    • StepCompleted, StepFailed, or StepSkipped - Step outcome

  3. If a step fails:

    • CompensationStarted - Compensation begins for a completed step

    • CompensationCompleted or CompensationFailed - Compensation result

  4. If a step is skipped:

    • Remaining steps are skipped, no compensation triggered

  5. 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

Link copied to clipboard
data class CompensationCompleted(val step: TypedValue, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when compensation completes successfully.

Link copied to clipboard
data class CompensationFailed(val step: TypedValue, val error: Throwable, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when compensation fails.

Link copied to clipboard
data class CompensationStarted(val step: TypedValue, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when compensation starts for a completed step.

Link copied to clipboard
data class SagaAborted(val error: SagaExecutionError, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when the saga is aborted due to a failure.

Link copied to clipboard
data class SagaCompleted<out R>(val result: R, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when the saga completes successfully.

Link copied to clipboard
data class SagaStarted(val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when saga execution starts.

Link copied to clipboard
data class StepCompleted<out R>(val step: TypedValue, val result: R, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when a saga step completes successfully.

Link copied to clipboard
data class StepFailed(val step: TypedValue, val error: Throwable, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when a saga step fails.

Link copied to clipboard
data class StepSkipped(val step: TypedValue, val skipReason: TypedValue, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when a saga step is skipped.

Link copied to clipboard
data class StepStarted(val step: TypedValue, val timestamp: Long = System.currentTimeMillis()) : SagaEvent

Emitted when a saga step starts execution.

Properties

Link copied to clipboard
abstract val timestamp: Long

Timestamp when the event occurred (milliseconds since epoch).