| | | 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 | | |
| | 2066 | 20 | | private GovernanceOutboxEntry( |
| | 2066 | 21 | | string outboxEntryId, |
| | 2066 | 22 | | GovernanceEmissionEnvelope envelope, |
| | 2066 | 23 | | GovernanceEmissionStatus status, |
| | 2066 | 24 | | DateTimeOffset createdUtc, |
| | 2066 | 25 | | DateTimeOffset updatedUtc, |
| | 2066 | 26 | | int retryCount, |
| | 2066 | 27 | | int maxRetryCount, |
| | 2066 | 28 | | DateTimeOffset? nextRetryUtc, |
| | 2066 | 29 | | GovernanceEmissionError? lastError, |
| | 2066 | 30 | | string? providerName, |
| | 2066 | 31 | | string? providerRecordId, |
| | 2066 | 32 | | string? deadLetterReason, |
| | 2066 | 33 | | IReadOnlyDictionary<string, string> metadata, |
| | 2066 | 34 | | string? claimOwner, |
| | 2066 | 35 | | string? claimToken, |
| | 2066 | 36 | | DateTimeOffset? claimedUtc, |
| | 2066 | 37 | | DateTimeOffset? claimExpiresUtc, |
| | 2066 | 38 | | int claimAttemptCount) |
| | | 39 | | { |
| | 2066 | 40 | | ArgumentException.ThrowIfNullOrWhiteSpace(outboxEntryId); |
| | 2062 | 41 | | ArgumentNullException.ThrowIfNull(envelope); |
| | | 42 | | |
| | 2060 | 43 | | if (!Enum.IsDefined(status)) |
| | | 44 | | { |
| | 4 | 45 | | throw new ArgumentOutOfRangeException(nameof(status), status, "Outbox status must be defined."); |
| | | 46 | | } |
| | | 47 | | |
| | 2056 | 48 | | if (retryCount < 0) |
| | | 49 | | { |
| | 4 | 50 | | throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "Retry count must be greater than or e |
| | | 51 | | } |
| | | 52 | | |
| | 2052 | 53 | | if (maxRetryCount < 0) |
| | | 54 | | { |
| | 6 | 55 | | throw new ArgumentOutOfRangeException(nameof(maxRetryCount), maxRetryCount, "Maximum retry count must be gre |
| | | 56 | | } |
| | | 57 | | |
| | 2046 | 58 | | if (claimAttemptCount < 0) |
| | | 59 | | { |
| | 2 | 60 | | throw new ArgumentOutOfRangeException(nameof(claimAttemptCount), claimAttemptCount, "Claim attempt count mus |
| | | 61 | | } |
| | | 62 | | |
| | 2044 | 63 | | OutboxEntryId = outboxEntryId.Trim(); |
| | 2044 | 64 | | Envelope = envelope; |
| | 2044 | 65 | | Status = status; |
| | 2044 | 66 | | CreatedUtc = createdUtc.ToUniversalTime(); |
| | 2044 | 67 | | UpdatedUtc = updatedUtc.ToUniversalTime(); |
| | 2044 | 68 | | RetryCount = retryCount; |
| | 2044 | 69 | | MaxRetryCount = maxRetryCount; |
| | 2044 | 70 | | NextRetryUtc = nextRetryUtc?.ToUniversalTime(); |
| | 2044 | 71 | | LastError = lastError; |
| | 2044 | 72 | | ProviderName = NormalizeOptional(providerName); |
| | 2044 | 73 | | ProviderRecordId = NormalizeOptional(providerRecordId); |
| | 2044 | 74 | | DeadLetterReason = NormalizeOptional(deadLetterReason); |
| | 2044 | 75 | | Metadata = metadata; |
| | 2044 | 76 | | ClaimOwner = NormalizeOptional(claimOwner); |
| | 2044 | 77 | | ClaimToken = NormalizeOptional(claimToken); |
| | 2044 | 78 | | ClaimedUtc = claimedUtc?.ToUniversalTime(); |
| | 2044 | 79 | | ClaimExpiresUtc = claimExpiresUtc?.ToUniversalTime(); |
| | 2044 | 80 | | ClaimAttemptCount = claimAttemptCount; |
| | 2044 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the stable outbox entry identifier. |
| | | 85 | | /// </summary> |
| | 3539 | 86 | | public string OutboxEntryId { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the provider-neutral governance emission envelope being persisted for delivery. |
| | | 90 | | /// </summary> |
| | 1920 | 91 | | public GovernanceEmissionEnvelope Envelope { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the current provider-neutral outbox status. |
| | | 95 | | /// </summary> |
| | 4395 | 96 | | public GovernanceEmissionStatus Status { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the UTC timestamp when the outbox entry was created. |
| | | 100 | | /// </summary> |
| | 1729 | 101 | | public DateTimeOffset CreatedUtc { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the UTC timestamp when the outbox entry was last updated. |
| | | 105 | | /// </summary> |
| | 994 | 106 | | public DateTimeOffset UpdatedUtc { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the number of failed or deferred delivery attempts recorded for this entry. |
| | | 110 | | /// </summary> |
| | 1825 | 111 | | public int RetryCount { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the maximum retry count before the entry should transition to dead-lettered. |
| | | 115 | | /// </summary> |
| | 1817 | 116 | | public int MaxRetryCount { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the next UTC retry timestamp, when retry scheduling is active. |
| | | 120 | | /// </summary> |
| | 1551 | 121 | | public DateTimeOffset? NextRetryUtc { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the last provider-neutral emission error, when available. |
| | | 125 | | /// </summary> |
| | 1547 | 126 | | public GovernanceEmissionError? LastError { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the provider name associated with the most recent attempt, when available. |
| | | 130 | | /// </summary> |
| | 1464 | 131 | | public string? ProviderName { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the provider-side record identifier, when delivery returned one and it is safe to store. |
| | | 135 | | /// </summary> |
| | 1454 | 136 | | public string? ProviderRecordId { get; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the dead-letter reason, when the entry has reached a terminal dead-letter state. |
| | | 140 | | /// </summary> |
| | 1444 | 141 | | public string? DeadLetterReason { get; } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets minimized provider-neutral outbox metadata. |
| | | 145 | | /// </summary> |
| | 1695 | 146 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Gets the current claim owner, when this row is leased by a worker. |
| | | 150 | | /// </summary> |
| | 2144 | 151 | | public string? ClaimOwner { get; } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Gets the opaque token for the current claim lease. |
| | | 155 | | /// </summary> |
| | 1572 | 156 | | public string? ClaimToken { get; } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Gets the UTC timestamp when the current claim was acquired. |
| | | 160 | | /// </summary> |
| | 1442 | 161 | | public DateTimeOffset? ClaimedUtc { get; } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Gets the UTC timestamp when the current claim lease expires. |
| | | 165 | | /// </summary> |
| | 1460 | 166 | | public DateTimeOffset? ClaimExpiresUtc { get; } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Gets the number of claim or reclaim attempts recorded for this entry. |
| | | 170 | | /// </summary> |
| | 1969 | 171 | | public int ClaimAttemptCount { get; } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets a value indicating whether this entry was delivered successfully. |
| | | 175 | | /// </summary> |
| | 834 | 176 | | public bool IsDelivered => Status is GovernanceEmissionStatus.Delivered; |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Gets a value indicating whether this entry has reached a terminal dead-letter state. |
| | | 180 | | /// </summary> |
| | 805 | 181 | | public bool IsDeadLettered => Status is GovernanceEmissionStatus.DeadLettered; |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets a value indicating whether metadata is present. |
| | | 185 | | /// </summary> |
| | 12 | 186 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Gets a value indicating whether claim metadata is present. |
| | | 190 | | /// </summary> |
| | 590 | 191 | | public bool HasClaim => ClaimOwner is not null && ClaimToken is not null && ClaimedUtc.HasValue && ClaimExpiresUtc.H |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Creates a pending durable governance outbox entry. |
| | | 195 | | /// </summary> |
| | | 196 | | public static GovernanceOutboxEntry Create( |
| | | 197 | | GovernanceEmissionEnvelope envelope, |
| | | 198 | | string? outboxEntryId = null, |
| | | 199 | | DateTimeOffset? createdUtc = null, |
| | | 200 | | int maxRetryCount = DefaultMaxRetryCount, |
| | | 201 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 202 | | { |
| | 645 | 203 | | DateTimeOffset timestamp = createdUtc ?? DateTimeOffset.UtcNow; |
| | | 204 | | |
| | 645 | 205 | | return new GovernanceOutboxEntry( |
| | 645 | 206 | | NormalizeIdentifier(outboxEntryId), |
| | 645 | 207 | | envelope, |
| | 645 | 208 | | GovernanceEmissionStatus.Pending, |
| | 645 | 209 | | timestamp, |
| | 645 | 210 | | timestamp, |
| | 645 | 211 | | retryCount: 0, |
| | 645 | 212 | | maxRetryCount, |
| | 645 | 213 | | nextRetryUtc: null, |
| | 645 | 214 | | lastError: null, |
| | 645 | 215 | | providerName: null, |
| | 645 | 216 | | providerRecordId: null, |
| | 645 | 217 | | deadLetterReason: null, |
| | 645 | 218 | | NormalizeMetadata(metadata), |
| | 645 | 219 | | claimOwner: null, |
| | 645 | 220 | | claimToken: null, |
| | 645 | 221 | | claimedUtc: null, |
| | 645 | 222 | | claimExpiresUtc: null, |
| | 645 | 223 | | claimAttemptCount: 0); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Restores a durable outbox entry from provider-neutral storage. |
| | | 228 | | /// </summary> |
| | | 229 | | /// <remarks> |
| | | 230 | | /// This factory exists for storage adapters. It does not perform provider emission and does not add any provider de |
| | | 231 | | /// </remarks> |
| | | 232 | | public static GovernanceOutboxEntry Restore( |
| | | 233 | | GovernanceEmissionEnvelope envelope, |
| | | 234 | | GovernanceEmissionStatus status, |
| | | 235 | | string outboxEntryId, |
| | | 236 | | DateTimeOffset createdUtc, |
| | | 237 | | DateTimeOffset updatedUtc, |
| | | 238 | | int retryCount = 0, |
| | | 239 | | int maxRetryCount = DefaultMaxRetryCount, |
| | | 240 | | DateTimeOffset? nextRetryUtc = null, |
| | | 241 | | GovernanceEmissionError? lastError = null, |
| | | 242 | | string? providerName = null, |
| | | 243 | | string? providerRecordId = null, |
| | | 244 | | string? deadLetterReason = null, |
| | | 245 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 246 | | string? claimOwner = null, |
| | | 247 | | string? claimToken = null, |
| | | 248 | | DateTimeOffset? claimedUtc = null, |
| | | 249 | | DateTimeOffset? claimExpiresUtc = null, |
| | | 250 | | int claimAttemptCount = 0) |
| | | 251 | | { |
| | 734 | 252 | | return new GovernanceOutboxEntry( |
| | 734 | 253 | | outboxEntryId, |
| | 734 | 254 | | envelope, |
| | 734 | 255 | | status, |
| | 734 | 256 | | createdUtc, |
| | 734 | 257 | | updatedUtc, |
| | 734 | 258 | | retryCount, |
| | 734 | 259 | | maxRetryCount, |
| | 734 | 260 | | nextRetryUtc, |
| | 734 | 261 | | lastError, |
| | 734 | 262 | | providerName, |
| | 734 | 263 | | providerRecordId, |
| | 734 | 264 | | deadLetterReason, |
| | 734 | 265 | | NormalizeMetadata(metadata), |
| | 734 | 266 | | claimOwner, |
| | 734 | 267 | | claimToken, |
| | 734 | 268 | | claimedUtc, |
| | 734 | 269 | | claimExpiresUtc, |
| | 734 | 270 | | claimAttemptCount); |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Determines whether the entry is ready for retry at the supplied UTC timestamp. |
| | | 275 | | /// </summary> |
| | | 276 | | public bool IsRetryReady(DateTimeOffset utcNow) |
| | | 277 | | { |
| | 92 | 278 | | return !IsDelivered && !IsDeadLettered && RetryCount < MaxRetryCount && Status is GovernanceEmissionStatus.Defer |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Determines whether the entry has an active claim lease at the supplied UTC timestamp. |
| | | 283 | | /// </summary> |
| | | 284 | | public bool HasActiveClaim(DateTimeOffset utcNow) |
| | | 285 | | { |
| | 508 | 286 | | return HasClaim && ClaimExpiresUtc > utcNow.ToUniversalTime(); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Determines whether the entry may be claimed by a cooperating worker at the supplied UTC timestamp. |
| | | 291 | | /// </summary> |
| | | 292 | | public bool CanBeClaimed(DateTimeOffset utcNow) |
| | | 293 | | { |
| | 504 | 294 | | return !IsDelivered && !IsDeadLettered && !HasActiveClaim(utcNow); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | /// <summary> |
| | | 298 | | /// Determines whether the entry is still owned by the supplied claim token. |
| | | 299 | | /// </summary> |
| | | 300 | | public bool IsClaimedBy(GovernanceOutboxClaim claim) |
| | | 301 | | { |
| | 112 | 302 | | ArgumentNullException.ThrowIfNull(claim); |
| | | 303 | | |
| | 112 | 304 | | return string.Equals(OutboxEntryId, claim.OutboxEntryId, StringComparison.Ordinal) |
| | 112 | 305 | | && string.Equals(ClaimOwner, claim.WorkerId, StringComparison.Ordinal) |
| | 112 | 306 | | && string.Equals(ClaimToken, claim.ClaimToken, StringComparison.Ordinal); |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Returns a claimed copy of this entry. |
| | | 311 | | /// </summary> |
| | | 312 | | public GovernanceOutboxEntry MarkClaimed( |
| | | 313 | | string claimOwner, |
| | | 314 | | string? claimToken = null, |
| | | 315 | | DateTimeOffset? claimedUtc = null, |
| | | 316 | | TimeSpan? leaseDuration = null) |
| | | 317 | | { |
| | 450 | 318 | | ArgumentException.ThrowIfNullOrWhiteSpace(claimOwner); |
| | | 319 | | |
| | 450 | 320 | | DateTimeOffset normalizedClaimedUtc = (claimedUtc ?? DateTimeOffset.UtcNow).ToUniversalTime(); |
| | 450 | 321 | | TimeSpan normalizedLeaseDuration = leaseDuration ?? GovernanceOutboxClaimRequest.DefaultLeaseDuration; |
| | | 322 | | |
| | 450 | 323 | | return normalizedLeaseDuration <= TimeSpan.Zero |
| | 450 | 324 | | ? throw new ArgumentOutOfRangeException(nameof(leaseDuration), leaseDuration, "Lease duration must be greate |
| | 450 | 325 | | : Copy( |
| | 450 | 326 | | Status, |
| | 450 | 327 | | normalizedClaimedUtc, |
| | 450 | 328 | | RetryCount, |
| | 450 | 329 | | NextRetryUtc, |
| | 450 | 330 | | LastError, |
| | 450 | 331 | | ProviderName, |
| | 450 | 332 | | ProviderRecordId, |
| | 450 | 333 | | DeadLetterReason, |
| | 450 | 334 | | Metadata, |
| | 450 | 335 | | claimOwner.Trim(), |
| | 450 | 336 | | string.IsNullOrWhiteSpace(claimToken) ? Guid.NewGuid().ToString("N") : claimToken.Trim(), |
| | 450 | 337 | | normalizedClaimedUtc, |
| | 450 | 338 | | normalizedClaimedUtc.Add(normalizedLeaseDuration), |
| | 450 | 339 | | ClaimAttemptCount + 1); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary> |
| | | 343 | | /// Returns a copy with claim lease fields cleared. |
| | | 344 | | /// </summary> |
| | | 345 | | public GovernanceOutboxEntry ReleaseClaim(DateTimeOffset? updatedUtc = null) |
| | | 346 | | { |
| | 10 | 347 | | return Copy( |
| | 10 | 348 | | Status, |
| | 10 | 349 | | updatedUtc, |
| | 10 | 350 | | RetryCount, |
| | 10 | 351 | | NextRetryUtc, |
| | 10 | 352 | | LastError, |
| | 10 | 353 | | ProviderName, |
| | 10 | 354 | | ProviderRecordId, |
| | 10 | 355 | | DeadLetterReason, |
| | 10 | 356 | | Metadata, |
| | 10 | 357 | | claimOwner: null, |
| | 10 | 358 | | claimToken: null, |
| | 10 | 359 | | claimedUtc: null, |
| | 10 | 360 | | claimExpiresUtc: null, |
| | 10 | 361 | | ClaimAttemptCount); |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <summary> |
| | | 365 | | /// Returns a delivered copy of this entry. |
| | | 366 | | /// </summary> |
| | | 367 | | public GovernanceOutboxEntry MarkDelivered( |
| | | 368 | | GovernanceEmissionResult result, |
| | | 369 | | DateTimeOffset? updatedUtc = null) |
| | | 370 | | { |
| | 76 | 371 | | ArgumentNullException.ThrowIfNull(result); |
| | | 372 | | |
| | 76 | 373 | | return !result.IsSuccess |
| | 76 | 374 | | ? throw new ArgumentException("Delivered outbox transitions require a successful emission result.", nameof(r |
| | 76 | 375 | | : Copy( |
| | 76 | 376 | | GovernanceEmissionStatus.Delivered, |
| | 76 | 377 | | updatedUtc, |
| | 76 | 378 | | retryCount: RetryCount, |
| | 76 | 379 | | nextRetryUtc: null, |
| | 76 | 380 | | lastError: null, |
| | 76 | 381 | | providerName: result.ProviderName, |
| | 76 | 382 | | providerRecordId: result.ProviderRecordId, |
| | 76 | 383 | | deadLetterReason: null, |
| | 76 | 384 | | metadata: MergeMetadata(Metadata, result.Metadata), |
| | 76 | 385 | | claimOwner: null, |
| | 76 | 386 | | claimToken: null, |
| | 76 | 387 | | claimedUtc: null, |
| | 76 | 388 | | claimExpiresUtc: null, |
| | 76 | 389 | | ClaimAttemptCount); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Returns a failed or retryable-failure copy of this entry. |
| | | 394 | | /// </summary> |
| | | 395 | | public GovernanceOutboxEntry MarkFailed( |
| | | 396 | | GovernanceEmissionError governanceEmissionError, |
| | | 397 | | DateTimeOffset? nextRetryUtc = null, |
| | | 398 | | DateTimeOffset? updatedUtc = null) |
| | | 399 | | { |
| | 89 | 400 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | | 401 | | |
| | 89 | 402 | | int nextRetryCount = RetryCount + 1; |
| | 89 | 403 | | GovernanceEmissionStatus nextStatus = nextRetryCount >= MaxRetryCount |
| | 89 | 404 | | ? GovernanceEmissionStatus.DeadLettered |
| | 89 | 405 | | : governanceEmissionError.IsRetryable |
| | 89 | 406 | | ? GovernanceEmissionStatus.RetryableFailure |
| | 89 | 407 | | : GovernanceEmissionStatus.Failed; |
| | | 408 | | |
| | 89 | 409 | | return Copy( |
| | 89 | 410 | | nextStatus, |
| | 89 | 411 | | updatedUtc, |
| | 89 | 412 | | nextRetryCount, |
| | 89 | 413 | | nextStatus is GovernanceEmissionStatus.DeadLettered ? null : nextRetryUtc, |
| | 89 | 414 | | governanceEmissionError, |
| | 89 | 415 | | governanceEmissionError.ProviderName, |
| | 89 | 416 | | providerRecordId: null, |
| | 89 | 417 | | nextStatus is GovernanceEmissionStatus.DeadLettered ? governanceEmissionError.Message : null, |
| | 89 | 418 | | Metadata, |
| | 89 | 419 | | claimOwner: null, |
| | 89 | 420 | | claimToken: null, |
| | 89 | 421 | | claimedUtc: null, |
| | 89 | 422 | | claimExpiresUtc: null, |
| | 89 | 423 | | ClaimAttemptCount); |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | /// <summary> |
| | | 427 | | /// Returns a deferred copy of this entry. |
| | | 428 | | /// </summary> |
| | | 429 | | public GovernanceOutboxEntry MarkDeferred( |
| | | 430 | | GovernanceEmissionError? governanceEmissionError = null, |
| | | 431 | | DateTimeOffset? nextRetryUtc = null, |
| | | 432 | | DateTimeOffset? updatedUtc = null) |
| | | 433 | | { |
| | 40 | 434 | | return Copy( |
| | 40 | 435 | | GovernanceEmissionStatus.Deferred, |
| | 40 | 436 | | updatedUtc, |
| | 40 | 437 | | retryCount: RetryCount, |
| | 40 | 438 | | nextRetryUtc, |
| | 40 | 439 | | governanceEmissionError, |
| | 40 | 440 | | governanceEmissionError?.ProviderName, |
| | 40 | 441 | | providerRecordId: null, |
| | 40 | 442 | | deadLetterReason: null, |
| | 40 | 443 | | metadata: Metadata, |
| | 40 | 444 | | claimOwner: null, |
| | 40 | 445 | | claimToken: null, |
| | 40 | 446 | | claimedUtc: null, |
| | 40 | 447 | | claimExpiresUtc: null, |
| | 40 | 448 | | ClaimAttemptCount); |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | /// <summary> |
| | | 452 | | /// Returns a dead-lettered copy of this entry. |
| | | 453 | | /// </summary> |
| | | 454 | | public GovernanceOutboxEntry MarkDeadLettered( |
| | | 455 | | GovernanceEmissionError governanceEmissionError, |
| | | 456 | | string? deadLetterReason = null, |
| | | 457 | | DateTimeOffset? updatedUtc = null) |
| | | 458 | | { |
| | 30 | 459 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | | 460 | | |
| | 30 | 461 | | return Copy( |
| | 30 | 462 | | GovernanceEmissionStatus.DeadLettered, |
| | 30 | 463 | | updatedUtc, |
| | 30 | 464 | | retryCount: RetryCount, |
| | 30 | 465 | | nextRetryUtc: null, |
| | 30 | 466 | | lastError: governanceEmissionError, |
| | 30 | 467 | | providerName: governanceEmissionError.ProviderName, |
| | 30 | 468 | | providerRecordId: null, |
| | 30 | 469 | | deadLetterReason: deadLetterReason ?? governanceEmissionError.Message, |
| | 30 | 470 | | metadata: Metadata, |
| | 30 | 471 | | claimOwner: null, |
| | 30 | 472 | | claimToken: null, |
| | 30 | 473 | | claimedUtc: null, |
| | 30 | 474 | | claimExpiresUtc: null, |
| | 30 | 475 | | ClaimAttemptCount); |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private GovernanceOutboxEntry Copy( |
| | | 479 | | GovernanceEmissionStatus status, |
| | | 480 | | DateTimeOffset? updatedUtc, |
| | | 481 | | int retryCount, |
| | | 482 | | DateTimeOffset? nextRetryUtc, |
| | | 483 | | GovernanceEmissionError? lastError, |
| | | 484 | | string? providerName, |
| | | 485 | | string? providerRecordId, |
| | | 486 | | string? deadLetterReason, |
| | | 487 | | IReadOnlyDictionary<string, string> metadata, |
| | | 488 | | string? claimOwner, |
| | | 489 | | string? claimToken, |
| | | 490 | | DateTimeOffset? claimedUtc, |
| | | 491 | | DateTimeOffset? claimExpiresUtc, |
| | | 492 | | int claimAttemptCount) |
| | | 493 | | { |
| | 687 | 494 | | return new GovernanceOutboxEntry( |
| | 687 | 495 | | OutboxEntryId, |
| | 687 | 496 | | Envelope, |
| | 687 | 497 | | status, |
| | 687 | 498 | | CreatedUtc, |
| | 687 | 499 | | updatedUtc ?? DateTimeOffset.UtcNow, |
| | 687 | 500 | | retryCount, |
| | 687 | 501 | | MaxRetryCount, |
| | 687 | 502 | | nextRetryUtc, |
| | 687 | 503 | | lastError, |
| | 687 | 504 | | providerName, |
| | 687 | 505 | | providerRecordId, |
| | 687 | 506 | | deadLetterReason, |
| | 687 | 507 | | metadata, |
| | 687 | 508 | | claimOwner, |
| | 687 | 509 | | claimToken, |
| | 687 | 510 | | claimedUtc, |
| | 687 | 511 | | claimExpiresUtc, |
| | 687 | 512 | | claimAttemptCount); |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | private static string NormalizeIdentifier(string? identifier) |
| | | 516 | | { |
| | 645 | 517 | | return string.IsNullOrWhiteSpace(identifier) |
| | 645 | 518 | | ? Guid.NewGuid().ToString("N") |
| | 645 | 519 | | : identifier.Trim(); |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | private static string? NormalizeOptional(string? value) |
| | | 523 | | { |
| | 10220 | 524 | | return string.IsNullOrWhiteSpace(value) |
| | 10220 | 525 | | ? null |
| | 10220 | 526 | | : value.Trim(); |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | private static IReadOnlyDictionary<string, string> MergeMetadata( |
| | | 530 | | IReadOnlyDictionary<string, string> originalMetadata, |
| | | 531 | | IReadOnlyDictionary<string, string> resultMetadata) |
| | | 532 | | { |
| | 72 | 533 | | if (resultMetadata.Count == 0) |
| | | 534 | | { |
| | 58 | 535 | | return originalMetadata; |
| | | 536 | | } |
| | | 537 | | |
| | 14 | 538 | | if (originalMetadata.Count == 0) |
| | | 539 | | { |
| | 10 | 540 | | return resultMetadata; |
| | | 541 | | } |
| | | 542 | | |
| | 4 | 543 | | Dictionary<string, string> mergedMetadata = new(originalMetadata.Count + resultMetadata.Count, StringComparer.Or |
| | | 544 | | |
| | 20 | 545 | | foreach (KeyValuePair<string, string> item in originalMetadata) |
| | | 546 | | { |
| | 6 | 547 | | mergedMetadata[item.Key] = item.Value; |
| | | 548 | | } |
| | | 549 | | |
| | 20 | 550 | | foreach (KeyValuePair<string, string> item in resultMetadata) |
| | | 551 | | { |
| | 6 | 552 | | mergedMetadata[item.Key] = item.Value; |
| | | 553 | | } |
| | | 554 | | |
| | 4 | 555 | | return new ReadOnlyDictionary<string, string>(mergedMetadata); |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 559 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 560 | | { |
| | 1379 | 561 | | if (metadata is null || metadata.Count == 0) |
| | | 562 | | { |
| | 1251 | 563 | | return EmptyMetadata; |
| | | 564 | | } |
| | | 565 | | |
| | 128 | 566 | | Dictionary<string, string> normalizedMetadata = new(metadata.Count, StringComparer.Ordinal); |
| | | 567 | | |
| | 556 | 568 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 569 | | { |
| | 150 | 570 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 571 | | { |
| | | 572 | | continue; |
| | | 573 | | } |
| | | 574 | | |
| | 144 | 575 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 576 | | } |
| | | 577 | | |
| | 128 | 578 | | return normalizedMetadata.Count == 0 |
| | 128 | 579 | | ? EmptyMetadata |
| | 128 | 580 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 581 | | } |
| | | 582 | | } |