| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 5 | | |
| | | 6 | | namespace ProjectTemplate.Infrastructure.Data.Extensions; |
| | | 7 | | |
| | | 8 | | public static class ApplicationAuditReconciliationServiceExtensions |
| | | 9 | | { |
| | | 10 | | public static IServiceCollection AddApplicationAuditReconciliationCore( |
| | | 11 | | this IServiceCollection services, |
| | | 12 | | Action<ApplicationAuditReconciliationOptions>? configure = null) |
| | | 13 | | { |
| | 0 | 14 | | ArgumentNullException.ThrowIfNull(services); |
| | | 15 | | |
| | 0 | 16 | | OptionsBuilder<ApplicationAuditReconciliationOptions> optionsBuilder = services |
| | 0 | 17 | | .AddOptions<ApplicationAuditReconciliationOptions>() |
| | 0 | 18 | | .Validate(options => options.Interval > TimeSpan.Zero, |
| | 0 | 19 | | "The audit reconciliation interval must be greater than zero.") |
| | 0 | 20 | | .Validate(options => options.CompletionGracePeriod >= TimeSpan.Zero, |
| | 0 | 21 | | "The completion grace period must not be negative.") |
| | 0 | 22 | | .Validate(options => options.StalePendingThreshold > TimeSpan.Zero, |
| | 0 | 23 | | "The stale pending threshold must be greater than zero.") |
| | 0 | 24 | | .Validate(options => options.StaleRetryReadyThreshold > TimeSpan.Zero, |
| | 0 | 25 | | "The stale retry-ready threshold must be greater than zero.") |
| | 0 | 26 | | .Validate(options => options.MaximumBatchesPerRun is > 0 and <= 10_000, |
| | 0 | 27 | | "Maximum batches per reconciliation run must be between 1 and 10000.") |
| | 0 | 28 | | .Validate(options => options.HealthWarningFindingCount >= 0, |
| | 0 | 29 | | "The warning finding threshold must not be negative.") |
| | 0 | 30 | | .Validate(options => options.HealthUnhealthyFindingCount >= options.HealthWarningFindingCount, |
| | 0 | 31 | | "The unhealthy finding threshold must not be less than the warning threshold.") |
| | 0 | 32 | | .ValidateOnStart(); |
| | | 33 | | |
| | 0 | 34 | | if (configure is not null) |
| | | 35 | | { |
| | 0 | 36 | | optionsBuilder.Configure(configure); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | services.TryAddSingleton(TimeProvider.System); |
| | 0 | 40 | | services.TryAddSingleton<ApplicationAuditReconciliationMetrics>(); |
| | 0 | 41 | | services.TryAddScoped<ApplicationAuditReconciler>(); |
| | 0 | 42 | | services.TryAddScoped<IApplicationAuditReconciler>(serviceProvider => |
| | 0 | 43 | | serviceProvider.GetRequiredService<ApplicationAuditReconciler>()); |
| | 0 | 44 | | return services; |
| | | 45 | | } |
| | | 46 | | } |