-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/Coding.Blog/Server/Configurations/ApplicationLifetimeConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Coding.Blog.Server.Configurations | ||
{ | ||
/// <summary> | ||
/// Controls how long the application should stay alive after receiving a SIGTERM | ||
/// signal. This prevents in-flight traffic from being dropped during deployments or | ||
/// application restarts. | ||
/// </summary> | ||
internal class ApplicationLifetimeConfiguration | ||
{ | ||
public static string Key => "ApplicationLifetime"; | ||
|
||
public int ApplicationStoppingGracePeriodSeconds { get; set; } | ||
|
||
public int ApplicationShutdownTimeoutSeconds { get; set; } | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Coding.Blog/Server/HostedServices/ApplicationLifetimeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Coding.Blog.Server.Configurations; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Coding.Blog.Server.HostedServices | ||
{ | ||
internal class ApplicationLifetimeService : IHostedService | ||
{ | ||
private readonly IHostApplicationLifetime _hostApplicationLifetime; | ||
private readonly TimeSpan _applicationStoppingGracePeriod; | ||
|
||
public ApplicationLifetimeService( | ||
IHostApplicationLifetime hostApplicationLifetime, | ||
ApplicationLifetimeConfiguration configuration) | ||
{ | ||
_hostApplicationLifetime = hostApplicationLifetime; | ||
_applicationStoppingGracePeriod = TimeSpan.FromSeconds(configuration.ApplicationStoppingGracePeriodSeconds); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_hostApplicationLifetime.ApplicationStopping.Register(() => Thread.Sleep(_applicationStoppingGracePeriod)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
{ | ||
"AllowedHosts": "*", | ||
"ApplicationLifetime": { | ||
"ApplicationStoppingGracePeriodSeconds": 30, | ||
"ApplicationShutdownTimeoutSeconds": 45 | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} | ||
} |