| | | 1 | | using ProjectTemplate.Infrastructure.Data.Entities; |
| | | 2 | | |
| | | 3 | | namespace ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Identifies durable audit-completion delivery states. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class ApplicationAuditCompletionOutboxStatuses |
| | | 9 | | { |
| | | 10 | | public const string Pending = "Pending"; |
| | | 11 | | public const string Delivered = "Delivered"; |
| | | 12 | | public const string RetryableFailure = "RetryableFailure"; |
| | | 13 | | public const string Failed = "Failed"; |
| | | 14 | | public const string Deferred = "Deferred"; |
| | | 15 | | public const string DeadLettered = "DeadLettered"; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Configures the optional durable audit-completion outbox and dispatcher. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class ApplicationAuditCompletionOutboxOptions |
| | | 22 | | { |
| | | 23 | | public bool Enabled { get; set; } = true; |
| | | 24 | | |
| | | 25 | | public string DefaultDestination { get; set; } = "default"; |
| | | 26 | | |
| | | 27 | | public int BatchSize { get; set; } = 25; |
| | | 28 | | |
| | | 29 | | public TimeSpan PollInterval { get; set; } = TimeSpan.FromSeconds(5); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the maximum delay between dispatch cycles after consecutive failures. The delay doubles for each |
| | | 33 | | /// additional consecutive failure, up to this value, and returns to <see cref="PollInterval"/> after a success. |
| | | 34 | | /// </summary> |
| | | 35 | | public TimeSpan MaximumCycleRetryDelay { get; set; } = TimeSpan.FromMinutes(5); |
| | | 36 | | |
| | | 37 | | public TimeSpan BaseRetryDelay { get; set; } = TimeSpan.FromSeconds(5); |
| | | 38 | | |
| | | 39 | | public TimeSpan MaxRetryDelay { get; set; } = TimeSpan.FromMinutes(5); |
| | | 40 | | |
| | | 41 | | public TimeSpan DeferredRetryDelay { get; set; } = TimeSpan.FromMinutes(1); |
| | | 42 | | |
| | | 43 | | public int MaxRetryAttempts { get; set; } = 5; |
| | | 44 | | |
| | | 45 | | public int MaxErrorDetailLength { get; set; } = 512; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Stages minimized audit-completion receipts in the application database. |
| | | 50 | | /// </summary> |
| | | 51 | | public interface IApplicationAuditCompletionOutbox |
| | | 52 | | { |
| | | 53 | | ApplicationAuditCompletionOutboxEntry? Stage( |
| | | 54 | | ApplicationDbContext dbContext, |
| | | 55 | | ApplicationMutationAuditReceipt receipt, |
| | | 56 | | string? destination = null); |
| | | 57 | | |
| | | 58 | | ValueTask<ApplicationAuditCompletionOutboxEntry?> StageAsync( |
| | | 59 | | ApplicationDbContext dbContext, |
| | | 60 | | ApplicationMutationAuditReceipt receipt, |
| | | 61 | | string? destination = null, |
| | | 62 | | CancellationToken cancellationToken = default); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Publishes one minimized audit-completion message to a host-owned destination. |
| | | 67 | | /// </summary> |
| | | 68 | | public interface IApplicationAuditCompletionPublisher |
| | | 69 | | { |
| | | 70 | | string Destination { get; } |
| | | 71 | | |
| | | 72 | | ValueTask<ApplicationAuditCompletionPublishResult> PublishAsync( |
| | | 73 | | ApplicationAuditCompletionMessage message, |
| | | 74 | | CancellationToken cancellationToken = default); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Dispatches ready durable audit-completion entries after their business transaction commits. |
| | | 79 | | /// </summary> |
| | | 80 | | public interface IApplicationAuditCompletionOutboxDispatcher |
| | | 81 | | { |
| | | 82 | | Task<ApplicationAuditCompletionDispatchSummary> DispatchReadyAsync( |
| | | 83 | | CancellationToken cancellationToken = default); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Exposes minimized operational status and query projections for the durable outbox. |
| | | 88 | | /// </summary> |
| | | 89 | | public interface IApplicationAuditCompletionOutboxQuery |
| | | 90 | | { |
| | | 91 | | Task<ApplicationAuditCompletionOutboxHealth> GetHealthAsync( |
| | | 92 | | CancellationToken cancellationToken = default); |
| | | 93 | | |
| | | 94 | | Task<IReadOnlyList<ApplicationAuditCompletionOutboxItem>> QueryAsync( |
| | | 95 | | ApplicationAuditCompletionOutboxQueryRequest request, |
| | | 96 | | CancellationToken cancellationToken = default); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public enum ApplicationAuditCompletionPublishDisposition |
| | | 100 | | { |
| | | 101 | | Delivered, |
| | | 102 | | RetryableFailure, |
| | | 103 | | Failed, |
| | | 104 | | Deferred |
| | | 105 | | } |
| | | 106 | | |
| | 10 | 107 | | public sealed record ApplicationAuditCompletionPublishResult( |
| | 10 | 108 | | ApplicationAuditCompletionPublishDisposition Disposition, |
| | 10 | 109 | | string? ErrorCode = null, |
| | 10 | 110 | | string? ErrorMessage = null, |
| | 10 | 111 | | TimeSpan? RetryAfter = null) |
| | | 112 | | { |
| | | 113 | | public static ApplicationAuditCompletionPublishResult Success() |
| | | 114 | | { |
| | 7 | 115 | | return new(ApplicationAuditCompletionPublishDisposition.Delivered); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public static ApplicationAuditCompletionPublishResult Retry( |
| | | 119 | | string? errorCode = null, |
| | | 120 | | string? errorMessage = null, |
| | | 121 | | TimeSpan? retryAfter = null) |
| | | 122 | | { |
| | 1 | 123 | | return new( |
| | 1 | 124 | | ApplicationAuditCompletionPublishDisposition.RetryableFailure, |
| | 1 | 125 | | errorCode, |
| | 1 | 126 | | errorMessage, |
| | 1 | 127 | | retryAfter); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static ApplicationAuditCompletionPublishResult TerminalFailure( |
| | | 131 | | string? errorCode = null, |
| | | 132 | | string? errorMessage = null) |
| | | 133 | | { |
| | 0 | 134 | | return new(ApplicationAuditCompletionPublishDisposition.Failed, errorCode, errorMessage); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public static ApplicationAuditCompletionPublishResult Defer( |
| | | 138 | | string? errorCode = null, |
| | | 139 | | string? errorMessage = null, |
| | | 140 | | TimeSpan? retryAfter = null) |
| | | 141 | | { |
| | 2 | 142 | | return new( |
| | 2 | 143 | | ApplicationAuditCompletionPublishDisposition.Deferred, |
| | 2 | 144 | | errorCode, |
| | 2 | 145 | | errorMessage, |
| | 2 | 146 | | retryAfter); |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | public sealed record ApplicationAuditCompletionMessage( |
| | | 151 | | string SchemaVersion, |
| | | 152 | | string Destination, |
| | | 153 | | string IdempotencyKey, |
| | | 154 | | string MutationBatchId, |
| | | 155 | | int AuditRecordCount, |
| | | 156 | | string PersistenceOutcome, |
| | | 157 | | DateTime ReceiptCompletedUtc, |
| | | 158 | | string MutationManifestHash, |
| | | 159 | | string MutationManifestAlgorithm, |
| | | 160 | | string MutationManifestSchemaVersion, |
| | | 161 | | string? OperationExecutionId, |
| | | 162 | | string? ExecutionAttemptId, |
| | | 163 | | string? DecisionAuditRecordId, |
| | | 164 | | string? CorrelationId, |
| | | 165 | | string? TraceId); |
| | | 166 | | |
| | | 167 | | public sealed record ApplicationAuditCompletionDispatchSummary( |
| | | 168 | | bool Enabled, |
| | | 169 | | int AttemptedCount, |
| | | 170 | | int DeliveredCount, |
| | | 171 | | int RetryScheduledCount, |
| | | 172 | | int FailedCount, |
| | | 173 | | int DeferredCount, |
| | | 174 | | int DeadLetteredCount); |
| | | 175 | | |
| | | 176 | | public sealed record ApplicationAuditCompletionOutboxHealth( |
| | | 177 | | bool Enabled, |
| | | 178 | | long BacklogCount, |
| | | 179 | | TimeSpan? OldestPendingAge, |
| | | 180 | | long TotalRetryCount, |
| | | 181 | | long DeadLetterCount); |
| | | 182 | | |
| | | 183 | | public sealed record ApplicationAuditCompletionOutboxQueryRequest( |
| | | 184 | | string? Status = null, |
| | | 185 | | string? Destination = null, |
| | | 186 | | string? MutationBatchId = null, |
| | | 187 | | int MaximumResults = 100); |
| | | 188 | | |
| | | 189 | | public sealed record ApplicationAuditCompletionOutboxItem( |
| | | 190 | | Guid Id, |
| | | 191 | | string SchemaVersion, |
| | | 192 | | string Destination, |
| | | 193 | | string IdempotencyKey, |
| | | 194 | | string MutationBatchId, |
| | | 195 | | int AuditRecordCount, |
| | | 196 | | string PersistenceOutcome, |
| | | 197 | | DateTime ReceiptCompletedUtc, |
| | | 198 | | string MutationManifestHash, |
| | | 199 | | string MutationManifestAlgorithm, |
| | | 200 | | string MutationManifestSchemaVersion, |
| | | 201 | | string? OperationExecutionId, |
| | | 202 | | string? ExecutionAttemptId, |
| | | 203 | | string? DecisionAuditRecordId, |
| | | 204 | | string? CorrelationId, |
| | | 205 | | string? TraceId, |
| | | 206 | | string Status, |
| | | 207 | | int RetryCount, |
| | | 208 | | DateTime CreatedUtc, |
| | | 209 | | DateTime? LastAttemptUtc, |
| | | 210 | | DateTime? NextAttemptUtc, |
| | | 211 | | DateTime? DeliveredUtc, |
| | | 212 | | string? LastErrorCode, |
| | | 213 | | string? LastErrorMessage); |