-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathMonitoringThroughputHostedService.cs
More file actions
43 lines (36 loc) · 1.87 KB
/
MonitoringThroughputHostedService.cs
File metadata and controls
43 lines (36 loc) · 1.87 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
namespace Particular.LicensingComponent.MonitoringThroughput;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus.Transport;
using ServiceControl.Transports;
using Shared;
class MonitoringThroughputHostedService(ITransportCustomization transportCustomization, TransportSettings transportSettings, ILogger<MonitoringThroughputHostedService> logger, MonitoringService monitoringService) : IHostedService
{
TransportInfrastructure? transportInfrastructure;
async Task Handle(MessageContext message, CancellationToken cancellationToken)
{
try
{
await monitoringService.RecordMonitoringThroughput(message.Body.ToArray(), cancellationToken);
}
catch (Exception ex)
{
logger.LogError(ex, "Error receiving throughput data from Monitoring");
}
}
public async Task StartAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Starting {ServiceName}", nameof(MonitoringThroughputHostedService));
transportInfrastructure = await transportCustomization.CreateTransportInfrastructure(ServiceControlSettings.ServiceControlThroughputDataQueue, transportSettings, Handle, (_, __) => Task.FromResult(ErrorHandleResult.Handled), (_, __) => Task.CompletedTask);
await transportInfrastructure.Receivers[ServiceControlSettings.ServiceControlThroughputDataQueue].StartReceive(cancellationToken);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Stopping {ServiceName}", nameof(MonitoringThroughputHostedService));
if (transportInfrastructure != null)
{
await transportInfrastructure.Receivers[ServiceControlSettings.ServiceControlThroughputDataQueue].StopReceive(cancellationToken);
await transportInfrastructure.Shutdown(cancellationToken);
}
}
}