Skip to content

Commit e1fa348

Browse files
authored
Use BackgroundService instead of IHostedService for EventBus (#435)
1 parent 2159957 commit e1fa348

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

src/Tingle.EventBus/EventBus.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Tingle.EventBus;
1414
/// <summary>
1515
/// The abstractions for an event bus
1616
/// </summary>
17-
public class EventBus : IHostedService
17+
public class EventBus : BackgroundService
1818
{
1919
private readonly IHostApplicationLifetime lifetime;
2020
private readonly IReadinessProvider readinessProvider;
@@ -206,15 +206,9 @@ public async Task CancelAsync<TEvent>(IList<string> ids, CancellationToken cance
206206
}
207207

208208
/// <inheritdoc/>
209-
public Task StartAsync(CancellationToken cancellationToken)
209+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
210210
{
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))
218212
{
219213
logger.ApplicationDidNotStartup();
220214
return;
@@ -224,16 +218,24 @@ private async Task StartInternalAsync(CancellationToken cancellationToken)
224218
var delay = options.StartupDelay;
225219
if (delay != null && delay > TimeSpan.Zero)
226220
{
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+
}
232234
}
233235
else
234236
{
235237
// Without a delay, just start the transports directly
236-
await StartTransportsAsync(cancellationToken);
238+
await StartTransportsAsync(stoppingToken);
237239
}
238240
}
239241

@@ -252,23 +254,6 @@ private static async Task<bool> WaitForAppStartupAsync(IHostApplicationLifetime
252254
return completedTask == startedTcs.Task;
253255
}
254256

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-
272257
private async Task StartTransportsAsync(CancellationToken cancellationToken)
273258
{
274259
if (options.Readiness.Enabled)
@@ -295,8 +280,10 @@ private async Task StartTransportsAsync(CancellationToken cancellationToken)
295280
}
296281

297282
/// <inheritdoc/>
298-
public async Task StopAsync(CancellationToken cancellationToken)
283+
public override async Task StopAsync(CancellationToken cancellationToken)
299284
{
285+
await base.StopAsync(cancellationToken);
286+
300287
// Stop the bus and its transports in parallel
301288
logger.StoppingBus();
302289
var tasks = transports.Select(t => t.StopAsync(cancellationToken));

0 commit comments

Comments
 (0)