Skip to content

Commit 509aa33

Browse files
authored
fix(web-api): ensure graceful shutdown (#1784)
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> Adjusting shutdown delay. Ref discussion in this thread: https://digdir.slack.com/archives/C07R312076E/p1738364898052139 Plucked from this blogpost: https://github.com/dotnet/dotnet-docker/blob/main/samples/kubernetes/graceful-shutdown/graceful-shutdown.md#adding-a-shutdown-delay ## Related Issue(s) - #N/A ## Verification - [ ] **Your** code builds clean without any errors or warnings - [ ] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out) ## Documentation - [ ] Documentation is updated (either in `docs`-directory, Altinnpedia or a separate linked PR in [altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if applicable)
1 parent b347fb6 commit 509aa33

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Digdir.Domain.Dialogporten.WebApi;
4+
5+
public sealed class DelayedShutdownHostLifetime : IHostLifetime, IDisposable
6+
{
7+
private readonly IHostApplicationLifetime _applicationLifetime;
8+
private readonly TimeSpan _delay;
9+
private IEnumerable<IDisposable>? _disposables;
10+
11+
public DelayedShutdownHostLifetime(IHostApplicationLifetime applicationLifetime, TimeSpan delay)
12+
{
13+
_applicationLifetime = applicationLifetime;
14+
_delay = delay;
15+
}
16+
17+
public Task StopAsync(CancellationToken cancellationToken)
18+
{
19+
return Task.CompletedTask;
20+
}
21+
22+
public Task WaitForStartAsync(CancellationToken cancellationToken)
23+
{
24+
_disposables = new IDisposable[]
25+
{
26+
PosixSignalRegistration.Create(PosixSignal.SIGINT, HandleSignal),
27+
PosixSignalRegistration.Create(PosixSignal.SIGQUIT, HandleSignal),
28+
PosixSignalRegistration.Create(PosixSignal.SIGTERM, HandleSignal)
29+
};
30+
return Task.CompletedTask;
31+
}
32+
33+
private void HandleSignal(PosixSignalContext ctx)
34+
{
35+
ctx.Cancel = true;
36+
Task.Delay(_delay).ContinueWith(t => _applicationLifetime.StopApplication());
37+
}
38+
39+
public void Dispose()
40+
{
41+
foreach (var disposable in _disposables ?? Enumerable.Empty<IDisposable>())
42+
{
43+
disposable.Dispose();
44+
}
45+
GC.SuppressFinalize(this);
46+
}
47+
}

src/Digdir.Domain.Dialogporten.WebApi/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ static void BuildAndRun(string[] args)
7575
.ValidateFluently()
7676
.ValidateOnStart();
7777

78+
builder.Services.AddSingleton<IHostLifetime>(sp => new DelayedShutdownHostLifetime(
79+
sp.GetRequiredService<IHostApplicationLifetime>(),
80+
TimeSpan.FromSeconds(10)
81+
));
82+
7883
var thisAssembly = Assembly.GetExecutingAssembly();
7984

8085
builder.Services

0 commit comments

Comments
 (0)