-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathQueueLengthProvider.cs
More file actions
118 lines (99 loc) · 4.28 KB
/
QueueLengthProvider.cs
File metadata and controls
118 lines (99 loc) · 4.28 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace ServiceControl.Transports.ASQ
{
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Storage.Queues;
using Azure.Storage.Queues.Models;
using Microsoft.Extensions.Logging;
class QueueLengthProvider : AbstractQueueLengthProvider
{
public QueueLengthProvider(TransportSettings settings, Action<QueueLengthEntry[], EndpointToQueueMapping> store, ILogger<QueueLengthProvider> logger)
: base(settings, store)
{
connectionString = ConnectionString.RemoveCustomConnectionStringParts(out _);
this.logger = logger;
}
public override void TrackEndpointInputQueue(EndpointToQueueMapping queueToTrack)
{
var queueName = BackwardsCompatibleQueueNameSanitizer.Sanitize(queueToTrack.InputQueue);
var queueClient = new QueueClient(connectionString, queueName);
var emptyQueueLength = new QueueLengthValue
{
QueueName = queueName,
Length = 0,
QueueReference = queueClient
};
queueLengths.AddOrUpdate(queueToTrack, _ => emptyQueueLength, (_, existingQueueLength) => existingQueueLength);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await FetchQueueSizes(stoppingToken);
UpdateQueueLengthStore();
await Task.Delay(QueryDelayInterval, stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
// no-op
}
catch (Exception e)
{
logger.LogError(e, "Error querying sql queue sizes.");
}
}
}
Task FetchQueueSizes(CancellationToken cancellationToken) => Task.WhenAll(queueLengths.Select(kvp => FetchLength(kvp.Value, cancellationToken)));
async Task FetchLength(QueueLengthValue queueLength, CancellationToken cancellationToken)
{
try
{
var queueReference = queueLength.QueueReference;
QueueProperties properties = await queueReference.GetPropertiesAsync(cancellationToken);
queueLength.Length = properties.ApproximateMessagesCount;
problematicQueuesNames.TryRemove(queueLength.QueueName, out _);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// no-op
}
catch (Exception ex)
{
// simple "log once" approach to do not flood logs
if (problematicQueuesNames.TryAdd(queueLength.QueueName, queueLength.QueueName))
{
logger.LogError(ex, "Obtaining Azure Storage Queue count failed for '{QueueName}'", queueLength.QueueName);
}
}
}
void UpdateQueueLengthStore()
{
var nowTicks = DateTime.UtcNow.Ticks;
foreach (var endpointQueueLengthPair in queueLengths)
{
var queueLengthEntry = new QueueLengthEntry
{
DateTicks = nowTicks,
Value = endpointQueueLengthPair.Value.Length
};
Store(new[] { queueLengthEntry }, endpointQueueLengthPair.Key);
}
}
readonly string connectionString;
readonly ConcurrentDictionary<EndpointToQueueMapping, QueueLengthValue> queueLengths = new ConcurrentDictionary<EndpointToQueueMapping, QueueLengthValue>();
readonly ConcurrentDictionary<string, string> problematicQueuesNames = new ConcurrentDictionary<string, string>();
readonly ILogger<QueueLengthProvider> logger;
static readonly TimeSpan QueryDelayInterval = TimeSpan.FromMilliseconds(200);
class QueueLengthValue
{
public string QueueName;
public volatile int Length;
public QueueClient QueueReference;
}
}
}