| | | 1 | | using AsiBackbone.Core.Emissions; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Core.Outbox; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Drains provider-neutral governance outbox entries through a configured governance emitter. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// This drain path is provider-neutral. It is suitable for tests, samples, local validation, and host-owned workers tha |
| | | 13 | | /// </remarks> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Initializes a new instance of the <see cref="AsiBackboneGovernanceOutboxDrain" /> class. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="outboxStore">The provider-neutral outbox store.</param> |
| | | 18 | | /// <param name="emitter">The provider-neutral governance emitter.</param> |
| | | 19 | | /// <param name="logger">The logger used to record local operational diagnostics for drain failures.</param> |
| | | 20 | | /// <param name="outboxOptions">The provider-neutral retry, poison-message, and claim options used by the drain.</param> |
| | 108 | 21 | | public sealed class AsiBackboneGovernanceOutboxDrain( |
| | 108 | 22 | | IAsiBackboneGovernanceOutboxStore outboxStore, |
| | 108 | 23 | | IAsiBackboneGovernanceEmitter emitter, |
| | 108 | 24 | | ILogger<AsiBackboneGovernanceOutboxDrain>? logger = null, |
| | 108 | 25 | | IOptions<AsiBackboneGovernanceOutboxOptions>? outboxOptions = null) |
| | | 26 | | { |
| | 3 | 27 | | private static readonly Action<ILogger, string, int, string, DateTimeOffset, string?, string?, Exception?> LogGovern |
| | 3 | 28 | | LogLevel.Warning, |
| | 3 | 29 | | new EventId(19701, nameof(LogGovernanceEmissionException)), |
| | 3 | 30 | | "Governance outbox emission threw an exception for outbox entry {OutboxEntryId} on attempt {AttemptCount}. Emitt |
| | | 31 | | |
| | 116 | 32 | | private readonly IAsiBackboneGovernanceOutboxStore outboxStore = outboxStore ?? throw new ArgumentNullException(name |
| | 114 | 33 | | private readonly IAsiBackboneGovernanceEmitter emitter = emitter ?? throw new ArgumentNullException(nameof(emitter)) |
| | 112 | 34 | | private readonly ILogger<AsiBackboneGovernanceOutboxDrain> logger = logger ?? NullLogger<AsiBackboneGovernanceOutbox |
| | 112 | 35 | | private readonly AsiBackboneGovernanceOutboxOptions retryOptions = ResolveOptions(outboxOptions); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Drains pending and retry-ready outbox entries through the configured emitter. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="utcNow">The UTC timestamp used for retry-ready checks.</param> |
| | | 41 | | /// <param name="maxCount">The maximum number of entries to drain.</param> |
| | | 42 | | /// <param name="cancellationToken">A cancellation token.</param> |
| | | 43 | | /// <returns>The updated outbox entries that were attempted by the drain.</returns> |
| | | 44 | | public async ValueTask<IReadOnlyList<GovernanceOutboxEntry>> DrainAsync( |
| | | 45 | | DateTimeOffset? utcNow = null, |
| | | 46 | | int maxCount = 100, |
| | | 47 | | CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 112 | 49 | | if (maxCount <= 0) |
| | | 50 | | { |
| | 2 | 51 | | throw new ArgumentOutOfRangeException(nameof(maxCount), maxCount, "Maximum count must be greater than zero." |
| | | 52 | | } |
| | | 53 | | |
| | 110 | 54 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 55 | | |
| | 110 | 56 | | DateTimeOffset drainUtc = (utcNow ?? DateTimeOffset.UtcNow).ToUniversalTime(); |
| | | 57 | | |
| | 110 | 58 | | if (retryOptions.UseClaimLeases) |
| | | 59 | | { |
| | 42 | 60 | | return await DrainClaimedAsync(drainUtc, maxCount, cancellationToken).ConfigureAwait(false); |
| | | 61 | | } |
| | | 62 | | |
| | 68 | 63 | | IReadOnlyList<GovernanceOutboxEntry> pendingEntries = await outboxStore |
| | 68 | 64 | | .FindPendingAsync(maxCount, cancellationToken) |
| | 68 | 65 | | .ConfigureAwait(false); |
| | | 66 | | |
| | 65 | 67 | | if (pendingEntries.Count >= maxCount) |
| | | 68 | | { |
| | 12 | 69 | | return await DrainEntriesAsync(pendingEntries, drainUtc, cancellationToken).ConfigureAwait(false); |
| | | 70 | | } |
| | | 71 | | |
| | 53 | 72 | | IReadOnlyList<GovernanceOutboxEntry> retryReadyEntries = await outboxStore |
| | 53 | 73 | | .FindRetryReadyAsync(drainUtc, maxCount - pendingEntries.Count, cancellationToken) |
| | 53 | 74 | | .ConfigureAwait(false); |
| | | 75 | | |
| | 53 | 76 | | IReadOnlyList<GovernanceOutboxEntry> entriesToDrain = MergeEntries(pendingEntries, retryReadyEntries, maxCount); |
| | 53 | 77 | | return await DrainEntriesAsync(entriesToDrain, drainUtc, cancellationToken).ConfigureAwait(false); |
| | 99 | 78 | | } |
| | | 79 | | |
| | | 80 | | private async ValueTask<IReadOnlyList<GovernanceOutboxEntry>> DrainClaimedAsync( |
| | | 81 | | DateTimeOffset drainUtc, |
| | | 82 | | int maxCount, |
| | | 83 | | CancellationToken cancellationToken) |
| | | 84 | | { |
| | 42 | 85 | | if (outboxStore is not IAsiBackboneGovernanceOutboxClaimStore claimStore) |
| | | 86 | | { |
| | 4 | 87 | | throw new InvalidOperationException("Claim leases are enabled, but the configured outbox store does not impl |
| | | 88 | | } |
| | | 89 | | |
| | 38 | 90 | | string workerId = retryOptions.ClaimWorkerId ?? throw new InvalidOperationException("ClaimWorkerId is required w |
| | 38 | 91 | | var pendingRequest = GovernanceOutboxClaimRequest.Create( |
| | 38 | 92 | | workerId, |
| | 38 | 93 | | drainUtc, |
| | 38 | 94 | | retryOptions.ClaimLeaseDuration, |
| | 38 | 95 | | maxCount); |
| | 38 | 96 | | IReadOnlyList<GovernanceOutboxClaim> pendingClaims = await claimStore |
| | 38 | 97 | | .ClaimPendingAsync(pendingRequest, cancellationToken) |
| | 38 | 98 | | .ConfigureAwait(false); |
| | | 99 | | |
| | 38 | 100 | | if (pendingClaims.Count >= maxCount) |
| | | 101 | | { |
| | 4 | 102 | | return await DrainClaimsAsync(claimStore, pendingClaims, drainUtc, cancellationToken).ConfigureAwait(false); |
| | | 103 | | } |
| | | 104 | | |
| | 34 | 105 | | var retryRequest = GovernanceOutboxClaimRequest.Create( |
| | 34 | 106 | | workerId, |
| | 34 | 107 | | drainUtc, |
| | 34 | 108 | | retryOptions.ClaimLeaseDuration, |
| | 34 | 109 | | maxCount - pendingClaims.Count); |
| | 34 | 110 | | IReadOnlyList<GovernanceOutboxClaim> retryReadyClaims = await claimStore |
| | 34 | 111 | | .ClaimRetryReadyAsync(retryRequest, cancellationToken) |
| | 34 | 112 | | .ConfigureAwait(false); |
| | | 113 | | |
| | 34 | 114 | | IReadOnlyList<GovernanceOutboxClaim> claimsToDrain = MergeClaims(pendingClaims, retryReadyClaims, maxCount); |
| | 34 | 115 | | return await DrainClaimsAsync(claimStore, claimsToDrain, drainUtc, cancellationToken).ConfigureAwait(false); |
| | 38 | 116 | | } |
| | | 117 | | |
| | | 118 | | private async ValueTask<IReadOnlyList<GovernanceOutboxEntry>> DrainEntriesAsync( |
| | | 119 | | IReadOnlyList<GovernanceOutboxEntry> entriesToDrain, |
| | | 120 | | DateTimeOffset drainUtc, |
| | | 121 | | CancellationToken cancellationToken) |
| | | 122 | | { |
| | 65 | 123 | | if (entriesToDrain.Count == 0) |
| | | 124 | | { |
| | 10 | 125 | | return Array.Empty<GovernanceOutboxEntry>(); |
| | | 126 | | } |
| | | 127 | | |
| | 55 | 128 | | List<GovernanceOutboxEntry> updatedEntries = new(entriesToDrain.Count); |
| | | 129 | | |
| | 240 | 130 | | foreach (GovernanceOutboxEntry entry in entriesToDrain) |
| | | 131 | | { |
| | 67 | 132 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 67 | 133 | | GovernanceOutboxEntry updatedEntry = await DrainEntryAsync(entry, drainUtc, cancellationToken).ConfigureAwai |
| | 63 | 134 | | updatedEntries.Add(updatedEntry); |
| | | 135 | | } |
| | | 136 | | |
| | 51 | 137 | | return updatedEntries; |
| | 61 | 138 | | } |
| | | 139 | | |
| | | 140 | | private async ValueTask<IReadOnlyList<GovernanceOutboxEntry>> DrainClaimsAsync( |
| | | 141 | | IAsiBackboneGovernanceOutboxClaimStore claimStore, |
| | | 142 | | IReadOnlyList<GovernanceOutboxClaim> claimsToDrain, |
| | | 143 | | DateTimeOffset drainUtc, |
| | | 144 | | CancellationToken cancellationToken) |
| | | 145 | | { |
| | 38 | 146 | | if (claimsToDrain.Count == 0) |
| | | 147 | | { |
| | 4 | 148 | | return Array.Empty<GovernanceOutboxEntry>(); |
| | | 149 | | } |
| | | 150 | | |
| | 34 | 151 | | List<GovernanceOutboxEntry> updatedEntries = new(claimsToDrain.Count); |
| | | 152 | | |
| | 136 | 153 | | foreach (GovernanceOutboxClaim claim in claimsToDrain) |
| | | 154 | | { |
| | 34 | 155 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 34 | 156 | | GovernanceOutboxEntry updatedEntry = await DrainClaimAsync(claimStore, claim, drainUtc, cancellationToken).C |
| | 34 | 157 | | updatedEntries.Add(updatedEntry); |
| | | 158 | | } |
| | | 159 | | |
| | 34 | 160 | | return updatedEntries; |
| | 38 | 161 | | } |
| | | 162 | | |
| | | 163 | | private static IReadOnlyList<GovernanceOutboxEntry> MergeEntries( |
| | | 164 | | IReadOnlyList<GovernanceOutboxEntry> pendingEntries, |
| | | 165 | | IReadOnlyList<GovernanceOutboxEntry> retryReadyEntries, |
| | | 166 | | int maxCount) |
| | | 167 | | { |
| | 53 | 168 | | if (pendingEntries.Count == 0) |
| | | 169 | | { |
| | 12 | 170 | | return retryReadyEntries; |
| | | 171 | | } |
| | | 172 | | |
| | 41 | 173 | | if (retryReadyEntries.Count == 0) |
| | | 174 | | { |
| | 37 | 175 | | return pendingEntries; |
| | | 176 | | } |
| | | 177 | | |
| | 4 | 178 | | var entriesToDrain = new List<GovernanceOutboxEntry>(Math.Min(maxCount, pendingEntries.Count + retryReadyEntries |
| | 4 | 179 | | var existingEntryIds = new HashSet<string>(pendingEntries.Count + retryReadyEntries.Count, StringComparer.Ordina |
| | | 180 | | |
| | 16 | 181 | | foreach (GovernanceOutboxEntry pendingEntry in pendingEntries) |
| | | 182 | | { |
| | 4 | 183 | | _ = existingEntryIds.Add(pendingEntry.OutboxEntryId); |
| | 4 | 184 | | entriesToDrain.Add(pendingEntry); |
| | | 185 | | } |
| | | 186 | | |
| | 20 | 187 | | foreach (GovernanceOutboxEntry retryReadyEntry in retryReadyEntries) |
| | | 188 | | { |
| | 6 | 189 | | if (entriesToDrain.Count >= maxCount) |
| | | 190 | | { |
| | 0 | 191 | | break; |
| | | 192 | | } |
| | | 193 | | |
| | 6 | 194 | | if (existingEntryIds.Add(retryReadyEntry.OutboxEntryId)) |
| | | 195 | | { |
| | 4 | 196 | | entriesToDrain.Add(retryReadyEntry); |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | 4 | 200 | | return entriesToDrain; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private static IReadOnlyList<GovernanceOutboxClaim> MergeClaims( |
| | | 204 | | IReadOnlyList<GovernanceOutboxClaim> pendingClaims, |
| | | 205 | | IReadOnlyList<GovernanceOutboxClaim> retryReadyClaims, |
| | | 206 | | int maxCount) |
| | | 207 | | { |
| | 48 | 208 | | if (pendingClaims.Count == 0) |
| | | 209 | | { |
| | 14 | 210 | | return retryReadyClaims; |
| | | 211 | | } |
| | | 212 | | |
| | 34 | 213 | | if (retryReadyClaims.Count == 0) |
| | | 214 | | { |
| | 26 | 215 | | return pendingClaims; |
| | | 216 | | } |
| | | 217 | | |
| | 8 | 218 | | var claimsToDrain = new List<GovernanceOutboxClaim>(Math.Min(maxCount, pendingClaims.Count + retryReadyClaims.Co |
| | 8 | 219 | | var existingEntryIds = new HashSet<string>(pendingClaims.Count + retryReadyClaims.Count, StringComparer.Ordinal) |
| | | 220 | | |
| | 36 | 221 | | foreach (GovernanceOutboxClaim pendingClaim in pendingClaims) |
| | | 222 | | { |
| | 10 | 223 | | _ = existingEntryIds.Add(pendingClaim.OutboxEntryId); |
| | 10 | 224 | | claimsToDrain.Add(pendingClaim); |
| | | 225 | | } |
| | | 226 | | |
| | 70 | 227 | | foreach (GovernanceOutboxClaim retryReadyClaim in retryReadyClaims) |
| | | 228 | | { |
| | 28 | 229 | | if (claimsToDrain.Count >= maxCount) |
| | | 230 | | { |
| | 2 | 231 | | break; |
| | | 232 | | } |
| | | 233 | | |
| | 26 | 234 | | if (existingEntryIds.Add(retryReadyClaim.OutboxEntryId)) |
| | | 235 | | { |
| | 14 | 236 | | claimsToDrain.Add(retryReadyClaim); |
| | | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | 8 | 240 | | return claimsToDrain; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | private async ValueTask<GovernanceOutboxEntry> DrainEntryAsync( |
| | | 244 | | GovernanceOutboxEntry entry, |
| | | 245 | | DateTimeOffset drainUtc, |
| | | 246 | | CancellationToken cancellationToken) |
| | | 247 | | { |
| | | 248 | | GovernanceEmissionResult result; |
| | | 249 | | |
| | | 250 | | try |
| | | 251 | | { |
| | 67 | 252 | | result = await emitter.EmitAsync(entry.Envelope, cancellationToken).ConfigureAwait(false); |
| | 58 | 253 | | } |
| | 2 | 254 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 255 | | { |
| | 2 | 256 | | throw; |
| | | 257 | | } |
| | 7 | 258 | | catch (Exception ex) |
| | | 259 | | { |
| | 7 | 260 | | DateTimeOffset nextRetryUtc = GetRetryUtc(drainUtc); |
| | 7 | 261 | | LogEmissionException(entry, nextRetryUtc, ex); |
| | 7 | 262 | | GovernanceEmissionError governanceEmissionError = CreateExceptionError(ex); |
| | | 263 | | |
| | 7 | 264 | | return await ApplyFailureAsync( |
| | 7 | 265 | | entry, |
| | 7 | 266 | | governanceEmissionError, |
| | 7 | 267 | | nextRetryUtc, |
| | 7 | 268 | | cancellationToken) |
| | 7 | 269 | | .ConfigureAwait(false); |
| | | 270 | | } |
| | | 271 | | |
| | 58 | 272 | | return await ApplyEmissionResultAsync(entry, result, drainUtc, cancellationToken).ConfigureAwait(false); |
| | 63 | 273 | | } |
| | | 274 | | |
| | | 275 | | private async ValueTask<GovernanceOutboxEntry> DrainClaimAsync( |
| | | 276 | | IAsiBackboneGovernanceOutboxClaimStore claimStore, |
| | | 277 | | GovernanceOutboxClaim claim, |
| | | 278 | | DateTimeOffset drainUtc, |
| | | 279 | | CancellationToken cancellationToken) |
| | | 280 | | { |
| | | 281 | | GovernanceEmissionResult result; |
| | | 282 | | |
| | | 283 | | try |
| | | 284 | | { |
| | 34 | 285 | | result = await emitter.EmitAsync(claim.Entry.Envelope, cancellationToken).ConfigureAwait(false); |
| | 30 | 286 | | } |
| | 0 | 287 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 288 | | { |
| | 0 | 289 | | throw; |
| | | 290 | | } |
| | 4 | 291 | | catch (Exception ex) |
| | | 292 | | { |
| | 4 | 293 | | DateTimeOffset nextRetryUtc = GetRetryUtc(drainUtc); |
| | 4 | 294 | | LogEmissionException(claim.Entry, nextRetryUtc, ex); |
| | 4 | 295 | | GovernanceEmissionError governanceEmissionError = CreateExceptionError(ex); |
| | | 296 | | |
| | 4 | 297 | | return await ApplyClaimFailureAsync( |
| | 4 | 298 | | claimStore, |
| | 4 | 299 | | claim, |
| | 4 | 300 | | governanceEmissionError, |
| | 4 | 301 | | nextRetryUtc, |
| | 4 | 302 | | cancellationToken) |
| | 4 | 303 | | .ConfigureAwait(false); |
| | | 304 | | } |
| | | 305 | | |
| | 30 | 306 | | return await ApplyEmissionResultAsync(claimStore, claim, result, drainUtc, cancellationToken).ConfigureAwait(fal |
| | 34 | 307 | | } |
| | | 308 | | |
| | | 309 | | private async ValueTask<GovernanceOutboxEntry> ApplyEmissionResultAsync( |
| | | 310 | | GovernanceOutboxEntry entry, |
| | | 311 | | GovernanceEmissionResult result, |
| | | 312 | | DateTimeOffset drainUtc, |
| | | 313 | | CancellationToken cancellationToken) |
| | | 314 | | { |
| | 58 | 315 | | ArgumentNullException.ThrowIfNull(result); |
| | | 316 | | |
| | 58 | 317 | | if (result.IsSuccess) |
| | | 318 | | { |
| | 22 | 319 | | return await outboxStore.MarkDeliveredAsync(entry.OutboxEntryId, result, cancellationToken).ConfigureAwait(f |
| | | 320 | | } |
| | | 321 | | |
| | 36 | 322 | | if (result.Status is GovernanceEmissionStatus.DeadLettered) |
| | | 323 | | { |
| | 4 | 324 | | GovernanceEmissionError governanceEmissionError = result.Error ?? GovernanceEmissionError.Create( |
| | 4 | 325 | | "emission.deadlettered", |
| | 4 | 326 | | "Governance emission returned a dead-lettered result.", |
| | 4 | 327 | | providerName: result.ProviderName); |
| | | 328 | | |
| | 4 | 329 | | return await outboxStore.MarkDeadLetteredAsync( |
| | 4 | 330 | | entry.OutboxEntryId, |
| | 4 | 331 | | governanceEmissionError, |
| | 4 | 332 | | governanceEmissionError.Message, |
| | 4 | 333 | | cancellationToken) |
| | 4 | 334 | | .ConfigureAwait(false); |
| | | 335 | | } |
| | | 336 | | |
| | 32 | 337 | | if (result.Status is GovernanceEmissionStatus.Deferred or GovernanceEmissionStatus.Pending) |
| | | 338 | | { |
| | 14 | 339 | | GovernanceEmissionError? governanceEmissionError = result.Error ?? (result.Status is GovernanceEmissionStatu |
| | 14 | 340 | | ? GovernanceEmissionError.Create( |
| | 14 | 341 | | "emission.pending", |
| | 14 | 342 | | "Governance emission remained pending after the outbox drain attempt.", |
| | 14 | 343 | | isRetryable: true, |
| | 14 | 344 | | providerName: result.ProviderName) |
| | 14 | 345 | | : null); |
| | | 346 | | |
| | 14 | 347 | | GovernanceOutboxEntry deferredEntry = entry.MarkDeferred( |
| | 14 | 348 | | governanceEmissionError, |
| | 14 | 349 | | result.RetryAfterUtc ?? GetDeferredUtc(drainUtc), |
| | 14 | 350 | | drainUtc); |
| | | 351 | | |
| | 14 | 352 | | return await outboxStore.SaveAsync(deferredEntry, cancellationToken).ConfigureAwait(false); |
| | | 353 | | } |
| | | 354 | | |
| | 18 | 355 | | GovernanceEmissionError failure = result.Error ?? GovernanceEmissionError.Create( |
| | 18 | 356 | | "emission.failed", |
| | 18 | 357 | | "Governance emission returned a failed result without provider-neutral error details.", |
| | 18 | 358 | | isRetryable: result.ShouldRetry, |
| | 18 | 359 | | providerName: result.ProviderName); |
| | | 360 | | |
| | 18 | 361 | | return await ApplyFailureAsync( |
| | 18 | 362 | | entry, |
| | 18 | 363 | | failure, |
| | 18 | 364 | | result.RetryAfterUtc, |
| | 18 | 365 | | cancellationToken) |
| | 18 | 366 | | .ConfigureAwait(false); |
| | 56 | 367 | | } |
| | | 368 | | |
| | | 369 | | private async ValueTask<GovernanceOutboxEntry> ApplyEmissionResultAsync( |
| | | 370 | | IAsiBackboneGovernanceOutboxClaimStore claimStore, |
| | | 371 | | GovernanceOutboxClaim claim, |
| | | 372 | | GovernanceEmissionResult result, |
| | | 373 | | DateTimeOffset drainUtc, |
| | | 374 | | CancellationToken cancellationToken) |
| | | 375 | | { |
| | 30 | 376 | | ArgumentNullException.ThrowIfNull(result); |
| | | 377 | | |
| | 30 | 378 | | if (result.IsSuccess) |
| | | 379 | | { |
| | 10 | 380 | | return await claimStore.MarkClaimDeliveredAsync(claim, result, cancellationToken).ConfigureAwait(false); |
| | | 381 | | } |
| | | 382 | | |
| | 20 | 383 | | if (result.Status is GovernanceEmissionStatus.DeadLettered) |
| | | 384 | | { |
| | 4 | 385 | | GovernanceEmissionError governanceEmissionError = result.Error ?? GovernanceEmissionError.Create( |
| | 4 | 386 | | "emission.deadlettered", |
| | 4 | 387 | | "Governance emission returned a dead-lettered result.", |
| | 4 | 388 | | providerName: result.ProviderName); |
| | | 389 | | |
| | 4 | 390 | | return await claimStore.MarkClaimDeadLetteredAsync( |
| | 4 | 391 | | claim, |
| | 4 | 392 | | governanceEmissionError, |
| | 4 | 393 | | governanceEmissionError.Message, |
| | 4 | 394 | | cancellationToken) |
| | 4 | 395 | | .ConfigureAwait(false); |
| | | 396 | | } |
| | | 397 | | |
| | 16 | 398 | | if (result.Status is GovernanceEmissionStatus.Deferred or GovernanceEmissionStatus.Pending) |
| | | 399 | | { |
| | 8 | 400 | | GovernanceEmissionError? governanceEmissionError = result.Error ?? (result.Status is GovernanceEmissionStatu |
| | 8 | 401 | | ? GovernanceEmissionError.Create( |
| | 8 | 402 | | "emission.pending", |
| | 8 | 403 | | "Governance emission remained pending after the outbox drain attempt.", |
| | 8 | 404 | | isRetryable: true, |
| | 8 | 405 | | providerName: result.ProviderName) |
| | 8 | 406 | | : null); |
| | | 407 | | |
| | 8 | 408 | | GovernanceOutboxEntry deferredEntry = claim.Entry.MarkDeferred( |
| | 8 | 409 | | governanceEmissionError, |
| | 8 | 410 | | result.RetryAfterUtc ?? GetDeferredUtc(drainUtc), |
| | 8 | 411 | | drainUtc); |
| | | 412 | | |
| | 8 | 413 | | return await claimStore.SaveClaimAsync(claim, deferredEntry, cancellationToken).ConfigureAwait(false); |
| | | 414 | | } |
| | | 415 | | |
| | 8 | 416 | | GovernanceEmissionError failure = result.Error ?? GovernanceEmissionError.Create( |
| | 8 | 417 | | "emission.failed", |
| | 8 | 418 | | "Governance emission returned a failed result without provider-neutral error details.", |
| | 8 | 419 | | isRetryable: result.ShouldRetry, |
| | 8 | 420 | | providerName: result.ProviderName); |
| | | 421 | | |
| | 8 | 422 | | return await ApplyClaimFailureAsync( |
| | 8 | 423 | | claimStore, |
| | 8 | 424 | | claim, |
| | 8 | 425 | | failure, |
| | 8 | 426 | | result.RetryAfterUtc, |
| | 8 | 427 | | cancellationToken) |
| | 8 | 428 | | .ConfigureAwait(false); |
| | 30 | 429 | | } |
| | | 430 | | |
| | | 431 | | private async ValueTask<GovernanceOutboxEntry> ApplyFailureAsync( |
| | | 432 | | GovernanceOutboxEntry entry, |
| | | 433 | | GovernanceEmissionError failure, |
| | | 434 | | DateTimeOffset? nextRetryUtc, |
| | | 435 | | CancellationToken cancellationToken) |
| | | 436 | | { |
| | 25 | 437 | | if (ShouldDeadLetter(entry)) |
| | | 438 | | { |
| | 2 | 439 | | GovernanceEmissionError deadLetterError = CreateMaxRetryError(failure); |
| | 2 | 440 | | return await outboxStore.MarkDeadLetteredAsync( |
| | 2 | 441 | | entry.OutboxEntryId, |
| | 2 | 442 | | deadLetterError, |
| | 2 | 443 | | retryOptions.DeadLetterReasonMessage, |
| | 2 | 444 | | cancellationToken) |
| | 2 | 445 | | .ConfigureAwait(false); |
| | | 446 | | } |
| | | 447 | | |
| | 23 | 448 | | return await outboxStore.MarkFailedAsync( |
| | 23 | 449 | | entry.OutboxEntryId, |
| | 23 | 450 | | failure, |
| | 23 | 451 | | nextRetryUtc, |
| | 23 | 452 | | cancellationToken) |
| | 23 | 453 | | .ConfigureAwait(false); |
| | 25 | 454 | | } |
| | | 455 | | |
| | | 456 | | private async ValueTask<GovernanceOutboxEntry> ApplyClaimFailureAsync( |
| | | 457 | | IAsiBackboneGovernanceOutboxClaimStore claimStore, |
| | | 458 | | GovernanceOutboxClaim claim, |
| | | 459 | | GovernanceEmissionError failure, |
| | | 460 | | DateTimeOffset? nextRetryUtc, |
| | | 461 | | CancellationToken cancellationToken) |
| | | 462 | | { |
| | 12 | 463 | | if (ShouldDeadLetter(claim.Entry)) |
| | | 464 | | { |
| | 2 | 465 | | GovernanceEmissionError deadLetterError = CreateMaxRetryError(failure); |
| | 2 | 466 | | return await claimStore.MarkClaimDeadLetteredAsync( |
| | 2 | 467 | | claim, |
| | 2 | 468 | | deadLetterError, |
| | 2 | 469 | | retryOptions.DeadLetterReasonMessage, |
| | 2 | 470 | | cancellationToken) |
| | 2 | 471 | | .ConfigureAwait(false); |
| | | 472 | | } |
| | | 473 | | |
| | 10 | 474 | | return await claimStore.MarkClaimFailedAsync( |
| | 10 | 475 | | claim, |
| | 10 | 476 | | failure, |
| | 10 | 477 | | nextRetryUtc, |
| | 10 | 478 | | cancellationToken) |
| | 10 | 479 | | .ConfigureAwait(false); |
| | 12 | 480 | | } |
| | | 481 | | |
| | | 482 | | private bool ShouldDeadLetter(GovernanceOutboxEntry entry) |
| | | 483 | | { |
| | 37 | 484 | | return retryOptions.DeadLetterOnMaxRetryAttempts |
| | 37 | 485 | | && entry.RetryCount + 1 >= retryOptions.MaxRetryAttempts; |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | private GovernanceEmissionError CreateMaxRetryError(GovernanceEmissionError failure) |
| | | 489 | | { |
| | 4 | 490 | | return GovernanceEmissionError.Create( |
| | 4 | 491 | | retryOptions.DeadLetterReasonCode, |
| | 4 | 492 | | retryOptions.DeadLetterReasonMessage, |
| | 4 | 493 | | providerName: failure.ProviderName, |
| | 4 | 494 | | providerErrorCode: failure.Code); |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | private void LogEmissionException(GovernanceOutboxEntry entry, DateTimeOffset nextRetryUtc, Exception exception) |
| | | 498 | | { |
| | 11 | 499 | | LogGovernanceEmissionException( |
| | 11 | 500 | | logger, |
| | 11 | 501 | | entry.OutboxEntryId, |
| | 11 | 502 | | entry.RetryCount + 1, |
| | 11 | 503 | | ResolveEmitterProvider(entry), |
| | 11 | 504 | | nextRetryUtc, |
| | 11 | 505 | | entry.Envelope.CorrelationId, |
| | 11 | 506 | | entry.Envelope.AuditResidueId, |
| | 11 | 507 | | exception); |
| | 11 | 508 | | } |
| | | 509 | | |
| | | 510 | | private static GovernanceEmissionError CreateExceptionError(Exception exception) |
| | | 511 | | { |
| | 11 | 512 | | return GovernanceEmissionError.Create( |
| | 11 | 513 | | "emission.exception", |
| | 11 | 514 | | $"Governance emission threw {exception.GetType().Name} during outbox drain.", |
| | 11 | 515 | | isRetryable: true, |
| | 11 | 516 | | providerErrorCode: exception.GetType().FullName); |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | private DateTimeOffset GetRetryUtc(DateTimeOffset drainUtc) |
| | | 520 | | { |
| | 11 | 521 | | return drainUtc.Add(retryOptions.RetryDelay); |
| | | 522 | | } |
| | | 523 | | |
| | | 524 | | private DateTimeOffset GetDeferredUtc(DateTimeOffset drainUtc) |
| | | 525 | | { |
| | 12 | 526 | | return drainUtc.Add(retryOptions.DeferredDelay); |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | private static AsiBackboneGovernanceOutboxOptions ResolveOptions(IOptions<AsiBackboneGovernanceOutboxOptions>? optio |
| | | 530 | | { |
| | 112 | 531 | | AsiBackboneGovernanceOutboxOptions resolved = options?.Value ?? new AsiBackboneGovernanceOutboxOptions(); |
| | 112 | 532 | | resolved.Validate(); |
| | | 533 | | |
| | 108 | 534 | | return new AsiBackboneGovernanceOutboxOptions |
| | 108 | 535 | | { |
| | 108 | 536 | | RetryDelay = resolved.RetryDelay, |
| | 108 | 537 | | DeferredDelay = resolved.DeferredDelay, |
| | 108 | 538 | | MaxRetryAttempts = resolved.MaxRetryAttempts, |
| | 108 | 539 | | DeadLetterOnMaxRetryAttempts = resolved.DeadLetterOnMaxRetryAttempts, |
| | 108 | 540 | | DeadLetterReasonCode = resolved.DeadLetterReasonCode.Trim(), |
| | 108 | 541 | | DeadLetterReasonMessage = resolved.DeadLetterReasonMessage.Trim(), |
| | 108 | 542 | | UseClaimLeases = resolved.UseClaimLeases, |
| | 108 | 543 | | ClaimWorkerId = string.IsNullOrWhiteSpace(resolved.ClaimWorkerId) ? null : resolved.ClaimWorkerId.Trim(), |
| | 108 | 544 | | ClaimLeaseDuration = resolved.ClaimLeaseDuration |
| | 108 | 545 | | }; |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | private static string ResolveEmitterProvider(GovernanceOutboxEntry entry) |
| | | 549 | | { |
| | 11 | 550 | | return entry.Envelope.EmitterProvider ?? entry.ProviderName ?? "unspecified"; |
| | | 551 | | } |
| | | 552 | | } |