|
| 1 | + |
| 2 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 3 | + |
| 4 | +namespace Microsoft.Azure.SpaceFx; |
| 5 | + |
| 6 | +public partial class Core { |
| 7 | + public partial class Services { |
| 8 | + public class LivenessCheck : IHealthCheck { |
| 9 | + private readonly ILogger<LivenessCheck> _logger; |
| 10 | + private readonly IServiceProvider _serviceProvider; |
| 11 | + private readonly IHostApplicationLifetime _appLifetime; |
| 12 | + private readonly Services.ResourceUtilizationMonitor _resourceUtilizationMonitor; |
| 13 | + private readonly Services.MessageReceiver _messageReceiver; |
| 14 | + private readonly Services.HeartbeatService _heartbeatService; |
| 15 | + |
| 16 | + public LivenessCheck(ILogger<LivenessCheck> logger, IServiceProvider serviceProvider, IHostApplicationLifetime appLifetime, Services.MessageReceiver messageReceiver, Services.HeartbeatService heartbeatService, Services.ResourceUtilizationMonitor resourceUtilizationMonitor) { |
| 17 | + _logger = logger; |
| 18 | + _serviceProvider = serviceProvider; |
| 19 | + _appLifetime = appLifetime; |
| 20 | + _messageReceiver = messageReceiver; |
| 21 | + |
| 22 | + _resourceUtilizationMonitor = resourceUtilizationMonitor; |
| 23 | + _heartbeatService = heartbeatService; |
| 24 | + |
| 25 | + _logger.LogInformation("Services.{serviceName} Initialized.", nameof(LivenessCheck)); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { |
| 30 | + // return Task.FromResult(HealthCheckResult.Unhealthy("The check indicates an unhealthy status.")); |
| 31 | + |
| 32 | + using (var scope = _serviceProvider.CreateScope()) { |
| 33 | + List<string> unhealthyServices = new List<string>(); |
| 34 | + |
| 35 | + var monitorableServices = _serviceProvider.GetServices<IMonitorableService>().ToList(); |
| 36 | + |
| 37 | + // Scan all the services utilizing the IMonitorableService interface and check if they are healthy |
| 38 | + unhealthyServices.AddRange(monitorableServices.Where(service => !service.IsHealthy()).Select(service => service.GetType().Name)); |
| 39 | + |
| 40 | + if (!_heartbeatService.IsHealthy()) |
| 41 | + unhealthyServices.Add(_heartbeatService.GetType().Name); |
| 42 | + |
| 43 | + if (!_messageReceiver.IsHealthy()) |
| 44 | + unhealthyServices.Add(_messageReceiver.GetType().Name); |
| 45 | + |
| 46 | + if (!_resourceUtilizationMonitor.IsHealthy()) |
| 47 | + unhealthyServices.Add(_resourceUtilizationMonitor.GetType().Name); |
| 48 | + |
| 49 | + if (unhealthyServices.Any()) { |
| 50 | + string unhealthServicesOutput = string.Join(",", unhealthyServices); |
| 51 | + _logger.LogCritical($"Unhealthy services detected. Services reporting unhealthy: {unhealthServicesOutput}"); |
| 52 | + _logger.LogCritical("Triggering application stop."); |
| 53 | + _appLifetime.StopApplication(); |
| 54 | + // throw new RpcException(new Status(StatusCode.Unknown, $"Unhealthy services detected. Services reporting unhealthy: {unhealthServicesOutput}")); |
| 55 | + return Task.FromResult(HealthCheckResult.Unhealthy($"Unhealthy services detected. Services reporting unhealthy: {unhealthServicesOutput}")); |
| 56 | + } |
| 57 | + |
| 58 | + _logger.LogDebug("All services report healthy."); |
| 59 | + } |
| 60 | + |
| 61 | + return Task.FromResult(HealthCheckResult.Healthy("Health check passed. All services report healthy.")); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments