| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Dispatches durable audit-completion entries after their originating transaction commits. |
| | | 8 | | /// </summary> |
| | 3 | 9 | | public sealed partial class ApplicationAuditCompletionOutboxHostedService( |
| | 3 | 10 | | IServiceScopeFactory scopeFactory, |
| | 3 | 11 | | IOptions<ApplicationAuditCompletionOutboxOptions> options, |
| | 3 | 12 | | TimeProvider timeProvider, |
| | 3 | 13 | | ILogger<ApplicationAuditCompletionOutboxHostedService> logger) |
| | | 14 | | : BackgroundService |
| | | 15 | | { |
| | 4 | 16 | | private readonly IServiceScopeFactory _scopeFactory = |
| | 4 | 17 | | scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); |
| | 3 | 18 | | private readonly ApplicationAuditCompletionOutboxOptions _options = |
| | 3 | 19 | | options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 3 | 20 | | private readonly TimeProvider _timeProvider = |
| | 3 | 21 | | timeProvider ?? throw new ArgumentNullException(nameof(timeProvider)); |
| | 3 | 22 | | private readonly ILogger<ApplicationAuditCompletionOutboxHostedService> _logger = |
| | 3 | 23 | | logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | 24 | | |
| | | 25 | | [LoggerMessage( |
| | | 26 | | EventId = 19100, |
| | | 27 | | Level = LogLevel.Error, |
| | | 28 | | Message = "The audit-completion outbox dispatch cycle failed and will be retried. ConsecutiveFailureCount: {Cons |
| | | 29 | | private static partial void LogDispatchCycleFailure( |
| | | 30 | | ILogger logger, |
| | | 31 | | Exception exception, |
| | | 32 | | int consecutiveFailureCount, |
| | | 33 | | double retryDelaySeconds); |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 37 | | { |
| | 2 | 38 | | if (!_options.Enabled) |
| | | 39 | | { |
| | 0 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 2 | 43 | | int consecutiveFailureCount = 0; |
| | | 44 | | |
| | 3 | 45 | | while (!stoppingToken.IsCancellationRequested) |
| | | 46 | | { |
| | 3 | 47 | | Exception? cycleFailure = null; |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 3 | 51 | | using IServiceScope scope = _scopeFactory.CreateScope(); |
| | 3 | 52 | | IApplicationAuditCompletionOutboxDispatcher dispatcher = scope.ServiceProvider |
| | 3 | 53 | | .GetRequiredService<IApplicationAuditCompletionOutboxDispatcher>(); |
| | 3 | 54 | | _ = await dispatcher.DispatchReadyAsync(stoppingToken).ConfigureAwait(false); |
| | 0 | 55 | | consecutiveFailureCount = 0; |
| | 0 | 56 | | } |
| | 2 | 57 | | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) |
| | | 58 | | { |
| | 2 | 59 | | break; |
| | | 60 | | } |
| | 1 | 61 | | catch (Exception exception) |
| | | 62 | | { |
| | 1 | 63 | | cycleFailure = exception; |
| | 1 | 64 | | consecutiveFailureCount++; |
| | 1 | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | TimeSpan delay = BackgroundServiceRetryDelay.Calculate( |
| | 1 | 68 | | _options.PollInterval, |
| | 1 | 69 | | _options.MaximumCycleRetryDelay, |
| | 1 | 70 | | consecutiveFailureCount); |
| | | 71 | | |
| | 1 | 72 | | if (cycleFailure is not null) |
| | | 73 | | { |
| | 1 | 74 | | LogDispatchCycleFailure( |
| | 1 | 75 | | _logger, |
| | 1 | 76 | | cycleFailure, |
| | 1 | 77 | | consecutiveFailureCount, |
| | 1 | 78 | | delay.TotalSeconds); |
| | | 79 | | } |
| | | 80 | | |
| | 1 | 81 | | await Task.Delay(delay, _timeProvider, stoppingToken).ConfigureAwait(false); |
| | 1 | 82 | | } |
| | 2 | 83 | | } |
| | | 84 | | } |