Skip to content

Commit 5a5533e

Browse files
committed
Overriding Start and Stop to support graceful shutdown and improve intent and readability
1 parent 9c249c9 commit 5a5533e

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/ServiceControl.Audit/Auditing/AuditIngestion.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,14 @@ async Task OnMessage(MessageContext messageContext, CancellationToken cancellati
194194
await taskCompletionSource.Task;
195195
}
196196

197-
198-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
197+
public override async Task StartAsync(CancellationToken cancellationToken)
199198
{
200199
await watchdog.Start(() => applicationLifetime.StopApplication());
200+
await base.StartAsync(cancellationToken);
201+
}
201202

203+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
204+
{
202205
try
203206
{
204207
var contexts = new List<MessageContext>(transportSettings.MaxConcurrency.Value);
@@ -243,18 +246,30 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
243246
}
244247
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
245248
{
246-
// Cancellation
249+
// ExecuteAsync cancelled
247250
}
248-
finally
251+
}
252+
253+
public override async Task StopAsync(CancellationToken cancellationToken)
254+
{
255+
try
249256
{
250257
await watchdog.Stop();
251258
channel.Writer.Complete();
252-
259+
await base.StopAsync(cancellationToken);
260+
}
261+
finally
262+
{
253263
if (transportInfrastructure != null)
254264
{
255-
// stoppingToken is cancelled, invoke so transport infrastructure will run teardown
256-
// No need to await, as this will throw OperationCancelledException
257-
_ = transportInfrastructure.Shutdown(stoppingToken);
265+
try
266+
{
267+
await transportInfrastructure.Shutdown(cancellationToken);
268+
}
269+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
270+
{
271+
// StopAsync cancelled
272+
}
258273
}
259274
}
260275
}

src/ServiceControl/Operations/ErrorIngestion.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ public ErrorIngestion(
7070
);
7171
}
7272

73-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
73+
public override async Task StartAsync(CancellationToken cancellationToken)
7474
{
7575
await watchdog.Start(() => applicationLifetime.StopApplication());
76+
await base.StartAsync(cancellationToken);
77+
}
7678

79+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
80+
{
7781
try
7882
{
7983
var contexts = new List<MessageContext>(transportSettings.MaxConcurrency.Value);
@@ -118,17 +122,30 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
118122
}
119123
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
120124
{
121-
// Cancellation
125+
// ExecuteAsync cancelled
122126
}
123-
finally
127+
}
128+
129+
public override async Task StopAsync(CancellationToken cancellationToken)
130+
{
131+
try
124132
{
125133
await watchdog.Stop();
126134
channel.Writer.Complete();
135+
await base.StopAsync(cancellationToken);
136+
}
137+
finally
138+
{
127139
if (transportInfrastructure != null)
128140
{
129-
// stoppingToken is cancelled, invoke so transport infrastructure will run teardown
130-
// No need to await, as this will throw OperationCancelledException
131-
_ = transportInfrastructure.Shutdown(stoppingToken);
141+
try
142+
{
143+
await transportInfrastructure.Shutdown(cancellationToken);
144+
}
145+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
146+
{
147+
// StopAsync cancelled
148+
}
132149
}
133150
}
134151
}

0 commit comments

Comments
 (0)