-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionHostedService.cs
More file actions
125 lines (114 loc) · 4.44 KB
/
Copy pathConnectionHostedService.cs
File metadata and controls
125 lines (114 loc) · 4.44 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Features.Connections.Supervisor;
namespace RustPlusBot.Features.Connections.Hosting;
/// <summary>Drives the connection supervisor: start all on startup, react to server registration, stop on shutdown.</summary>
/// <param name="supervisor">The connection supervisor.</param>
/// <param name="eventBus">The in-process event bus.</param>
/// <param name="logger">The logger.</param>
internal sealed partial class ConnectionHostedService(
IConnectionSupervisor supervisor,
IEventBus eventBus,
ILogger<ConnectionHostedService> logger) : IHostedService, IDisposable
{
private readonly CancellationTokenSource _cts = new();
private Task? _eventLoop;
/// <inheritdoc />
public void Dispose() => _cts.Dispose();
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
_eventLoop = Task.Run(() => RunAsync(_cts.Token), CancellationToken.None);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cts.CancelAsync().ConfigureAwait(false);
if (_eventLoop is not null)
{
try
{
#pragma warning disable VSTHRD003 // Suppress: this is our own loop task, joined on stop.
await _eventLoop.ConfigureAwait(false);
#pragma warning restore VSTHRD003
}
catch (OperationCanceledException)
{
// Expected on shutdown.
}
}
await supervisor.StopAllAsync().ConfigureAwait(false);
}
private async Task RunAsync(CancellationToken cancellationToken)
{
// Subscriptions register when each loop first awaits the bus, after StartAllAsync completes.
// The in-process bus does not replay, so events published before this point are not delivered. Safe:
// both events are only raised at runtime, long after startup.
try
{
await supervisor.StartAllAsync(cancellationToken).ConfigureAwait(false);
await Task.WhenAll(
ConsumeRegisteredAsync(cancellationToken),
ConsumeCredentialsChangedAsync(cancellationToken))
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Shutting down.
}
#pragma warning disable CA1031 // Broad catch is intentional: a faulting consumer must not crash the host.
catch (Exception ex)
{
LogLoopFaulted(logger, ex);
}
#pragma warning restore CA1031
}
private async Task ConsumeRegisteredAsync(CancellationToken cancellationToken)
{
try
{
await foreach (var registered in eventBus.SubscribeAsync<ServerRegisteredEvent>(cancellationToken)
.ConfigureAwait(false))
{
await supervisor.EnsureConnectionAsync(registered.GuildId, registered.ServerId, cancellationToken)
.ConfigureAwait(false);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
#pragma warning disable CA1031 // Broad catch is intentional: one faulting consumer must not stop the other or crash the host.
catch (Exception ex)
{
LogLoopFaulted(logger, ex);
}
#pragma warning restore CA1031
}
private async Task ConsumeCredentialsChangedAsync(CancellationToken cancellationToken)
{
try
{
await foreach (var changed in eventBus.SubscribeAsync<ServerCredentialsChangedEvent>(cancellationToken)
.ConfigureAwait(false))
{
await supervisor.EnsureConnectionAsync(changed.GuildId, changed.ServerId, cancellationToken)
.ConfigureAwait(false);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
#pragma warning disable CA1031 // Broad catch is intentional: one faulting consumer must not stop the other or crash the host.
catch (Exception ex)
{
LogLoopFaulted(logger, ex);
}
#pragma warning restore CA1031
}
[LoggerMessage(Level = LogLevel.Error, Message = "Connection hosted-service loop faulted.")]
private static partial void LogLoopFaulted(ILogger logger, Exception exception);
}