| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Services; |
| | | 5 | | |
| | 4 | 6 | | public sealed partial class ApplicationAuditReconciliationHostedService( |
| | 4 | 7 | | IServiceScopeFactory scopeFactory, |
| | 4 | 8 | | IOptions<ApplicationAuditReconciliationOptions> options, |
| | 4 | 9 | | TimeProvider timeProvider, |
| | 4 | 10 | | ILogger<ApplicationAuditReconciliationHostedService> logger) |
| | | 11 | | : BackgroundService |
| | | 12 | | { |
| | 5 | 13 | | private readonly IServiceScopeFactory _scopeFactory = |
| | 5 | 14 | | scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); |
| | 5 | 15 | | private readonly ApplicationAuditReconciliationOptions _options = |
| | 5 | 16 | | options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 4 | 17 | | private readonly TimeProvider _timeProvider = |
| | 4 | 18 | | timeProvider ?? throw new ArgumentNullException(nameof(timeProvider)); |
| | 4 | 19 | | private readonly ILogger<ApplicationAuditReconciliationHostedService> _logger = |
| | 4 | 20 | | logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | 21 | | |
| | | 22 | | [LoggerMessage( |
| | | 23 | | EventId = 19110, |
| | | 24 | | Level = LogLevel.Error, |
| | | 25 | | Message = "The audit reconciliation cycle failed and will be retried. ConsecutiveFailureCount: {ConsecutiveFailu |
| | | 26 | | private static partial void LogReconciliationFailure( |
| | | 27 | | ILogger logger, |
| | | 28 | | Exception exception, |
| | | 29 | | int consecutiveFailureCount, |
| | | 30 | | double retryDelaySeconds); |
| | | 31 | | |
| | | 32 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 33 | | { |
| | 2 | 34 | | if (!_options.Enabled || !_options.RunWorker) |
| | | 35 | | { |
| | 0 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 2 | 39 | | int consecutiveFailureCount = 0; |
| | | 40 | | |
| | 4 | 41 | | while (!stoppingToken.IsCancellationRequested) |
| | | 42 | | { |
| | 4 | 43 | | Exception? cycleFailure = null; |
| | | 44 | | |
| | | 45 | | try |
| | | 46 | | { |
| | 4 | 47 | | using IServiceScope scope = _scopeFactory.CreateScope(); |
| | 4 | 48 | | IApplicationAuditReconciler reconciler = scope.ServiceProvider |
| | 4 | 49 | | .GetRequiredService<IApplicationAuditReconciler>(); |
| | 4 | 50 | | _ = await reconciler.ReconcileAsync(stoppingToken).ConfigureAwait(false); |
| | | 51 | | |
| | 2 | 52 | | IApplicationAuditCompletionOutboxQuery? outboxQuery = scope.ServiceProvider |
| | 2 | 53 | | .GetService<IApplicationAuditCompletionOutboxQuery>(); |
| | 2 | 54 | | if (outboxQuery is not null) |
| | | 55 | | { |
| | 2 | 56 | | ApplicationAuditCompletionOutboxHealth deliveryHealth = await outboxQuery |
| | 2 | 57 | | .GetHealthAsync(stoppingToken) |
| | 2 | 58 | | .ConfigureAwait(false); |
| | 1 | 59 | | scope.ServiceProvider |
| | 1 | 60 | | .GetRequiredService<ApplicationAuditReconciliationMetrics>() |
| | 1 | 61 | | .UpdateDelivery(deliveryHealth); |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | consecutiveFailureCount = 0; |
| | 1 | 65 | | } |
| | 2 | 66 | | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) |
| | | 67 | | { |
| | 2 | 68 | | break; |
| | | 69 | | } |
| | 1 | 70 | | catch (Exception exception) |
| | | 71 | | { |
| | 1 | 72 | | cycleFailure = exception; |
| | 1 | 73 | | consecutiveFailureCount++; |
| | 1 | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | TimeSpan delay = BackgroundServiceRetryDelay.Calculate( |
| | 2 | 77 | | _options.Interval, |
| | 2 | 78 | | _options.MaximumCycleRetryDelay, |
| | 2 | 79 | | consecutiveFailureCount); |
| | | 80 | | |
| | 2 | 81 | | if (cycleFailure is not null) |
| | | 82 | | { |
| | 1 | 83 | | LogReconciliationFailure( |
| | 1 | 84 | | _logger, |
| | 1 | 85 | | cycleFailure, |
| | 1 | 86 | | consecutiveFailureCount, |
| | 1 | 87 | | delay.TotalSeconds); |
| | | 88 | | } |
| | | 89 | | |
| | 2 | 90 | | await Task.Delay(delay, _timeProvider, stoppingToken).ConfigureAwait(false); |
| | 2 | 91 | | } |
| | 2 | 92 | | } |
| | | 93 | | } |