/
ImportFailedAuditsCommand.cs
55 lines (46 loc) · 1.74 KB
/
ImportFailedAuditsCommand.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
namespace ServiceControl.Audit.Infrastructure.Hosting.Commands
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Auditing;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using NServiceBus;
using Settings;
class ImportFailedAuditsCommand : AbstractCommand
{
public override async Task Execute(HostArguments args, Settings settings)
{
settings.IngestAuditMessages = false;
var endpointConfiguration = new EndpointConfiguration(settings.ServiceName);
var loggingSettings = new LoggingSettings(settings.ServiceName, LogLevel.Info);
using var tokenSource = new CancellationTokenSource();
// TODO: Ideally we would never want to actually bootstrap the web api. Figure out how
var hostBuilder = WebApplication.CreateBuilder();
hostBuilder.AddServiceControlAudit((_, __) =>
{
tokenSource.Cancel();
return Task.CompletedTask;
}, settings, endpointConfiguration, loggingSettings);
using var app = hostBuilder.Build();
app.UseServiceControlAudit();
await app.StartAsync(tokenSource.Token);
var importer = app.Services.GetRequiredService<ImportFailedAudits>();
Console.CancelKeyPress += (_, _) => { tokenSource.Cancel(); };
try
{
await importer.Run(tokenSource.Token);
}
catch (OperationCanceledException)
{
// no op
}
finally
{
await app.StopAsync(CancellationToken.None);
}
}
}
}