-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWipesHostedService.cs
More file actions
108 lines (98 loc) · 3.98 KB
/
Copy pathWipesHostedService.cs
File metadata and controls
108 lines (98 loc) · 3.98 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
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Features.Wipes.Announcing;
using RustPlusBot.Features.Wipes.Detection;
namespace RustPlusBot.Features.Wipes.Hosting;
/// <summary>Runs the wipe-check loop (on reconnect transitions) and the wipe-announcement loop.</summary>
/// <param name="eventBus">The in-process event bus.</param>
/// <param name="detector">Diffs the live server against the persisted baseline.</param>
/// <param name="announcer">Posts the wipe announcement in #events.</param>
/// <param name="logger">The logger.</param>
internal sealed partial class WipesHostedService(
IEventBus eventBus,
IWipeDetector detector,
IWipeAnnouncer announcer,
ILogger<WipesHostedService> logger) : IHostedService, IDisposable
{
private readonly CancellationTokenSource _cts = new();
private Task? _statusLoop;
private Task? _wipedLoop;
/// <inheritdoc />
public void Dispose() => _cts.Dispose();
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
_statusLoop = Task.Run(() => ConsumeStatusAsync(_cts.Token), CancellationToken.None);
_wipedLoop = Task.Run(() => ConsumeWipedAsync(_cts.Token), CancellationToken.None);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cts.CancelAsync().ConfigureAwait(false);
foreach (var loop in new[]
{
_statusLoop, _wipedLoop
}.OfType<Task>())
{
try
{
#pragma warning disable VSTHRD003 // Our own loop tasks, joined on stop.
await loop.ConfigureAwait(false);
#pragma warning restore VSTHRD003
}
catch (OperationCanceledException)
{
// Expected on shutdown.
}
}
}
private async Task ConsumeStatusAsync(CancellationToken cancellationToken)
{
try
{
await eventBus.ConsumeAsync<ConnectionStatusChangedEvent>(
(evt, ct) => !evt.IsConnected || evt.WasConnected
? Task.CompletedTask
: detector.CheckAsync(evt.GuildId, evt.ServerId, ct),
ex => LogHandlerFailed(logger, ex, nameof(ConnectionStatusChangedEvent)), cancellationToken)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Shutting down.
}
#pragma warning disable CA1031 // Broad catch: a faulting consumer must not crash the host.
catch (Exception ex)
#pragma warning restore CA1031
{
LogStatusLoopFaulted(logger, ex);
}
}
private async Task ConsumeWipedAsync(CancellationToken cancellationToken)
{
try
{
await eventBus.ConsumeAsync<ServerWipedEvent>(announcer.HandleServerWipedAsync,
ex => LogHandlerFailed(logger, ex, nameof(ServerWipedEvent)), cancellationToken)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Shutting down.
}
#pragma warning disable CA1031 // Broad catch: a faulting consumer must not crash the host.
catch (Exception ex)
#pragma warning restore CA1031
{
LogWipedLoopFaulted(logger, ex);
}
}
[LoggerMessage(Level = LogLevel.Error, Message = "Handling {EventType} failed; skipping that event.")]
private static partial void LogHandlerFailed(ILogger logger, Exception exception, string eventType);
[LoggerMessage(Level = LogLevel.Error, Message = "Wipe connection-status loop faulted.")]
private static partial void LogStatusLoopFaulted(ILogger logger, Exception exception);
[LoggerMessage(Level = LogLevel.Error, Message = "Wipe announcement loop faulted.")]
private static partial void LogWipedLoopFaulted(ILogger logger, Exception exception);
}