| | 1 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | 2 | | using Microsoft.Extensions.Hosting; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | |
|
| | 5 | | namespace Infrastructure.Service; |
| | 6 | |
|
| | 7 | | public class HealthCheckResilienceService : IHostedService { |
| | 8 | | private readonly ILogger<HealthCheckResilienceService>? _logger; |
| | 9 | | private readonly HealthCheckService _service; |
| | 10 | |
|
| 0 | 11 | | public HealthCheckResilienceService(ILogger<HealthCheckResilienceService>? logger, HealthCheckService service) { |
| 0 | 12 | | _logger = logger; |
| 0 | 13 | | _service = service; |
| 0 | 14 | | } |
| | 15 | |
|
| | 16 | | private const int MAX_RETRIES = 10; |
| | 17 | | private const int RETRY_DELAY = 5; |
| | 18 | |
|
| 0 | 19 | | public async Task WaitForHealthyStatusAsync(CancellationToken cancellationToken = default) { |
| 0 | 20 | | var isHealthy = false; |
| | 21 | |
|
| 0 | 22 | | for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) { |
| 0 | 23 | | try { |
| 0 | 24 | | var healthReport = await _service.CheckHealthAsync(cancellationToken); |
| | 25 | |
|
| 0 | 26 | | if (healthReport.Status == HealthStatus.Healthy) { |
| 0 | 27 | | _logger?.LogInformation("Health check passed on attempt {Attempt}.", attempt); |
| 0 | 28 | | isHealthy = true; |
| 0 | 29 | | break; |
| | 30 | | } |
| | 31 | |
|
| 0 | 32 | | _logger?.LogWarning( |
| 0 | 33 | | "Health check failed on attempt {Attempt}/{MaxRetries}. Retrying in {RetryDelaySeconds} seconds...", |
| 0 | 34 | | attempt, |
| 0 | 35 | | MAX_RETRIES, |
| 0 | 36 | | RETRY_DELAY |
| 0 | 37 | | ); |
| | 38 | |
|
| 0 | 39 | | foreach (var entry in healthReport.Entries) { |
| 0 | 40 | | _logger?.LogWarning( |
| 0 | 41 | | "Health check entry {Key}: {Status} - {Description}", |
| 0 | 42 | | entry.Key, |
| 0 | 43 | | entry.Value.Status, |
| 0 | 44 | | entry.Value.Description |
| 0 | 45 | | ); |
| 0 | 46 | | } |
| 0 | 47 | | } |
| 0 | 48 | | catch (Exception ex) { |
| 0 | 49 | | _logger?.LogError( |
| 0 | 50 | | ex, |
| 0 | 51 | | "An exception occurred during health check attempt {Attempt}/{MaxRetries}.", |
| 0 | 52 | | attempt, |
| 0 | 53 | | MAX_RETRIES |
| 0 | 54 | | ); |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | for (int remainingSeconds = RETRY_DELAY; remainingSeconds > 0; remainingSeconds--) { |
| 0 | 58 | | _logger?.LogInformation("Retrying in {RemainingSeconds} second(s)...", remainingSeconds); |
| 0 | 59 | | await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); |
| 0 | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | if (!isHealthy) { |
| 0 | 64 | | _logger?.LogError("-------------------------------------------------------------------------------"); |
| 0 | 65 | | _logger?.LogError("Health check failed after {MaxRetries:D2} attempts. Application starting as not operational!", |
| 0 | 66 | | _logger?.LogError("-------------------------------------------------------------------------------"); |
| 0 | 67 | | } |
| 0 | 68 | | ; |
| 0 | 69 | | } |
| | 70 | |
|
| 0 | 71 | | public async Task RunManuallyAsync(CancellationToken cancellationToken = default) { |
| 0 | 72 | | await StartAsync(cancellationToken); |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | public async Task StartAsync(CancellationToken cancellationToken) { |
| 0 | 76 | | await WaitForHealthyStatusAsync(cancellationToken); |
| 0 | 77 | | } |
| | 78 | |
|
| 0 | 79 | | public Task StopAsync(CancellationToken cancellationToken) { |
| 0 | 80 | | throw new NotImplementedException(); |
| | 81 | | } |
| | 82 | | } |