Commit 509aa33 1 parent b347fb6 commit 509aa33 Copy full SHA for 509aa33
File tree 2 files changed +52
-0
lines changed
src/Digdir.Domain.Dialogporten.WebApi
2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -75,6 +75,11 @@ static void BuildAndRun(string[] args)
75
75
. ValidateFluently ( )
76
76
. ValidateOnStart ( ) ;
77
77
78
+ builder . Services . AddSingleton < IHostLifetime > ( sp => new DelayedShutdownHostLifetime (
79
+ sp . GetRequiredService < IHostApplicationLifetime > ( ) ,
80
+ TimeSpan . FromSeconds ( 10 )
81
+ ) ) ;
82
+
78
83
var thisAssembly = Assembly . GetExecutingAssembly ( ) ;
79
84
80
85
builder . Services
You can’t perform that action at this time.
0 commit comments