@@ -14,7 +14,7 @@ namespace Tingle.EventBus;
14
14
/// <summary>
15
15
/// The abstractions for an event bus
16
16
/// </summary>
17
- public class EventBus : IHostedService
17
+ public class EventBus : BackgroundService
18
18
{
19
19
private readonly IHostApplicationLifetime lifetime ;
20
20
private readonly IReadinessProvider readinessProvider ;
@@ -206,15 +206,9 @@ public async Task CancelAsync<TEvent>(IList<string> ids, CancellationToken cance
206
206
}
207
207
208
208
/// <inheritdoc/>
209
- public Task StartAsync ( CancellationToken cancellationToken )
209
+ protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
210
210
{
211
- _ = StartInternalAsync ( cancellationToken ) ;
212
- return Task . CompletedTask ;
213
- }
214
-
215
- private async Task StartInternalAsync ( CancellationToken cancellationToken )
216
- {
217
- if ( ! await WaitForAppStartupAsync ( lifetime , cancellationToken ) . ConfigureAwait ( false ) )
211
+ if ( ! await WaitForAppStartupAsync ( lifetime , stoppingToken ) . ConfigureAwait ( false ) )
218
212
{
219
213
logger . ApplicationDidNotStartup ( ) ;
220
214
return ;
@@ -224,16 +218,24 @@ private async Task StartInternalAsync(CancellationToken cancellationToken)
224
218
var delay = options . StartupDelay ;
225
219
if ( delay != null && delay > TimeSpan . Zero )
226
220
{
227
- // We cannot await the call because it will cause other components not to start.
228
- // Instead, create a cancellation token linked to the one provided so that we can
229
- // stop startup if told to do so.
230
- var cts = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
231
- _ = DelayThenStartTransportsAsync ( delay . Value , cts . Token ) ;
221
+ // With delayed startup, the error may dissappear since the call to this method is not awaited.
222
+ // The appropriate logging needs to be done.
223
+ try
224
+ {
225
+ logger . DelayedBusStartup ( delay . Value ) ;
226
+ await Task . Delay ( delay . Value , stoppingToken ) ;
227
+ await StartTransportsAsync ( stoppingToken ) ;
228
+ }
229
+ catch ( Exception ex )
230
+ when ( ! ( ex is OperationCanceledException || ex is TaskCanceledException ) ) // skip operation cancel
231
+ {
232
+ logger . DelayedBusStartupError ( ex ) ;
233
+ }
232
234
}
233
235
else
234
236
{
235
237
// Without a delay, just start the transports directly
236
- await StartTransportsAsync ( cancellationToken ) ;
238
+ await StartTransportsAsync ( stoppingToken ) ;
237
239
}
238
240
}
239
241
@@ -252,23 +254,6 @@ private static async Task<bool> WaitForAppStartupAsync(IHostApplicationLifetime
252
254
return completedTask == startedTcs . Task ;
253
255
}
254
256
255
- private async Task DelayThenStartTransportsAsync ( TimeSpan delay , CancellationToken cancellationToken )
256
- {
257
- // With delayed startup, the error may dissappear since the call to this method is not awaited.
258
- // The appropriate logging needs to be done.
259
- try
260
- {
261
- logger . DelayedBusStartup ( delay ) ;
262
- await Task . Delay ( delay , cancellationToken ) ;
263
- await StartTransportsAsync ( cancellationToken ) ;
264
- }
265
- catch ( Exception ex )
266
- when ( ! ( ex is OperationCanceledException || ex is TaskCanceledException ) ) // skip operation cancel
267
- {
268
- logger . DelayedBusStartupError ( ex ) ;
269
- }
270
- }
271
-
272
257
private async Task StartTransportsAsync ( CancellationToken cancellationToken )
273
258
{
274
259
if ( options . Readiness . Enabled )
@@ -295,8 +280,10 @@ private async Task StartTransportsAsync(CancellationToken cancellationToken)
295
280
}
296
281
297
282
/// <inheritdoc/>
298
- public async Task StopAsync ( CancellationToken cancellationToken )
283
+ public override async Task StopAsync ( CancellationToken cancellationToken )
299
284
{
285
+ await base . StopAsync ( cancellationToken ) ;
286
+
300
287
// Stop the bus and its transports in parallel
301
288
logger . StoppingBus ( ) ;
302
289
var tasks = transports . Select ( t => t . StopAsync ( cancellationToken ) ) ;
0 commit comments