Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Handle SIGTERMs for graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym committed Nov 1, 2016
1 parent 0f1eac5 commit c6400a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private RequestDelegate BuildApplication()
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_5 || NETSTANDARD1_3
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).GetTypeInfo().Assembly;
var assemblyVersion = new AssemblyName(systemRuntimeAssembly.FullName).Version.ToString();
var clrVersion = assemblyVersion;
Expand Down
28 changes: 24 additions & 4 deletions src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Threading;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
#if NETSTANDARD1_5
using System.Runtime.Loader;
#endif

namespace Microsoft.AspNetCore.Hosting
{
Expand All @@ -16,18 +19,26 @@ public static class WebHostExtensions
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Run(this IWebHost host)
{
AutoResetEvent done = new AutoResetEvent(false);
using (var cts = new CancellationTokenSource())
{
#if NETSTANDARD1_5
var applicationLifetime = host.Services.GetService<IApplicationLifetime>();
AssemblyLoadContext.Default.Unloading += context => {
Shutdown(cts);

done.WaitOne();
};
#endif
Console.CancelKeyPress += (sender, eventArgs) =>
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
Shutdown(cts);

// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
done.WaitOne();
};

host.Run(cts.Token, "Application started. Press Ctrl+C to shut down.");
done.Set();
}
}

Expand Down Expand Up @@ -76,5 +87,14 @@ private static void Run(this IWebHost host, CancellationToken token, string shut
applicationLifetime.ApplicationStopping.WaitHandle.WaitOne();
}
}

private static void Shutdown(CancellationTokenSource cts)
{
if (!cts.IsCancellationRequested)
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
}
}
}
}
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.Hosting/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.TestHost/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"System.Net.Http": ""
}
},
"netstandard1.3": {
"netstandard1.5": {
"dependencies": {
"System.Diagnostics.Contracts": "4.3.0-*"
}
Expand Down

0 comments on commit c6400a1

Please sign in to comment.