-
Notifications
You must be signed in to change notification settings - Fork 9
/
MessageProcessingHealthCheck.cs
35 lines (30 loc) · 1.23 KB
/
MessageProcessingHealthCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;
namespace Motor.Extensions.Hosting.HealthChecks;
public class MessageProcessingHealthCheck<TInput> : IHealthCheck where TInput : class
{
private readonly TimeSpan _maxTimeWithoutAcknowledgedMessage;
private readonly IBackgroundTaskQueue<MotorCloudEvent<TInput>> _queue;
public MessageProcessingHealthCheck(IOptions<MessageProcessingOptions> options,
IBackgroundTaskQueue<MotorCloudEvent<TInput>> queue)
{
_maxTimeWithoutAcknowledgedMessage = options.Value.MaxTimeSinceLastProcessedMessage;
_queue = queue;
}
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
CancellationToken token = default)
{
if (_queue.ItemCount == 0)
{
return Task.FromResult(HealthCheckResult.Healthy());
}
return Task.FromResult(DateTimeOffset.UtcNow - _queue.LastDequeuedAt > _maxTimeWithoutAcknowledgedMessage
? HealthCheckResult.Unhealthy()
: HealthCheckResult.Healthy());
}
}