-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureQueueStorageHealthCheck.cs
66 lines (54 loc) · 2.8 KB
/
AzureQueueStorageHealthCheck.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Threading;
using System.Threading.Tasks;
using Bet.Extensions.AzureStorage;
using Bet.Extensions.AzureStorage.Options;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Queue;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Bet.Extensions.HealthChecks.AzureStorage
{
public class AzureQueueStorageHealthCheck : IHealthCheck
{
private readonly IOptionsMonitor<StorageQueueOptions> _storageQueueOptionsMonitor;
private readonly ILogger<AzureQueueStorageHealthCheck> _logger;
private readonly IStorageQueue<StorageQueueOptions> _storageQueue;
public AzureQueueStorageHealthCheck(
IOptionsMonitor<StorageQueueOptions> storageQueueOptionsMonitor,
IStorageQueue<StorageQueueOptions> storageQueue,
ILogger<AzureQueueStorageHealthCheck> logger)
{
_storageQueueOptionsMonitor = storageQueueOptionsMonitor ?? throw new ArgumentNullException(nameof(storageQueueOptionsMonitor));
_storageQueue = storageQueue ?? throw new ArgumentNullException(nameof(storageQueue));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var checkName = context.Registration.Name;
var options = _storageQueueOptionsMonitor.Get(checkName);
var fullCheckName = $"HealCheck Name:{checkName}; Queue Name:{options?.QueueName}";
try
{
_logger.LogInformation("[HealthCheck][{checkName}] Queue: {queueName}", checkName, options?.QueueName);
var queue = await _storageQueue.GetNamedQueue(checkName, cancellationToken).Value;
if (queue != null)
{
var blobClient = queue.ServiceClient;
var serviceProperties = await blobClient.GetServicePropertiesAsync(
new QueueRequestOptions(),
operationContext: null,
cancellationToken: cancellationToken);
return new HealthCheckResult(HealthStatus.Healthy, fullCheckName);
}
return new HealthCheckResult(context.Registration.FailureStatus, $"{checkName} failed to get {nameof(CloudStorageAccount)} instance for Queue: {options?.QueueName}");
}
catch (Exception ex)
{
_logger.LogError(ex, "[HealthCheck][{checkName}] Queue: {queueName}", checkName, options?.QueueName);
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}
}