Skip to content

Commit 2ecc93f

Browse files
committed
Merge branch 'fix-watchdog-stop' into spike-ravendb-termination
2 parents 04248fe + 41e31a1 commit 2ecc93f

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/ServiceControl.Audit/Auditing/AuditIngestion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Task OnCriticalError(string failure, Exception exception)
6868
return watchdog.OnFailure(failure);
6969
}
7070

71-
async Task EnsureStarted(CancellationToken cancellationToken = default)
71+
async Task EnsureStarted(CancellationToken cancellationToken)
7272
{
7373
try
7474
{
@@ -177,7 +177,7 @@ async Task StopAndTeardownInfrastructure(CancellationToken cancellationToken)
177177
}
178178
}
179179

180-
async Task EnsureStopped(CancellationToken cancellationToken = default)
180+
async Task EnsureStopped(CancellationToken cancellationToken)
181181
{
182182
try
183183
{
@@ -231,7 +231,7 @@ async Task OnMessage(MessageContext messageContext, CancellationToken cancellati
231231

232232
public override async Task StartAsync(CancellationToken cancellationToken)
233233
{
234-
await watchdog.Start(() => applicationLifetime.StopApplication());
234+
await watchdog.Start(() => applicationLifetime.StopApplication(), cancellationToken);
235235
await base.StartAsync(cancellationToken);
236236
}
237237

src/ServiceControl.Infrastructure.Tests/WatchdogTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace ServiceControl.Infrastructure.Tests
22
{
33
using System;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using NServiceBus.Logging;
67
using NUnit.Framework;
@@ -27,7 +28,7 @@ public async Task It_shuts_down_gracefully()
2728
return Task.CompletedTask;
2829
}, x => { }, () => { }, TimeSpan.FromSeconds(1), log);
2930

30-
await dog.Start(() => { });
31+
await dog.Start(() => { }, CancellationToken.None);
3132

3233
await started.Task;
3334

@@ -48,7 +49,7 @@ public async Task When_stop_fails_it_reports_the_failure()
4849
return Task.CompletedTask;
4950
}, token => throw new Exception("Simulated"), x => lastFailure = x, () => lastFailure = null, TimeSpan.FromSeconds(1), log);
5051

51-
await dog.Start(() => { });
52+
await dog.Start(() => { }, CancellationToken.None);
5253

5354
await started.Task;
5455

@@ -83,7 +84,7 @@ public async Task On_failure_triggers_stopping()
8384
return Task.CompletedTask;
8485
}, x => { }, () => { }, TimeSpan.FromSeconds(1), log);
8586

86-
await dog.Start(() => { });
87+
await dog.Start(() => { }, CancellationToken.None);
8788

8889
await started.Task;
8990

@@ -125,7 +126,7 @@ public async Task When_first_start_attempt_works_it_recovers_from_further_errors
125126
return Task.CompletedTask;
126127
}, token => Task.CompletedTask, x => lastFailure = x, () => lastFailure = null, TimeSpan.FromSeconds(1), log);
127128

128-
await dog.Start(() => { });
129+
await dog.Start(() => { }, CancellationToken.None);
129130

130131
await recoveredFromError.Task;
131132

@@ -144,7 +145,7 @@ public async Task When_first_start_attempt_fails_onFailedOnStartup_is_called()
144145

145146
var dog = new Watchdog("test process", token => throw new Exception("Simulated"), token => Task.CompletedTask, x => lastFailure = x, () => lastFailure = null, TimeSpan.FromSeconds(1), log);
146147

147-
await dog.Start(() => { onStartupFailureCalled.SetResult(true); });
148+
await dog.Start(() => { onStartupFailureCalled.SetResult(true); }, CancellationToken.None);
148149

149150
await onStartupFailureCalled.Task;
150151

src/ServiceControl.Infrastructure/Watchdog.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Task OnFailure(string failure)
4141
return ensureStopped(shutdownTokenSource.Token);
4242
}
4343

44-
public Task Start(Action onFailedOnStartup)
44+
public Task Start(Action onFailedOnStartup, CancellationToken cancellationToken)
4545
{
4646
watchdog = Task.Run(async () =>
4747
{
@@ -53,8 +53,12 @@ public Task Start(Action onFailedOnStartup)
5353
{
5454
try
5555
{
56+
// Host builder start is launching the loop. The watch dog loop task runs in isolation
57+
// We want the start not to run to infinity. An NServiceBus endpoint should easily
58+
// start within 15 seconds.
59+
const int MaxStartDurationMs = 15000;
5660
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(shutdownTokenSource.Token);
57-
cancellationTokenSource.CancelAfter(15000);
61+
cancellationTokenSource.CancelAfter(MaxStartDurationMs);
5862

5963
log.Debug($"Ensuring {taskName} is running");
6064
await ensureStarted(cancellationTokenSource.Token).ConfigureAwait(false);
@@ -88,7 +92,8 @@ public Task Start(Action onFailedOnStartup)
8892
//Ignore, no need to log cancellation of delay
8993
}
9094
}
91-
});
95+
}, cancellationToken);
96+
9297
return Task.CompletedTask;
9398
}
9499

src/ServiceControl/Operations/ErrorIngestion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ErrorIngestion(
7272

7373
public override async Task StartAsync(CancellationToken cancellationToken)
7474
{
75-
await watchdog.Start(() => applicationLifetime.StopApplication());
75+
await watchdog.Start(() => applicationLifetime.StopApplication(), cancellationToken);
7676
await base.StartAsync(cancellationToken);
7777
}
7878

0 commit comments

Comments
 (0)