-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnoseFromCache.cs
More file actions
60 lines (51 loc) · 2.55 KB
/
DiagnoseFromCache.cs
File metadata and controls
60 lines (51 loc) · 2.55 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
using Ardalis.Result;
using Azure.Messaging.ServiceBus;
using Mediator;
using ServiceBusToolset.Application.DeadLetters.DiagnoseDlq.Common;
using ServiceBusToolset.Application.DeadLetters.DiagnoseDlq.Common.AppInsights;
using ServiceBusToolset.Application.DeadLetters.DiagnoseDlq.Models;
namespace ServiceBusToolset.Application.DeadLetters.DiagnoseDlq;
public sealed record DiagnoseFromCacheCommand(string? AppInsightsResourceId,
IReadOnlyList<ServiceBusReceivedMessage> MessagesToDiagnose,
IProgress<(int Current, int Total)>? BatchProgress = null) : ICommand<Result<DiagnoseDlqResult>>;
public sealed class DiagnoseFromCacheCommandHandler(IAppInsightsService appInsightsService)
: ICommandHandler<DiagnoseFromCacheCommand, Result<DiagnoseDlqResult>>
{
public async ValueTask<Result<DiagnoseDlqResult>> Handle(
DiagnoseFromCacheCommand command,
CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(command.AppInsightsResourceId))
{
appInsightsService.Initialize(command.AppInsightsResourceId);
}
var messages = command.MessagesToDiagnose.ToList();
if (messages.Count == 0)
{
return Result.Success(new DiagnoseDlqResult([],
0,
0,
0));
}
List<DiagnosticResult> results;
int skipped;
if (string.IsNullOrEmpty(command.AppInsightsResourceId))
{
results = MessageDiagnostics.CreateBasicResults(messages);
skipped = 0;
}
else
{
(results, skipped) = await MessageDiagnostics.DiagnoseMessagesAsync(appInsightsService,
messages,
command.BatchProgress,
cancellationToken);
}
var resultsWithTelemetry = results
.Count(r => r.Exceptions.Count > 0 || r.Traces.Count > 0 || r.FailedDependencies.Count > 0);
return Result.Success(new DiagnoseDlqResult(results,
messages.Count,
skipped,
resultsWithTelemetry));
}
}