Skip to content

Commit b40baa7

Browse files
committed
fixing health checks
Signed-off-by: Neil South <[email protected]>
1 parent 83dcd66 commit b40baa7

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/InformaticsGateway/Logging/Log.0.General.cs

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,11 @@ public static partial class Log
5959

6060
[LoggerMessage(EventId = 13, Level = LogLevel.Critical, Message = "Failed to start {ServiceName}.")]
6161
public static partial void ServiceFailedToStart(this ILogger logger, string serviceName, Exception ex);
62+
63+
[LoggerMessage(EventId = 14, Level = LogLevel.Critical, Message = "All services are unhealthy")]
64+
public static partial void AllServiceUnheathly(this ILogger logger);
65+
66+
[LoggerMessage(EventId = 15, Level = LogLevel.Error, Message = "Some services are unhealthy {list}")]
67+
public static partial void SomeServiceUnheathly(this ILogger logger, string list);
6268
}
6369
}

src/InformaticsGateway/Repositories/MonaiServiceLocator.cs

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
using System.Linq;
2020
using System.Reflection;
2121
using Ardalis.GuardClauses;
22+
using Microsoft.Extensions.DependencyInjection;
23+
using Microsoft.Extensions.Hosting;
2224
using Monai.Deploy.InformaticsGateway.Api.Rest;
2325
using Monai.Deploy.InformaticsGateway.Services.Common;
26+
using Monai.Deploy.InformaticsGateway.Services.Connectors;
2427

2528
namespace Monai.Deploy.InformaticsGateway.Repositories
2629
{
@@ -51,7 +54,16 @@ public Dictionary<string, ServiceStatus> GetServiceStatus()
5154
{
5255
Guard.Against.Null(type, nameof(type));
5356

54-
return (_serviceProvider.GetService(type) as IMonaiService);
57+
var hostedServices = _serviceProvider.GetServices<IHostedService>().ToList();
58+
var t = (hostedServices[2].GetType()).Name;
59+
60+
var TypeInterface = type.GetInterfaces().FirstOrDefault(i => i.Name == $"I{type.Name}");
61+
if (TypeInterface is null)
62+
{
63+
var te = _serviceProvider.GetServices<IHostedService>().FirstOrDefault(i => i.GetType().Name == type.Name);
64+
return te as IMonaiService;
65+
}
66+
return (_serviceProvider.GetService(TypeInterface) as IMonaiService);
5567

5668
}
5769

src/InformaticsGateway/Services/Connectors/PayloadAssembler.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
using Microsoft.Extensions.DependencyInjection;
2626
using Microsoft.Extensions.Logging;
2727
using Monai.Deploy.InformaticsGateway.Api;
28+
using Monai.Deploy.InformaticsGateway.Api.Rest;
2829
using Monai.Deploy.InformaticsGateway.Api.Storage;
2930
using Monai.Deploy.InformaticsGateway.Database.Api.Repositories;
3031
using Monai.Deploy.InformaticsGateway.Logging;
32+
using Monai.Deploy.InformaticsGateway.Services.Common;
3133
using Monai.Deploy.Messaging.Events;
3234

3335
#nullable enable
@@ -38,7 +40,7 @@ namespace Monai.Deploy.InformaticsGateway.Services.Connectors
3840
/// An in-memory queue for providing any files/DICOM instances received by the Informatics Gateway to
3941
/// other internal services.
4042
/// </summary>
41-
internal sealed partial class PayloadAssembler : IPayloadAssembler, IDisposable
43+
internal sealed partial class PayloadAssembler : IPayloadAssembler, IDisposable, IMonaiService
4244
{
4345
internal const int DEFAULT_TIMEOUT = 5;
4446
private readonly ILogger<PayloadAssembler> _logger;
@@ -49,13 +51,16 @@ internal sealed partial class PayloadAssembler : IPayloadAssembler, IDisposable
4951
private readonly Task _intializedTask;
5052
private readonly BlockingCollection<Payload> _workItems;
5153
private readonly System.Timers.Timer _timer;
54+
private readonly IPayloadRepository _repository;
5255

5356
public PayloadAssembler(
5457
ILogger<PayloadAssembler> logger,
5558
IServiceScopeFactory serviceScopeFactory)
5659
{
5760
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
5861
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
62+
var scope = _serviceScopeFactory.CreateScope();
63+
_repository = scope.ServiceProvider.GetRequiredService<IPayloadRepository>();
5964

6065
_workItems = [];
6166
_tokenSource = new CancellationTokenSource();
@@ -69,15 +74,18 @@ public PayloadAssembler(
6974
};
7075
_timer.Elapsed += OnTimedEvent;
7176
_timer.Enabled = true;
77+
78+
Status = ServiceStatus.Running;
7279
}
7380

81+
public string ServiceName { get => nameof(PayloadAssembler); }
82+
83+
public ServiceStatus Status { get; set; } = ServiceStatus.Unknown;
84+
7485
private async Task RemovePendingPayloads()
7586
{
7687
_logger.RemovingPendingPayloads();
77-
var scope = _serviceScopeFactory.CreateScope();
78-
var repository = scope.ServiceProvider.GetRequiredService<IPayloadRepository>();
79-
80-
var removed = await repository.RemovePendingPayloadsAsync().ConfigureAwait(false);
88+
var removed = await _repository.RemovePendingPayloadsAsync().ConfigureAwait(false);
8189

8290
_logger.TotalNumberOfPayloadsRemoved(removed);
8391
}
@@ -192,8 +200,7 @@ private async Task QueueBucketForNotification(string key, Payload payload)
192200
{
193201
payload.State = Payload.PayloadState.Move;
194202
var scope = _serviceScopeFactory.CreateScope();
195-
var repository = scope.ServiceProvider.GetRequiredService<IPayloadRepository>();
196-
await repository.UpdateAsync(payload).ConfigureAwait(false);
203+
await _repository.UpdateAsync(payload).ConfigureAwait(false);
197204
_logger.PayloadSaved(payload.PayloadId);
198205
_workItems.Add(payload);
199206
_logger.BucketReady(key, payload.Count);
@@ -219,10 +226,8 @@ private async Task<Payload> CreateOrGetPayload(string key, string correlationId,
219226

220227
private async Task<Payload> PayloadFactory(string key, string correlationId, string? workflowInstanceId, string? taskId, Messaging.Events.DataOrigin dataOrigin, uint timeout, CancellationToken cancellationToken)
221228
{
222-
var scope = _serviceScopeFactory.CreateScope();
223-
var repository = scope.ServiceProvider.GetRequiredService<IPayloadRepository>();
224229
var newPayload = new Payload(key, correlationId, workflowInstanceId, taskId, dataOrigin, timeout, null);
225-
await repository.AddAsync(newPayload, cancellationToken).ConfigureAwait(false);
230+
await _repository.AddAsync(newPayload, cancellationToken).ConfigureAwait(false);
226231
_logger.BucketCreated(key, timeout);
227232
return newPayload;
228233
}
@@ -232,6 +237,7 @@ public void Dispose()
232237
_tokenSource.Cancel();
233238
_payloads.Clear();
234239
_timer.Stop();
240+
Status = ServiceStatus.Stopped;
235241
}
236242
}
237243
}

src/InformaticsGateway/Services/Http/MonaiHealthCheck.cs

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
using System.Linq;
1919
using System.Threading;
2020
using System.Threading.Tasks;
21+
2122
using Microsoft.Extensions.Diagnostics.HealthChecks;
23+
using Microsoft.Extensions.Logging;
24+
using Monai.Deploy.InformaticsGateway.Logging;
2225
using Monai.Deploy.InformaticsGateway.Repositories;
2326

2427
namespace Monai.Deploy.InformaticsGateway.Services.Http
2528
{
2629
public class MonaiHealthCheck : IHealthCheck
2730
{
2831
private readonly IMonaiServiceLocator _monaiServiceLocator;
32+
private readonly ILogger<MonaiHealthCheck> _logger;
2933

30-
public MonaiHealthCheck(IMonaiServiceLocator monaiServiceLocator)
34+
public MonaiHealthCheck(IMonaiServiceLocator monaiServiceLocator, ILogger<MonaiHealthCheck> logger)
3135
{
3236
_monaiServiceLocator = monaiServiceLocator ?? throw new ArgumentNullException(nameof(monaiServiceLocator));
37+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3338
}
3439

3540
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
@@ -44,9 +49,10 @@ public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, Canc
4449

4550
if (unhealthyServices.Count == services.Count)
4651
{
52+
_logger.AllServiceUnheathly();
4753
return Task.FromResult(HealthCheckResult.Unhealthy(data: unhealthyServices));
4854
}
49-
55+
_logger.SomeServiceUnheathly(string.Join(",", unhealthyServices.Keys));
5056
return Task.FromResult(HealthCheckResult.Degraded(data: unhealthyServices));
5157
}
5258
}

0 commit comments

Comments
 (0)