-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairingHostedService.cs
More file actions
56 lines (49 loc) · 1.75 KB
/
Copy pathPairingHostedService.cs
File metadata and controls
56 lines (49 loc) · 1.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
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RustPlusBot.Features.Pairing.Supervisor;
namespace RustPlusBot.Features.Pairing.Hosting;
/// <summary>Drives the supervisor from the host lifecycle: start listeners on Ready, stop them on shutdown.</summary>
/// <param name="client">The socket client (for the Ready event).</param>
/// <param name="supervisor">The pairing supervisor.</param>
/// <param name="logger">The logger.</param>
internal sealed partial class PairingHostedService(
DiscordSocketClient client,
IPairingSupervisor supervisor,
ILogger<PairingHostedService> logger) : IHostedService
{
private bool _started;
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
client.Ready += OnReadyAsync;
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task StopAsync(CancellationToken cancellationToken)
{
client.Ready -= OnReadyAsync;
await supervisor.StopAllAsync().ConfigureAwait(false);
}
private async Task OnReadyAsync()
{
// Ready fires on every (re)connect; only start listeners once per process.
if (_started)
{
return;
}
_started = true;
try
{
await supervisor.StartAllActiveAsync().ConfigureAwait(false);
}
#pragma warning disable CA1031 // Broad catch is intentional: a failed startup must not crash the host.
catch (Exception ex)
{
LogStartupFailed(ex);
}
#pragma warning restore CA1031
}
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to start FCM pairing listeners.")]
private partial void LogStartupFailed(Exception exception);
}