-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AzureServiceBusHealthCheck.cs
38 lines (31 loc) · 1.49 KB
/
AzureServiceBusHealthCheck.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
36
37
using Azure.Messaging.ServiceBus.Administration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Tingle.EventBus.Transports.Azure.ServiceBus;
namespace HealthCheck;
internal class AzureServiceBusHealthCheck(IOptionsMonitor<AzureServiceBusTransportOptions> optionsMonitor) : IHealthCheck
{
private const string QueueName = "health-check";
private readonly AzureServiceBusTransportOptions options = optionsMonitor?.Get(AzureServiceBusDefaults.Name) ?? throw new ArgumentNullException(nameof(optionsMonitor));
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var cred = (AzureServiceBusTransportCredentials)options.Credentials.CurrentValue;
var managementClient = new ServiceBusAdministrationClient(
fullyQualifiedNamespace: cred.FullyQualifiedNamespace,
credential: cred.TokenCredential);
_ = await managementClient.GetQueueRuntimePropertiesAsync(QueueName, cancellationToken);
return HealthCheckResult.Healthy();
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// ignore long running calls
return HealthCheckResult.Healthy();
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}