-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathAzureServiceBusTopicHealthCheck.cs
More file actions
58 lines (47 loc) · 2.13 KB
/
AzureServiceBusTopicHealthCheck.cs
File metadata and controls
58 lines (47 loc) · 2.13 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace HealthChecks.AzureServiceBus;
public class AzureServiceBusTopicHealthCheck : AzureServiceBusHealthCheck<AzureServiceBusTopicHealthCheckOptions>, IHealthCheck
{
private readonly string _topicKey;
public AzureServiceBusTopicHealthCheck(AzureServiceBusTopicHealthCheckOptions options, ServiceBusClientProvider clientProvider)
: base(options, clientProvider)
{
Guard.ThrowIfNull(options.TopicName, true);
_topicKey = $"{nameof(AzureServiceBusTopicHealthCheck)}_{ConnectionKey}_{Options.TopicName}";
}
public AzureServiceBusTopicHealthCheck(AzureServiceBusTopicHealthCheckOptions options)
: this(options, new ServiceBusClientProvider())
{ }
/// <inheritdoc />
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
if (Options.UseCreateMessageBatchAsyncMode)
await CheckWithSender().ConfigureAwait(false);
else
await CheckWithManagement().ConfigureAwait(false);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
async Task CheckWithSender()
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);
var sender = await ClientCache.GetOrAddAsyncDisposableAsync(
_topicKey,
_ => client.CreateSender(Options.TopicName))
.ConfigureAwait(false);
await sender.CreateMessageBatchAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
Task CheckWithManagement()
{
var managementClient = ClientCache.GetOrAdd(ConnectionKey, _ => CreateManagementClient());
return managementClient.GetTopicRuntimePropertiesAsync(Options.TopicName, cancellationToken);
}
}
}