| | | 1 | | using AsiBackbone.Core.Emissions; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Outbox; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Drains provider-neutral governance outbox entries through a configured governance emitter. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// This drain path is provider-neutral. It is suitable for tests, samples, local validation, and host-owned workers tha |
| | | 10 | | /// </remarks> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Initializes a new instance of the <see cref="AsiBackboneGovernanceOutboxDrain" /> class. |
| | | 13 | | /// </remarks> |
| | | 14 | | /// <param name="outboxStore">The provider-neutral outbox store.</param> |
| | | 15 | | /// <param name="emitter">The provider-neutral governance emitter.</param> |
| | 24 | 16 | | public sealed class AsiBackboneGovernanceOutboxDrain( |
| | 24 | 17 | | IAsiBackboneGovernanceOutboxStore outboxStore, |
| | 24 | 18 | | IAsiBackboneGovernanceEmitter emitter) |
| | | 19 | | { |
| | 26 | 20 | | private readonly IAsiBackboneGovernanceOutboxStore outboxStore = outboxStore ?? throw new ArgumentNullException(name |
| | 25 | 21 | | private readonly IAsiBackboneGovernanceEmitter emitter = emitter ?? throw new ArgumentNullException(nameof(emitter)) |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Drains pending and retry-ready outbox entries through the configured emitter. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="utcNow">The UTC timestamp used for retry-ready checks.</param> |
| | | 27 | | /// <param name="maxCount">The maximum number of entries to drain.</param> |
| | | 28 | | /// <param name="cancellationToken">A cancellation token.</param> |
| | | 29 | | /// <returns>The updated outbox entries that were attempted by the drain.</returns> |
| | | 30 | | public async ValueTask<IReadOnlyList<GovernanceOutboxEntry>> DrainAsync( |
| | | 31 | | DateTimeOffset? utcNow = null, |
| | | 32 | | int maxCount = 100, |
| | | 33 | | CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 24 | 35 | | if (maxCount <= 0) |
| | | 36 | | { |
| | 1 | 37 | | throw new ArgumentOutOfRangeException(nameof(maxCount), maxCount, "Maximum count must be greater than zero." |
| | | 38 | | } |
| | | 39 | | |
| | 23 | 40 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 41 | | |
| | 23 | 42 | | DateTimeOffset drainUtc = (utcNow ?? DateTimeOffset.UtcNow).ToUniversalTime(); |
| | 23 | 43 | | IReadOnlyList<GovernanceOutboxEntry> pendingEntries = await outboxStore |
| | 23 | 44 | | .FindPendingAsync(maxCount, cancellationToken) |
| | 23 | 45 | | .ConfigureAwait(false); |
| | | 46 | | |
| | 23 | 47 | | List<GovernanceOutboxEntry> entriesToDrain = [.. pendingEntries]; |
| | | 48 | | |
| | 23 | 49 | | if (entriesToDrain.Count < maxCount) |
| | | 50 | | { |
| | 12 | 51 | | IReadOnlyList<GovernanceOutboxEntry> retryReadyEntries = await outboxStore |
| | 12 | 52 | | .FindRetryReadyAsync(drainUtc, maxCount - entriesToDrain.Count, cancellationToken) |
| | 12 | 53 | | .ConfigureAwait(false); |
| | | 54 | | |
| | 12 | 55 | | HashSet<string> existingEntryIds = new( |
| | 10 | 56 | | entriesToDrain.Select(entry => entry.OutboxEntryId), |
| | 12 | 57 | | StringComparer.Ordinal); |
| | | 58 | | |
| | 28 | 59 | | foreach (GovernanceOutboxEntry retryReadyEntry in retryReadyEntries) |
| | | 60 | | { |
| | 2 | 61 | | if (existingEntryIds.Add(retryReadyEntry.OutboxEntryId)) |
| | | 62 | | { |
| | 1 | 63 | | entriesToDrain.Add(retryReadyEntry); |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 23 | 68 | | List<GovernanceOutboxEntry> updatedEntries = new(entriesToDrain.Count); |
| | | 69 | | |
| | 101 | 70 | | foreach (GovernanceOutboxEntry entry in entriesToDrain) |
| | | 71 | | { |
| | 29 | 72 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 29 | 73 | | GovernanceOutboxEntry updatedEntry = await DrainEntryAsync(entry, drainUtc, cancellationToken).ConfigureAwai |
| | 26 | 74 | | updatedEntries.Add(updatedEntry); |
| | | 75 | | } |
| | | 76 | | |
| | 20 | 77 | | return updatedEntries; |
| | 20 | 78 | | } |
| | | 79 | | |
| | | 80 | | private async ValueTask<GovernanceOutboxEntry> DrainEntryAsync( |
| | | 81 | | GovernanceOutboxEntry entry, |
| | | 82 | | DateTimeOffset drainUtc, |
| | | 83 | | CancellationToken cancellationToken) |
| | | 84 | | { |
| | | 85 | | GovernanceEmissionResult result; |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 29 | 89 | | result = await emitter.EmitAsync(entry.Envelope, cancellationToken).ConfigureAwait(false); |
| | 25 | 90 | | } |
| | 1 | 91 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 92 | | { |
| | 1 | 93 | | throw; |
| | | 94 | | } |
| | 3 | 95 | | catch (Exception ex) |
| | | 96 | | { |
| | 3 | 97 | | var governanceEmissionError = GovernanceEmissionError.Create( |
| | 3 | 98 | | "emission.exception", |
| | 3 | 99 | | $"Governance emission threw {ex.GetType().Name} during outbox drain.", |
| | 3 | 100 | | isRetryable: true, |
| | 3 | 101 | | providerErrorCode: ex.GetType().FullName); |
| | | 102 | | |
| | 3 | 103 | | return await outboxStore.MarkFailedAsync( |
| | 3 | 104 | | entry.OutboxEntryId, |
| | 3 | 105 | | governanceEmissionError, |
| | 3 | 106 | | nextRetryUtc: drainUtc.AddMinutes(1), |
| | 3 | 107 | | cancellationToken) |
| | 3 | 108 | | .ConfigureAwait(false); |
| | | 109 | | } |
| | | 110 | | |
| | 25 | 111 | | return await ApplyEmissionResultAsync(entry, result, drainUtc, cancellationToken).ConfigureAwait(false); |
| | 26 | 112 | | } |
| | | 113 | | |
| | | 114 | | private async ValueTask<GovernanceOutboxEntry> ApplyEmissionResultAsync( |
| | | 115 | | GovernanceOutboxEntry entry, |
| | | 116 | | GovernanceEmissionResult result, |
| | | 117 | | DateTimeOffset drainUtc, |
| | | 118 | | CancellationToken cancellationToken) |
| | | 119 | | { |
| | 25 | 120 | | ArgumentNullException.ThrowIfNull(result); |
| | | 121 | | |
| | 25 | 122 | | if (result.IsSuccess) |
| | | 123 | | { |
| | 13 | 124 | | return await outboxStore.MarkDeliveredAsync( |
| | 13 | 125 | | entry.OutboxEntryId, |
| | 13 | 126 | | result, |
| | 13 | 127 | | cancellationToken) |
| | 13 | 128 | | .ConfigureAwait(false); |
| | | 129 | | } |
| | | 130 | | |
| | 12 | 131 | | if (result.Status is GovernanceEmissionStatus.DeadLettered) |
| | | 132 | | { |
| | 1 | 133 | | GovernanceEmissionError governanceEmissionError = result.Error ?? GovernanceEmissionError.Create( |
| | 1 | 134 | | "emission.deadlettered", |
| | 1 | 135 | | "Governance emission returned a dead-lettered result.", |
| | 1 | 136 | | providerName: result.ProviderName); |
| | | 137 | | |
| | 1 | 138 | | return await outboxStore.MarkDeadLetteredAsync( |
| | 1 | 139 | | entry.OutboxEntryId, |
| | 1 | 140 | | governanceEmissionError, |
| | 1 | 141 | | governanceEmissionError.Message, |
| | 1 | 142 | | cancellationToken) |
| | 1 | 143 | | .ConfigureAwait(false); |
| | | 144 | | } |
| | | 145 | | |
| | 11 | 146 | | if (result.Status is GovernanceEmissionStatus.Deferred or GovernanceEmissionStatus.Pending) |
| | | 147 | | { |
| | 2 | 148 | | GovernanceEmissionError? governanceEmissionError = result.Error ?? (result.Status is GovernanceEmissionStatu |
| | 2 | 149 | | ? GovernanceEmissionError.Create( |
| | 2 | 150 | | "emission.pending", |
| | 2 | 151 | | "Governance emission remained pending after the outbox drain attempt.", |
| | 2 | 152 | | isRetryable: true, |
| | 2 | 153 | | providerName: result.ProviderName) |
| | 2 | 154 | | : null); |
| | | 155 | | |
| | 2 | 156 | | GovernanceOutboxEntry deferredEntry = entry.MarkDeferred( |
| | 2 | 157 | | governanceEmissionError, |
| | 2 | 158 | | result.RetryAfterUtc ?? drainUtc.AddMinutes(1), |
| | 2 | 159 | | drainUtc); |
| | | 160 | | |
| | 2 | 161 | | return await outboxStore.SaveAsync(deferredEntry, cancellationToken).ConfigureAwait(false); |
| | | 162 | | } |
| | | 163 | | |
| | 9 | 164 | | GovernanceEmissionError failure = result.Error ?? GovernanceEmissionError.Create( |
| | 9 | 165 | | "emission.failed", |
| | 9 | 166 | | "Governance emission returned a failed result without provider-neutral error details.", |
| | 9 | 167 | | isRetryable: result.ShouldRetry, |
| | 9 | 168 | | providerName: result.ProviderName); |
| | | 169 | | |
| | 9 | 170 | | return await outboxStore.MarkFailedAsync( |
| | 9 | 171 | | entry.OutboxEntryId, |
| | 9 | 172 | | failure, |
| | 9 | 173 | | result.RetryAfterUtc, |
| | 9 | 174 | | cancellationToken) |
| | 9 | 175 | | .ConfigureAwait(false); |
| | 23 | 176 | | } |
| | | 177 | | } |