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