Handle SIGTERMs for graceful shutdown (#876)

This commit is contained in:
Pavel Krymets 2016-11-02 09:29:44 -07:00 committed by GitHub
parent a29ceeb9e8
commit 2bddba8f90
3 changed files with 29 additions and 4 deletions

View File

@ -176,7 +176,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
var model = new ErrorPageModel();
var runtimeType = Microsoft.Extensions.Internal.RuntimeEnvironment.RuntimeType;
model.RuntimeDisplayName = (runtimeType == "CoreCLR") ? ".NET Core" : runtimeType == "CLR" ? ".NET Framework" : "Mono";
#if NETSTANDARD1_3
#if NETSTANDARD1_3 || NETSTANDARD1_5
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).GetTypeInfo().Assembly;
var assemblyVersion = new AssemblyName(systemRuntimeAssembly.FullName).Version.ToString();
var clrVersion = assemblyVersion;

View File

@ -2,6 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
#if NETSTANDARD1_5
using System.Reflection;
using System.Runtime.Loader;
#endif
using System.Threading;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
@ -16,18 +20,33 @@ namespace Microsoft.AspNetCore.Hosting
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Run(this IWebHost host)
{
var done = new ManualResetEventSlim(false);
using (var cts = new CancellationTokenSource())
{
Action shutdown = () =>
{
if (!cts.IsCancellationRequested)
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
}
done.Wait();
};
#if NETSTANDARD1_5
var assemblyLoadContext = AssemblyLoadContext.GetLoadContext(typeof(WebHostExtensions).GetTypeInfo().Assembly);
assemblyLoadContext.Unloading += context => shutdown();
#endif
Console.CancelKeyPress += (sender, eventArgs) =>
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
shutdown();
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
};
host.Run(cts.Token, "Application started. Press Ctrl+C to shut down.");
done.Set();
}
}

View File

@ -69,6 +69,12 @@
"dependencies": {
"System.Diagnostics.StackTrace": "4.3.0-*"
}
},
"netstandard1.5": {
"dependencies": {
"System.Diagnostics.StackTrace": "4.3.0-*",
"System.Runtime.Loader": "4.3.0-*"
}
}
},
"tools": {