| | | 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 | | public TimeSpan BaseRetryDelay { get; set; } = TimeSpan.FromSeconds(5); |
| | | 32 | | |
| | | 33 | | public TimeSpan MaxRetryDelay { get; set; } = TimeSpan.FromMinutes(5); |
| | | 34 | | |
| | | 35 | | public TimeSpan DeferredRetryDelay { get; set; } = TimeSpan.FromMinutes(1); |
| | | 36 | | |
| | | 37 | | public int MaxRetryAttempts { get; set; } = 5; |
| | | 38 | | |
| | | 39 | | public int MaxErrorDetailLength { get; set; } = 512; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Stages minimized audit-completion receipts in the application database. |
| | | 44 | | /// </summary> |
| | | 45 | | public interface IApplicationAuditCompletionOutbox |
| | | 46 | | { |
| | | 47 | | ApplicationAuditCompletionOutboxEntry? Stage( |
| | | 48 | | ApplicationDbContext dbContext, |
| | | 49 | | ApplicationMutationAuditReceipt receipt, |
| | | 50 | | string? destination = null); |
| | | 51 | | |
| | | 52 | | ValueTask<ApplicationAuditCompletionOutboxEntry?> StageAsync( |
| | | 53 | | ApplicationDbContext dbContext, |
| | | 54 | | ApplicationMutationAuditReceipt receipt, |
| | | 55 | | string? destination = null, |
| | | 56 | | CancellationToken cancellationToken = default); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Publishes one minimized audit-completion message to a host-owned destination. |
| | | 61 | | /// </summary> |
| | | 62 | | public interface IApplicationAuditCompletionPublisher |
| | | 63 | | { |
| | | 64 | | string Destination { get; } |
| | | 65 | | |
| | | 66 | | ValueTask<ApplicationAuditCompletionPublishResult> PublishAsync( |
| | | 67 | | ApplicationAuditCompletionMessage message, |
| | | 68 | | CancellationToken cancellationToken = default); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Dispatches ready durable audit-completion entries after their business transaction commits. |
| | | 73 | | /// </summary> |
| | | 74 | | public interface IApplicationAuditCompletionOutboxDispatcher |
| | | 75 | | { |
| | | 76 | | Task<ApplicationAuditCompletionDispatchSummary> DispatchReadyAsync( |
| | | 77 | | CancellationToken cancellationToken = default); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Exposes minimized operational status and query projections for the durable outbox. |
| | | 82 | | /// </summary> |
| | | 83 | | public interface IApplicationAuditCompletionOutboxQuery |
| | | 84 | | { |
| | | 85 | | Task<ApplicationAuditCompletionOutboxHealth> GetHealthAsync( |
| | | 86 | | CancellationToken cancellationToken = default); |
| | | 87 | | |
| | | 88 | | Task<IReadOnlyList<ApplicationAuditCompletionOutboxItem>> QueryAsync( |
| | | 89 | | ApplicationAuditCompletionOutboxQueryRequest request, |
| | | 90 | | CancellationToken cancellationToken = default); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public enum ApplicationAuditCompletionPublishDisposition |
| | | 94 | | { |
| | | 95 | | Delivered, |
| | | 96 | | RetryableFailure, |
| | | 97 | | Failed, |
| | | 98 | | Deferred |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public sealed record ApplicationAuditCompletionPublishResult( |
| | | 102 | | ApplicationAuditCompletionPublishDisposition Disposition, |
| | | 103 | | string? ErrorCode = null, |
| | | 104 | | string? ErrorMessage = null, |
| | | 105 | | TimeSpan? RetryAfter = null) |
| | | 106 | | { |
| | | 107 | | public static ApplicationAuditCompletionPublishResult Success() |
| | | 108 | | { |
| | | 109 | | return new(ApplicationAuditCompletionPublishDisposition.Delivered); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public static ApplicationAuditCompletionPublishResult Retry( |
| | | 113 | | string? errorCode = null, |
| | | 114 | | string? errorMessage = null, |
| | | 115 | | TimeSpan? retryAfter = null) |
| | | 116 | | { |
| | | 117 | | return new( |
| | | 118 | | ApplicationAuditCompletionPublishDisposition.RetryableFailure, |
| | | 119 | | errorCode, |
| | | 120 | | errorMessage, |
| | | 121 | | retryAfter); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public static ApplicationAuditCompletionPublishResult TerminalFailure( |
| | | 125 | | string? errorCode = null, |
| | | 126 | | string? errorMessage = null) |
| | | 127 | | { |
| | | 128 | | return new(ApplicationAuditCompletionPublishDisposition.Failed, errorCode, errorMessage); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public static ApplicationAuditCompletionPublishResult Defer( |
| | | 132 | | string? errorCode = null, |
| | | 133 | | string? errorMessage = null, |
| | | 134 | | TimeSpan? retryAfter = null) |
| | | 135 | | { |
| | | 136 | | return new( |
| | | 137 | | ApplicationAuditCompletionPublishDisposition.Deferred, |
| | | 138 | | errorCode, |
| | | 139 | | errorMessage, |
| | | 140 | | retryAfter); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | public sealed record ApplicationAuditCompletionMessage( |
| | | 145 | | string SchemaVersion, |
| | | 146 | | string Destination, |
| | | 147 | | string IdempotencyKey, |
| | | 148 | | string MutationBatchId, |
| | | 149 | | int AuditRecordCount, |
| | | 150 | | string PersistenceOutcome, |
| | | 151 | | DateTime ReceiptCompletedUtc, |
| | | 152 | | string MutationManifestHash, |
| | | 153 | | string MutationManifestAlgorithm, |
| | | 154 | | string MutationManifestSchemaVersion, |
| | | 155 | | string? OperationExecutionId, |
| | | 156 | | string? ExecutionAttemptId, |
| | | 157 | | string? DecisionAuditRecordId, |
| | | 158 | | string? CorrelationId, |
| | | 159 | | string? TraceId); |
| | | 160 | | |
| | | 161 | | public sealed record ApplicationAuditCompletionDispatchSummary( |
| | | 162 | | bool Enabled, |
| | | 163 | | int AttemptedCount, |
| | | 164 | | int DeliveredCount, |
| | | 165 | | int RetryScheduledCount, |
| | | 166 | | int FailedCount, |
| | | 167 | | int DeferredCount, |
| | | 168 | | int DeadLetteredCount); |
| | | 169 | | |
| | | 170 | | public sealed record ApplicationAuditCompletionOutboxHealth( |
| | | 171 | | bool Enabled, |
| | | 172 | | long BacklogCount, |
| | | 173 | | TimeSpan? OldestPendingAge, |
| | | 174 | | long TotalRetryCount, |
| | | 175 | | long DeadLetterCount); |
| | | 176 | | |
| | | 177 | | public sealed record ApplicationAuditCompletionOutboxQueryRequest( |
| | | 178 | | string? Status = null, |
| | | 179 | | string? Destination = null, |
| | | 180 | | string? MutationBatchId = null, |
| | | 181 | | int MaximumResults = 100); |
| | | 182 | | |
| | 0 | 183 | | public sealed record ApplicationAuditCompletionOutboxItem( |
| | 0 | 184 | | Guid Id, |
| | 0 | 185 | | string SchemaVersion, |
| | 0 | 186 | | string Destination, |
| | 0 | 187 | | string IdempotencyKey, |
| | 0 | 188 | | string MutationBatchId, |
| | 0 | 189 | | int AuditRecordCount, |
| | 0 | 190 | | string PersistenceOutcome, |
| | 0 | 191 | | DateTime ReceiptCompletedUtc, |
| | 0 | 192 | | string MutationManifestHash, |
| | 0 | 193 | | string MutationManifestAlgorithm, |
| | 0 | 194 | | string MutationManifestSchemaVersion, |
| | 0 | 195 | | string? OperationExecutionId, |
| | 0 | 196 | | string? ExecutionAttemptId, |
| | 0 | 197 | | string? DecisionAuditRecordId, |
| | 0 | 198 | | string? CorrelationId, |
| | 0 | 199 | | string? TraceId, |
| | 0 | 200 | | string Status, |
| | 0 | 201 | | int RetryCount, |
| | 0 | 202 | | DateTime CreatedUtc, |
| | 0 | 203 | | DateTime? LastAttemptUtc, |
| | 0 | 204 | | DateTime? NextAttemptUtc, |
| | 0 | 205 | | DateTime? DeliveredUtc, |
| | 0 | 206 | | string? LastErrorCode, |
| | 0 | 207 | | string? LastErrorMessage); |