@@ -50,19 +50,17 @@ public async Task StopAsync(CancellationToken cancellationToken)
5050
5151 private async Task RunAsync ( CancellationToken cancellationToken )
5252 {
53- // Subscription is registered when this loop first awaits the bus, after StartAllAsync completes.
53+ // Subscriptions register when each loop first awaits the bus, after StartAllAsync completes.
5454 // The in-process bus does not replay, so events published before this point are not delivered. Safe:
55- // the server-registered event is only raised by the FCM pairing flow at runtime, long after startup.
55+ // both events are only raised at runtime, long after startup.
5656 try
5757 {
5858 await supervisor . StartAllAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
5959
60- await foreach ( var registered in eventBus . SubscribeAsync < ServerRegisteredEvent > ( cancellationToken )
61- . ConfigureAwait ( false ) )
62- {
63- await supervisor . EnsureConnectionAsync ( registered . GuildId , registered . ServerId , cancellationToken )
64- . ConfigureAwait ( false ) ;
65- }
60+ await Task . WhenAll (
61+ ConsumeRegisteredAsync ( cancellationToken ) ,
62+ ConsumeCredentialsChangedAsync ( cancellationToken ) )
63+ . ConfigureAwait ( false ) ;
6664 }
6765 catch ( OperationCanceledException )
6866 {
@@ -76,6 +74,26 @@ await supervisor.EnsureConnectionAsync(registered.GuildId, registered.ServerId,
7674#pragma warning restore CA1031
7775 }
7876
77+ private async Task ConsumeRegisteredAsync ( CancellationToken cancellationToken )
78+ {
79+ await foreach ( var registered in eventBus . SubscribeAsync < ServerRegisteredEvent > ( cancellationToken )
80+ . ConfigureAwait ( false ) )
81+ {
82+ await supervisor . EnsureConnectionAsync ( registered . GuildId , registered . ServerId , cancellationToken )
83+ . ConfigureAwait ( false ) ;
84+ }
85+ }
86+
87+ private async Task ConsumeCredentialsChangedAsync ( CancellationToken cancellationToken )
88+ {
89+ await foreach ( var changed in eventBus . SubscribeAsync < ServerCredentialsChangedEvent > ( cancellationToken )
90+ . ConfigureAwait ( false ) )
91+ {
92+ await supervisor . EnsureConnectionAsync ( changed . GuildId , changed . ServerId , cancellationToken )
93+ . ConfigureAwait ( false ) ;
94+ }
95+ }
96+
7997 [ LoggerMessage ( Level = LogLevel . Error , Message = "Connection hosted-service loop faulted." ) ]
8098 private static partial void LogLoopFaulted ( ILogger logger , Exception exception ) ;
8199}
0 commit comments