-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeekDlqBatchCommandHandler.cs
More file actions
147 lines (127 loc) · 5.75 KB
/
PeekDlqBatchCommandHandler.cs
File metadata and controls
147 lines (127 loc) · 5.75 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Ardalis.Result;
using Azure;
using Azure.Messaging.ServiceBus;
using Mediator;
using ServiceBusToolset.Application.Common.ServiceBus.Abstractions;
using ServiceBusToolset.Application.Common.ServiceBus.Helpers;
using ServiceBusToolset.Application.Common.ServiceBus.Models;
using ServiceBusToolset.Application.DeadLetters.DiagnoseDlq.Common;
namespace ServiceBusToolset.Application.DeadLetters.PeekDlq;
public sealed class PeekDlqBatchCommandHandler(IServiceBusClientFactory clientFactory)
: ICommandHandler<PeekDlqBatchCommand, Result<PeekDlqBatchResult>>
{
private const int PeekSubBatchSize = 100;
private const int EmptyBatchThreshold = 3;
public async ValueTask<Result<PeekDlqBatchResult>> Handle(
PeekDlqBatchCommand command,
CancellationToken cancellationToken)
{
await using var client = clientFactory.CreateClient(command.FullyQualifiedNamespace);
// Get total DLQ count — use known count if provided, otherwise query admin API
var totalDeadLetterCount = command.KnownDeadLetterCount
?? await GetDeadLetterCountAsync(clientFactory, command.FullyQualifiedNamespace, command.Target);
// Peek messages in sub-batches until we reach BatchSize or run out
await using var receiver = ReceiverFactory.CreateDlqReceiver(client, command.Target);
List<ServiceBusReceivedMessage> allMessages = [];
var emptyBatches = 0;
var isFirstPeek = true;
var highestSequenceNumber = command.FromSequenceNumber ?? -1;
while (allMessages.Count < command.BatchSize &&
emptyBatches < EmptyBatchThreshold &&
!cancellationToken.IsCancellationRequested)
{
var remaining = command.BatchSize - allMessages.Count;
var subBatchSize = Math.Min(PeekSubBatchSize, remaining);
IReadOnlyList<ServiceBusReceivedMessage> batch;
if (isFirstPeek && command.FromSequenceNumber.HasValue)
{
batch = await receiver.PeekMessagesAsync(subBatchSize, command.FromSequenceNumber.Value + 1, cancellationToken);
isFirstPeek = false;
}
else
{
batch = await receiver.PeekMessagesAsync(subBatchSize, cancellationToken:cancellationToken);
isFirstPeek = false;
}
if (batch.Count == 0)
{
emptyBatches++;
continue;
}
// Detect wrap-around: if the batch contains messages with sequence numbers
// we've already passed, the receiver has looped back to the beginning
if (batch[0].SequenceNumber <= highestSequenceNumber)
{
break;
}
emptyBatches = 0;
allMessages.AddRange(batch);
highestSequenceNumber = batch[^1].SequenceNumber;
}
if (allMessages.Count == 0)
{
return Result.Success(new PeekDlqBatchResult([],
0,
0,
command.FromSequenceNumber,
false,
totalDeadLetterCount));
}
// Extract operation IDs
List<PeekedMessage> messages = [];
var skipped = 0;
HashSet<string> seenOperationIds = [];
foreach (var message in allMessages)
{
var operationId = MessageDiagnostics.ExtractOperationId(message);
if (string.IsNullOrEmpty(operationId))
{
skipped++;
continue;
}
if (seenOperationIds.Add(operationId))
{
messages.Add(new PeekedMessage(message.MessageId,
message.Subject,
operationId,
message.EnqueuedTime,
message.DeadLetterReason));
}
}
var lastSequenceNumber = allMessages[^1].SequenceNumber;
// We have more messages if we filled the batch (didn't run out early)
var hasMore = allMessages.Count >= command.BatchSize && emptyBatches < EmptyBatchThreshold;
return Result.Success(new PeekDlqBatchResult(messages,
allMessages.Count,
skipped,
lastSequenceNumber,
hasMore,
totalDeadLetterCount));
}
private static async Task<long> GetDeadLetterCountAsync(
IServiceBusClientFactory clientFactory,
string fullyQualifiedNamespace,
EntityTarget target)
{
try
{
var adminClient = clientFactory.CreateAdministrationClient(fullyQualifiedNamespace);
if (target.IsQueueMode)
{
var props = await adminClient.GetQueueRuntimePropertiesAsync(target.Queue!);
return props.Value.DeadLetterMessageCount;
}
var subProps = await adminClient.GetSubscriptionRuntimePropertiesAsync(target.Topic!, target.Subscription!);
return subProps.Value.DeadLetterMessageCount;
}
catch (RequestFailedException)
{
// Admin API may not be available in all environments (e.g., emulator, unit tests)
return 0;
}
catch (OperationCanceledException)
{
return 0;
}
}
}