| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Emissions; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Core.Outbox; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a provider-neutral durable outbox entry for a governance emission envelope. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Outbox entries are intended to be persisted before optional downstream provider delivery is attempted. |
| | | 11 | | /// </remarks> |
| | | 12 | | public sealed class GovernanceOutboxEntry |
| | | 13 | | { |
| | | 14 | | private const int DefaultMaxRetryCount = 5; |
| | | 15 | | |
| | 5 | 16 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 5 | 17 | | new ReadOnlyDictionary<string, string>( |
| | 5 | 18 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 19 | | |
| | 274 | 20 | | private GovernanceOutboxEntry( |
| | 274 | 21 | | string outboxEntryId, |
| | 274 | 22 | | GovernanceEmissionEnvelope envelope, |
| | 274 | 23 | | GovernanceEmissionStatus status, |
| | 274 | 24 | | DateTimeOffset createdUtc, |
| | 274 | 25 | | DateTimeOffset updatedUtc, |
| | 274 | 26 | | int retryCount, |
| | 274 | 27 | | int maxRetryCount, |
| | 274 | 28 | | DateTimeOffset? nextRetryUtc, |
| | 274 | 29 | | GovernanceEmissionError? lastError, |
| | 274 | 30 | | string? providerName, |
| | 274 | 31 | | string? providerRecordId, |
| | 274 | 32 | | string? deadLetterReason, |
| | 274 | 33 | | IReadOnlyDictionary<string, string> metadata) |
| | | 34 | | { |
| | 274 | 35 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 273 | 36 | | ArgumentNullException.ThrowIfNull(envelope); |
| | | 37 | | |
| | 273 | 38 | | if (!Enum.IsDefined(status)) |
| | | 39 | | { |
| | 1 | 40 | | throw new ArgumentOutOfRangeException(nameof(status), status, "Outbox status must be defined."); |
| | | 41 | | } |
| | | 42 | | |
| | 272 | 43 | | if (retryCount < 0) |
| | | 44 | | { |
| | 1 | 45 | | throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "Retry count must be greater than or e |
| | | 46 | | } |
| | | 47 | | |
| | 271 | 48 | | if (maxRetryCount < 0) |
| | | 49 | | { |
| | 2 | 50 | | throw new ArgumentOutOfRangeException(nameof(maxRetryCount), maxRetryCount, "Maximum retry count must be gre |
| | | 51 | | } |
| | | 52 | | |
| | 269 | 53 | | OutboxEntryId = outboxEntryId.Trim(); |
| | 269 | 54 | | Envelope = envelope; |
| | 269 | 55 | | Status = status; |
| | 269 | 56 | | CreatedUtc = createdUtc.ToUniversalTime(); |
| | 269 | 57 | | UpdatedUtc = updatedUtc.ToUniversalTime(); |
| | 269 | 58 | | RetryCount = retryCount; |
| | 269 | 59 | | MaxRetryCount = maxRetryCount; |
| | 269 | 60 | | NextRetryUtc = nextRetryUtc?.ToUniversalTime(); |
| | 269 | 61 | | LastError = lastError; |
| | 269 | 62 | | ProviderName = NormalizeOptional(providerName); |
| | 269 | 63 | | ProviderRecordId = NormalizeOptional(providerRecordId); |
| | 269 | 64 | | DeadLetterReason = NormalizeOptional(deadLetterReason); |
| | 269 | 65 | | Metadata = metadata; |
| | 269 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the stable outbox entry identifier. |
| | | 70 | | /// </summary> |
| | 423 | 71 | | public string OutboxEntryId { get; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the provider-neutral governance emission envelope being persisted for delivery. |
| | | 75 | | /// </summary> |
| | 275 | 76 | | public GovernanceEmissionEnvelope Envelope { get; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the current provider-neutral outbox status. |
| | | 80 | | /// </summary> |
| | 413 | 81 | | public GovernanceEmissionStatus Status { get; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the UTC timestamp when the outbox entry was created. |
| | | 85 | | /// </summary> |
| | 204 | 86 | | public DateTimeOffset CreatedUtc { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the UTC timestamp when the outbox entry was last updated. |
| | | 90 | | /// </summary> |
| | 94 | 91 | | public DateTimeOffset UpdatedUtc { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the number of failed or deferred delivery attempts recorded for this entry. |
| | | 95 | | /// </summary> |
| | 200 | 96 | | public int RetryCount { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the maximum retry count before the entry should transition to dead-lettered. |
| | | 100 | | /// </summary> |
| | 209 | 101 | | public int MaxRetryCount { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the next UTC retry timestamp, when retry scheduling is active. |
| | | 105 | | /// </summary> |
| | 175 | 106 | | public DateTimeOffset? NextRetryUtc { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the last provider-neutral emission error, when available. |
| | | 110 | | /// </summary> |
| | 111 | 111 | | public GovernanceEmissionError? LastError { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the provider name associated with the most recent attempt, when available. |
| | | 115 | | /// </summary> |
| | 97 | 116 | | public string? ProviderName { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the provider-side record identifier, when delivery returned one and it is safe to store. |
| | | 120 | | /// </summary> |
| | 91 | 121 | | public string? ProviderRecordId { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the dead-letter reason, when the entry has reached a terminal dead-letter state. |
| | | 125 | | /// </summary> |
| | 93 | 126 | | public string? DeadLetterReason { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets minimized provider-neutral outbox metadata. |
| | | 130 | | /// </summary> |
| | 160 | 131 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets a value indicating whether this entry was delivered successfully. |
| | | 135 | | /// </summary> |
| | 64 | 136 | | public bool IsDelivered => Status is GovernanceEmissionStatus.Delivered; |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets a value indicating whether this entry has reached a terminal dead-letter state. |
| | | 140 | | /// </summary> |
| | 57 | 141 | | public bool IsDeadLettered => Status is GovernanceEmissionStatus.DeadLettered; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets a value indicating whether metadata is present. |
| | | 145 | | /// </summary> |
| | 5 | 146 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Creates a pending durable governance outbox entry. |
| | | 150 | | /// </summary> |
| | | 151 | | public static GovernanceOutboxEntry Create( |
| | | 152 | | GovernanceEmissionEnvelope envelope, |
| | | 153 | | string? outboxEntryId = null, |
| | | 154 | | DateTimeOffset? createdUtc = null, |
| | | 155 | | int maxRetryCount = DefaultMaxRetryCount, |
| | | 156 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 157 | | { |
| | 101 | 158 | | DateTimeOffset timestamp = createdUtc ?? DateTimeOffset.UtcNow; |
| | | 159 | | |
| | 101 | 160 | | return new GovernanceOutboxEntry( |
| | 101 | 161 | | NormalizeIdentifier(outboxEntryId), |
| | 101 | 162 | | envelope, |
| | 101 | 163 | | GovernanceEmissionStatus.Pending, |
| | 101 | 164 | | timestamp, |
| | 101 | 165 | | timestamp, |
| | 101 | 166 | | retryCount: 0, |
| | 101 | 167 | | maxRetryCount, |
| | 101 | 168 | | nextRetryUtc: null, |
| | 101 | 169 | | lastError: null, |
| | 101 | 170 | | providerName: null, |
| | 101 | 171 | | providerRecordId: null, |
| | 101 | 172 | | deadLetterReason: null, |
| | 101 | 173 | | NormalizeMetadata(metadata)); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Restores a durable outbox entry from provider-neutral storage. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <remarks> |
| | | 180 | | /// This factory exists for storage adapters. It does not perform provider emission and does not add any provider de |
| | | 181 | | /// </remarks> |
| | | 182 | | public static GovernanceOutboxEntry Restore( |
| | | 183 | | GovernanceEmissionEnvelope envelope, |
| | | 184 | | GovernanceEmissionStatus status, |
| | | 185 | | string outboxEntryId, |
| | | 186 | | DateTimeOffset createdUtc, |
| | | 187 | | DateTimeOffset updatedUtc, |
| | | 188 | | int retryCount = 0, |
| | | 189 | | int maxRetryCount = DefaultMaxRetryCount, |
| | | 190 | | DateTimeOffset? nextRetryUtc = null, |
| | | 191 | | GovernanceEmissionError? lastError = null, |
| | | 192 | | string? providerName = null, |
| | | 193 | | string? providerRecordId = null, |
| | | 194 | | string? deadLetterReason = null, |
| | | 195 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 196 | | { |
| | 112 | 197 | | return new GovernanceOutboxEntry( |
| | 112 | 198 | | outboxEntryId, |
| | 112 | 199 | | envelope, |
| | 112 | 200 | | status, |
| | 112 | 201 | | createdUtc, |
| | 112 | 202 | | updatedUtc, |
| | 112 | 203 | | retryCount, |
| | 112 | 204 | | maxRetryCount, |
| | 112 | 205 | | nextRetryUtc, |
| | 112 | 206 | | lastError, |
| | 112 | 207 | | providerName, |
| | 112 | 208 | | providerRecordId, |
| | 112 | 209 | | deadLetterReason, |
| | 112 | 210 | | NormalizeMetadata(metadata)); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Determines whether the entry is ready for retry at the supplied UTC timestamp. |
| | | 215 | | /// </summary> |
| | | 216 | | public bool IsRetryReady(DateTimeOffset utcNow) |
| | | 217 | | { |
| | 42 | 218 | | return !IsDelivered && !IsDeadLettered && RetryCount < MaxRetryCount && Status is GovernanceEmissionStatus.Defer |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Returns a delivered copy of this entry. |
| | | 223 | | /// </summary> |
| | | 224 | | public GovernanceOutboxEntry MarkDelivered( |
| | | 225 | | GovernanceEmissionResult result, |
| | | 226 | | DateTimeOffset? updatedUtc = null) |
| | | 227 | | { |
| | 20 | 228 | | ArgumentNullException.ThrowIfNull(result); |
| | | 229 | | |
| | 20 | 230 | | return !result.IsSuccess |
| | 20 | 231 | | ? throw new ArgumentException("Delivered outbox transitions require a successful emission result.", nameof(r |
| | 20 | 232 | | : Copy( |
| | 20 | 233 | | GovernanceEmissionStatus.Delivered, |
| | 20 | 234 | | updatedUtc, |
| | 20 | 235 | | retryCount: RetryCount, |
| | 20 | 236 | | nextRetryUtc: null, |
| | 20 | 237 | | lastError: null, |
| | 20 | 238 | | providerName: result.ProviderName, |
| | 20 | 239 | | providerRecordId: result.ProviderRecordId, |
| | 20 | 240 | | deadLetterReason: null, |
| | 20 | 241 | | metadata: MergeMetadata(Metadata, result.Metadata)); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Returns a failed or retryable-failure copy of this entry. |
| | | 246 | | /// </summary> |
| | | 247 | | public GovernanceOutboxEntry MarkFailed( |
| | | 248 | | GovernanceEmissionError governanceEmissionError, |
| | | 249 | | DateTimeOffset? nextRetryUtc = null, |
| | | 250 | | DateTimeOffset? updatedUtc = null) |
| | | 251 | | { |
| | 27 | 252 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | | 253 | | |
| | 27 | 254 | | int nextRetryCount = RetryCount + 1; |
| | 27 | 255 | | GovernanceEmissionStatus nextStatus = nextRetryCount >= MaxRetryCount |
| | 27 | 256 | | ? GovernanceEmissionStatus.DeadLettered |
| | 27 | 257 | | : governanceEmissionError.IsRetryable |
| | 27 | 258 | | ? GovernanceEmissionStatus.RetryableFailure |
| | 27 | 259 | | : GovernanceEmissionStatus.Failed; |
| | | 260 | | |
| | 27 | 261 | | return Copy( |
| | 27 | 262 | | nextStatus, |
| | 27 | 263 | | updatedUtc, |
| | 27 | 264 | | nextRetryCount, |
| | 27 | 265 | | nextStatus is GovernanceEmissionStatus.DeadLettered ? null : nextRetryUtc, |
| | 27 | 266 | | governanceEmissionError, |
| | 27 | 267 | | governanceEmissionError.ProviderName, |
| | 27 | 268 | | providerRecordId: null, |
| | 27 | 269 | | nextStatus is GovernanceEmissionStatus.DeadLettered ? governanceEmissionError.Message : null, |
| | 27 | 270 | | Metadata); |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Returns a deferred copy of this entry. |
| | | 275 | | /// </summary> |
| | | 276 | | public GovernanceOutboxEntry MarkDeferred( |
| | | 277 | | GovernanceEmissionError? governanceEmissionError = null, |
| | | 278 | | DateTimeOffset? nextRetryUtc = null, |
| | | 279 | | DateTimeOffset? updatedUtc = null) |
| | | 280 | | { |
| | 9 | 281 | | return Copy( |
| | 9 | 282 | | GovernanceEmissionStatus.Deferred, |
| | 9 | 283 | | updatedUtc, |
| | 9 | 284 | | retryCount: RetryCount, |
| | 9 | 285 | | nextRetryUtc, |
| | 9 | 286 | | governanceEmissionError, |
| | 9 | 287 | | governanceEmissionError?.ProviderName, |
| | 9 | 288 | | providerRecordId: null, |
| | 9 | 289 | | deadLetterReason: null, |
| | 9 | 290 | | metadata: Metadata); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Returns a dead-lettered copy of this entry. |
| | | 295 | | /// </summary> |
| | | 296 | | public GovernanceOutboxEntry MarkDeadLettered( |
| | | 297 | | GovernanceEmissionError governanceEmissionError, |
| | | 298 | | string? deadLetterReason = null, |
| | | 299 | | DateTimeOffset? updatedUtc = null) |
| | | 300 | | { |
| | 6 | 301 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | | 302 | | |
| | 6 | 303 | | return Copy( |
| | 6 | 304 | | GovernanceEmissionStatus.DeadLettered, |
| | 6 | 305 | | updatedUtc, |
| | 6 | 306 | | retryCount: RetryCount, |
| | 6 | 307 | | nextRetryUtc: null, |
| | 6 | 308 | | lastError: governanceEmissionError, |
| | 6 | 309 | | providerName: governanceEmissionError.ProviderName, |
| | 6 | 310 | | providerRecordId: null, |
| | 6 | 311 | | deadLetterReason: deadLetterReason ?? governanceEmissionError.Message, |
| | 6 | 312 | | metadata: Metadata); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | private GovernanceOutboxEntry Copy( |
| | | 316 | | GovernanceEmissionStatus status, |
| | | 317 | | DateTimeOffset? updatedUtc, |
| | | 318 | | int retryCount, |
| | | 319 | | DateTimeOffset? nextRetryUtc, |
| | | 320 | | GovernanceEmissionError? lastError, |
| | | 321 | | string? providerName, |
| | | 322 | | string? providerRecordId, |
| | | 323 | | string? deadLetterReason, |
| | | 324 | | IReadOnlyDictionary<string, string> metadata) |
| | | 325 | | { |
| | 61 | 326 | | return new GovernanceOutboxEntry( |
| | 61 | 327 | | OutboxEntryId, |
| | 61 | 328 | | Envelope, |
| | 61 | 329 | | status, |
| | 61 | 330 | | CreatedUtc, |
| | 61 | 331 | | updatedUtc ?? DateTimeOffset.UtcNow, |
| | 61 | 332 | | retryCount, |
| | 61 | 333 | | MaxRetryCount, |
| | 61 | 334 | | nextRetryUtc, |
| | 61 | 335 | | lastError, |
| | 61 | 336 | | providerName, |
| | 61 | 337 | | providerRecordId, |
| | 61 | 338 | | deadLetterReason, |
| | 61 | 339 | | metadata); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | private static string NormalizeIdentifier(string? identifier) |
| | | 343 | | { |
| | 101 | 344 | | return string.IsNullOrWhiteSpace(identifier) |
| | 101 | 345 | | ? Guid.NewGuid().ToString("N") |
| | 101 | 346 | | : identifier.Trim(); |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | private static string? NormalizeOptional(string? value) |
| | | 350 | | { |
| | 807 | 351 | | return string.IsNullOrWhiteSpace(value) |
| | 807 | 352 | | ? null |
| | 807 | 353 | | : value.Trim(); |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private static IReadOnlyDictionary<string, string> MergeMetadata( |
| | | 357 | | IReadOnlyDictionary<string, string> originalMetadata, |
| | | 358 | | IReadOnlyDictionary<string, string> resultMetadata) |
| | | 359 | | { |
| | 19 | 360 | | return NormalizeMetadata(originalMetadata, resultMetadata); |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 364 | | params IReadOnlyDictionary<string, string>?[] metadataSets) |
| | | 365 | | { |
| | 232 | 366 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 367 | | |
| | 966 | 368 | | foreach (IReadOnlyDictionary<string, string>? metadata in metadataSets) |
| | | 369 | | { |
| | 251 | 370 | | if (metadata is null || metadata.Count == 0) |
| | | 371 | | { |
| | | 372 | | continue; |
| | | 373 | | } |
| | | 374 | | |
| | 92 | 375 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 376 | | { |
| | 29 | 377 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 378 | | { |
| | | 379 | | continue; |
| | | 380 | | } |
| | | 381 | | |
| | 27 | 382 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 383 | | } |
| | | 384 | | } |
| | | 385 | | |
| | 232 | 386 | | return normalizedMetadata.Count == 0 |
| | 232 | 387 | | ? EmptyMetadata |
| | 232 | 388 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 389 | | } |
| | | 390 | | } |