109 lines
3.1 KiB
C#
109 lines
3.1 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Svrnty.CQRS.Events.Abstractions.Sagas;
|
|
|
|
/// <summary>
|
|
/// Orchestrates saga execution with compensation logic.
|
|
/// </summary>
|
|
public interface ISagaOrchestrator
|
|
{
|
|
/// <summary>
|
|
/// Start a new saga instance.
|
|
/// </summary>
|
|
/// <typeparam name="TSaga">The saga type.</typeparam>
|
|
/// <param name="correlationId">Correlation ID for this saga.</param>
|
|
/// <param name="initialData">Optional initial saga data.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>The saga ID.</returns>
|
|
Task<string> StartSagaAsync<TSaga>(
|
|
string correlationId,
|
|
ISagaData? initialData = null,
|
|
CancellationToken cancellationToken = default)
|
|
where TSaga : ISaga;
|
|
|
|
/// <summary>
|
|
/// Resume a paused saga.
|
|
/// </summary>
|
|
/// <param name="sagaId">The saga ID to resume.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
Task ResumeSagaAsync(string sagaId, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Cancel a running saga (triggers compensation).
|
|
/// </summary>
|
|
/// <param name="sagaId">The saga ID to cancel.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
Task CancelSagaAsync(string sagaId, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Get saga status.
|
|
/// </summary>
|
|
/// <param name="sagaId">The saga ID.</param>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <returns>Saga status information.</returns>
|
|
Task<SagaStatus> GetStatusAsync(string sagaId, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Status information for a saga instance.
|
|
/// </summary>
|
|
public sealed record SagaStatus
|
|
{
|
|
/// <summary>
|
|
/// Saga instance ID.
|
|
/// </summary>
|
|
public required string SagaId { get; init; }
|
|
|
|
/// <summary>
|
|
/// Correlation ID.
|
|
/// </summary>
|
|
public required string CorrelationId { get; init; }
|
|
|
|
/// <summary>
|
|
/// Saga type name.
|
|
/// </summary>
|
|
public required string SagaName { get; init; }
|
|
|
|
/// <summary>
|
|
/// Current state.
|
|
/// </summary>
|
|
public SagaState State { get; init; }
|
|
|
|
/// <summary>
|
|
/// Current step index being executed.
|
|
/// </summary>
|
|
public int CurrentStep { get; init; }
|
|
|
|
/// <summary>
|
|
/// Total number of steps.
|
|
/// </summary>
|
|
public int TotalSteps { get; init; }
|
|
|
|
/// <summary>
|
|
/// When the saga started.
|
|
/// </summary>
|
|
public DateTimeOffset StartedAt { get; init; }
|
|
|
|
/// <summary>
|
|
/// When the saga last updated.
|
|
/// </summary>
|
|
public DateTimeOffset LastUpdated { get; init; }
|
|
|
|
/// <summary>
|
|
/// When the saga completed (if completed).
|
|
/// </summary>
|
|
public DateTimeOffset? CompletedAt { get; init; }
|
|
|
|
/// <summary>
|
|
/// Error message (if failed).
|
|
/// </summary>
|
|
public string? ErrorMessage { get; init; }
|
|
|
|
/// <summary>
|
|
/// Saga data.
|
|
/// </summary>
|
|
public System.Collections.Generic.IDictionary<string, object>? Data { get; init; }
|
|
}
|