Path: redb-route/redb.Route/Processors/SagaProcessor.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/SagaProcessor.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Definitions; using redb.Route.Telemetry; namespace redb.Route.Processors; /// /// Executes saga steps sequentially. On failure, runs compensations in reverse order /// for all completed steps. Compensation failures are logged but do not interrupt rollback. /// After all steps succeed, invokes the optional completion callback. /// internal sealed class SagaProcessor : IProcessor { private readonly SagaStepEntry[] _steps; private readonly Func? _onCompletion; private readonly ILogger? _logger; public SagaProcessor( SagaStepEntry[] steps, Func? onCompletion = null, ILogger? logger = null) { _steps = steps; _onCompletion = onCompletion; _logger = logger; } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { var completedCount = 0; var compensationFailed = false; try { for (var i = 0; i < _steps.Length; i++) { ct.ThrowIfCancellationRequested(); await _steps[i].Action(exchange, ct).ConfigureAwait(false); completedCount++; } } catch (Exception ex) when (ex is not OperationCanceledException) { _logger?.LogWarning(ex, "Saga step {StepIndex} failed, compensating {CompletedCount} step(s)", completedCount, completedCount); // Run compensations in reverse order for all completed steps for (var i = completedCount - 1; i >= 0; i--) { var compensate = _steps[i].Compensate; if (compensate is null) continue; try { await compensate(exchange, ct).ConfigureAwait(false); } catch (Exception compEx) { compensationFailed = true; _logger?.LogError(compEx, "Saga compensation for step {StepIndex} failed", i); } } if (compensationFailed) ProcessorMetrics.SagaFailed.Add(1); else ProcessorMetrics.SagaCompensated.Add(1); throw; } ProcessorMetrics.SagaCompleted.Add(1); // All steps succeeded — invoke completion callback if present if (_onCompletion is not null) await _onCompletion(exchange, ct).ConfigureAwait(false); } }