Skip to content

Commit ce72d49

Browse files
committed
Add token to timer stop
1 parent 2dfebd4 commit ce72d49

File tree

6 files changed

+19
-36
lines changed

6 files changed

+19
-36
lines changed

src/ServiceControl.Infrastructure/AsyncTimer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
5757
}, CancellationToken.None);
5858
}
5959

60-
public async Task Stop()
60+
public async Task Stop(CancellationToken cancellationToken)
6161
{
6262
if (tokenSource == null)
6363
{
@@ -67,16 +67,18 @@ public async Task Stop()
6767
await tokenSource.CancelAsync().ConfigureAwait(false);
6868
tokenSource.Dispose();
6969

70-
if (task != null)
70+
if (task == null)
7171
{
72-
try
73-
{
74-
await task.ConfigureAwait(false);
75-
}
76-
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
77-
{
78-
//NOOP
79-
}
72+
return;
73+
}
74+
75+
try
76+
{
77+
Task.WaitAll([task], cancellationToken);
78+
}
79+
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
80+
{
81+
//NOOP
8082
}
8183
}
8284

src/ServiceControl.Monitoring/Licensing/LicenseCheckHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Task StartAsync(CancellationToken cancellationToken)
2020
return Task.CompletedTask;
2121
}
2222

23-
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop();
23+
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
2424

2525
TimerJob timer;
2626

src/ServiceControl/CustomChecks/InternalCustomChecks/InternalCustomCheckManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async Task<TimerJobExecutionResult> Run(CancellationToken cancellationToken)
6767
: TimerJobExecutionResult.DoNotContinueExecuting;
6868
}
6969

70-
public Task Stop() => timer?.Stop() ?? Task.CompletedTask;
70+
public Task Stop() => timer?.Stop(CancellationToken.None) ?? Task.CompletedTask;
7171

7272
TimerJob timer;
7373
readonly ICustomCheck check;

src/ServiceControl/Licensing/LicenseCheckHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Task StartAsync(CancellationToken cancellationToken)
2121
return Task.CompletedTask;
2222
}
2323

24-
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop();
24+
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
2525

2626
TimerJob timer;
2727

src/ServiceControl/Monitoring/HeartbeatMonitoringHostedService.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
2424
timer = scheduler.Schedule(_ => CheckEndpoints(), TimeSpan.Zero, TimeSpan.FromSeconds(5), e => { log.Error("Exception occurred when monitoring endpoint instances", e); });
2525
}
2626

27-
public async Task StopAsync(CancellationToken cancellationToken)
28-
{
29-
try
30-
{
31-
await timer.Stop();
32-
}
33-
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
34-
{
35-
//NOOP, invoked Stop does not
36-
}
37-
}
27+
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
3828

3929
async Task<TimerJobExecutionResult> CheckEndpoints()
4030
{

src/ServiceControl/Recoverability/RecoverabilityComponent.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ public Task StartAsync(CancellationToken cancellationToken)
174174
return Task.CompletedTask;
175175
}
176176

177-
public Task StopAsync(CancellationToken cancellationToken)
178-
{
179-
return timer?.Stop() ?? Task.CompletedTask;
180-
}
177+
public Task StopAsync(CancellationToken cancellationToken) => timer?.Stop(cancellationToken) ?? Task.CompletedTask;
181178

182179
async Task<TimerJobExecutionResult> ProcessRequestedBulkRetryOperations()
183180
{
@@ -237,10 +234,7 @@ public Task StartAsync(CancellationToken cancellationToken)
237234
return Task.CompletedTask;
238235
}
239236

240-
public Task StopAsync(CancellationToken cancellationToken)
241-
{
242-
return timer.Stop();
243-
}
237+
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
244238

245239
TimerJob timer;
246240
readonly IAsyncTimer scheduler;
@@ -266,10 +260,7 @@ public Task StartAsync(CancellationToken cancellationToken)
266260
return Task.CompletedTask;
267261
}
268262

269-
public async Task StopAsync(CancellationToken cancellationToken)
270-
{
271-
await timer.Stop();
272-
}
263+
public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
273264

274265
async Task<TimerJobExecutionResult> Process(CancellationToken cancellationToken)
275266
{

0 commit comments

Comments
 (0)