| | | 1 | | using AsiBackbone.Core.Outbox; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.AspNetCore.Outbox; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Runs the provider-neutral governance outbox drain from an ASP.NET Core or generic-host background worker. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Hosting remains outside Core. The worker resolves the drain through a scoped service provider so durable providers t |
| | | 14 | | /// </remarks> |
| | 10 | 15 | | public sealed class AsiBackboneGovernanceOutboxDrainHostedService( |
| | 10 | 16 | | IServiceScopeFactory scopeFactory, |
| | 10 | 17 | | IOptionsMonitor<AsiBackboneGovernanceOutboxDrainWorkerOptions> optionsMonitor, |
| | 10 | 18 | | ILogger<AsiBackboneGovernanceOutboxDrainHostedService> logger) : BackgroundService |
| | | 19 | | { |
| | 2 | 20 | | private static readonly Action<ILogger, Exception?> LogShutdownDrainCanceled = LoggerMessage.Define( |
| | 2 | 21 | | LogLevel.Debug, |
| | 2 | 22 | | new EventId(19801, nameof(LogShutdownDrainCanceled)), |
| | 2 | 23 | | "Governance outbox shutdown drain was canceled."); |
| | | 24 | | |
| | 2 | 25 | | private static readonly Action<ILogger, Exception?> LogShutdownDrainFailed = LoggerMessage.Define( |
| | 2 | 26 | | LogLevel.Warning, |
| | 2 | 27 | | new EventId(19802, nameof(LogShutdownDrainFailed)), |
| | 2 | 28 | | "Governance outbox shutdown drain failed."); |
| | | 29 | | |
| | 2 | 30 | | private static readonly Action<ILogger, Exception?> LogWorkerDisabled = LoggerMessage.Define( |
| | 2 | 31 | | LogLevel.Debug, |
| | 2 | 32 | | new EventId(19803, nameof(LogWorkerDisabled)), |
| | 2 | 33 | | "Governance outbox drain worker is disabled."); |
| | | 34 | | |
| | 2 | 35 | | private static readonly Action<ILogger, int, Exception?> LogDrainAttempted = LoggerMessage.Define<int>( |
| | 2 | 36 | | LogLevel.Debug, |
| | 2 | 37 | | new EventId(19804, nameof(LogDrainAttempted)), |
| | 2 | 38 | | "Governance outbox drain attempted {DrainedCount} entries."); |
| | | 39 | | |
| | 2 | 40 | | private static readonly Action<ILogger, Exception?> LogWorkerFailed = LoggerMessage.Define( |
| | 2 | 41 | | LogLevel.Warning, |
| | 2 | 42 | | new EventId(19805, nameof(LogWorkerFailed)), |
| | 2 | 43 | | "Governance outbox drain worker failed before the next polling interval."); |
| | | 44 | | |
| | 10 | 45 | | private readonly IServiceScopeFactory scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFac |
| | 10 | 46 | | private readonly IOptionsMonitor<AsiBackboneGovernanceOutboxDrainWorkerOptions> optionsMonitor = optionsMonitor ?? t |
| | 10 | 47 | | private readonly ILogger<AsiBackboneGovernanceOutboxDrainHostedService> logger = logger ?? throw new ArgumentNullExc |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public override async Task StopAsync(CancellationToken cancellationToken) |
| | | 51 | | { |
| | 8 | 52 | | await base.StopAsync(cancellationToken).ConfigureAwait(false); |
| | | 53 | | |
| | 8 | 54 | | AsiBackboneGovernanceOutboxDrainWorkerOptions options = optionsMonitor.CurrentValue; |
| | | 55 | | |
| | 8 | 56 | | if (options.Enabled && options.DrainOnShutdown && !cancellationToken.IsCancellationRequested) |
| | | 57 | | { |
| | 2 | 58 | | using var shutdownDrainCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 2 | 59 | | shutdownDrainCancellation.CancelAfter(options.ShutdownDrainTimeout); |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 2 | 63 | | _ = await DrainOnceAsync(options, shutdownDrainCancellation.Token).ConfigureAwait(false); |
| | 2 | 64 | | } |
| | 0 | 65 | | catch (OperationCanceledException) when (shutdownDrainCancellation.IsCancellationRequested) |
| | | 66 | | { |
| | 0 | 67 | | LogShutdownDrainCanceled(logger, null); |
| | 0 | 68 | | } |
| | 0 | 69 | | catch (Exception ex) |
| | | 70 | | { |
| | 0 | 71 | | LogShutdownDrainFailed(logger, ex); |
| | 0 | 72 | | } |
| | 2 | 73 | | } |
| | 8 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 78 | | { |
| | 8 | 79 | | AsiBackboneGovernanceOutboxDrainWorkerOptions startupOptions = optionsMonitor.CurrentValue; |
| | | 80 | | |
| | 8 | 81 | | if (!startupOptions.Enabled) |
| | | 82 | | { |
| | 2 | 83 | | LogWorkerDisabled(logger, null); |
| | 2 | 84 | | return; |
| | | 85 | | } |
| | | 86 | | |
| | 12 | 87 | | while (!stoppingToken.IsCancellationRequested) |
| | | 88 | | { |
| | 6 | 89 | | AsiBackboneGovernanceOutboxDrainWorkerOptions options = optionsMonitor.CurrentValue; |
| | | 90 | | |
| | 6 | 91 | | if (!options.Enabled) |
| | | 92 | | { |
| | 0 | 93 | | await DelayAsync(options.PollingInterval, stoppingToken).ConfigureAwait(false); |
| | 0 | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | try |
| | | 98 | | { |
| | 6 | 99 | | int drainedCount = await DrainOnceAsync(options, stoppingToken).ConfigureAwait(false); |
| | 6 | 100 | | LogDrainAttempted(logger, drainedCount, null); |
| | 6 | 101 | | await DelayAsync(options.PollingInterval, stoppingToken).ConfigureAwait(false); |
| | 6 | 102 | | } |
| | 0 | 103 | | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) |
| | | 104 | | { |
| | 0 | 105 | | break; |
| | | 106 | | } |
| | 0 | 107 | | catch (Exception ex) |
| | | 108 | | { |
| | 0 | 109 | | LogWorkerFailed(logger, ex); |
| | 0 | 110 | | await DelayAsync(options.FailureDelay, stoppingToken).ConfigureAwait(false); |
| | | 111 | | } |
| | 6 | 112 | | } |
| | 8 | 113 | | } |
| | | 114 | | |
| | | 115 | | private async ValueTask<int> DrainOnceAsync( |
| | | 116 | | AsiBackboneGovernanceOutboxDrainWorkerOptions options, |
| | | 117 | | CancellationToken cancellationToken) |
| | | 118 | | { |
| | 8 | 119 | | options.Validate(); |
| | 8 | 120 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 121 | | |
| | 8 | 122 | | using IServiceScope scope = scopeFactory.CreateScope(); |
| | 8 | 123 | | AsiBackboneGovernanceOutboxDrain drain = scope.ServiceProvider.GetRequiredService<AsiBackboneGovernanceOutboxDra |
| | 8 | 124 | | DateTimeOffset retryUtc = options.RetryClock().ToUniversalTime(); |
| | 8 | 125 | | IReadOnlyList<GovernanceOutboxEntry> drainedEntries = await drain.DrainAsync( |
| | 8 | 126 | | retryUtc, |
| | 8 | 127 | | options.BatchSize, |
| | 8 | 128 | | cancellationToken) |
| | 8 | 129 | | .ConfigureAwait(false); |
| | | 130 | | |
| | 8 | 131 | | return drainedEntries.Count; |
| | 8 | 132 | | } |
| | | 133 | | |
| | | 134 | | private static async ValueTask DelayAsync(TimeSpan delay, CancellationToken cancellationToken) |
| | | 135 | | { |
| | | 136 | | try |
| | | 137 | | { |
| | 6 | 138 | | await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| | 0 | 139 | | } |
| | 6 | 140 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 141 | | { |
| | | 142 | | // Host shutdown cancels the delay. The outer loop handles termination. |
| | 6 | 143 | | } |
| | 6 | 144 | | } |
| | | 145 | | } |