Created
August 17, 2015 07:23
-
-
Save Tragetaschen/435c5bb2062c29753070 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Threading.Tasks; | |
using Microsoft.Framework.Runtime; | |
using Mono.Unix; | |
public static class ApplicationShutdownExtensionMethods | |
{ | |
public static void OnUnixSignals(this IApplicationShutdown applicationShutdown) | |
{ | |
Task.Factory.StartNew(() => | |
{ | |
var sigInt = new UnixSignal(Mono.Unix.Native.Signum.SIGINT); | |
var sigTerm = new UnixSignal(Mono.Unix.Native.Signum.SIGTERM); | |
Console.WriteLine("To terminate, send SIGINT or SIGTERM to the process"); | |
UnixSignal.WaitAny(new[] { sigInt, sigTerm }); | |
Console.WriteLine("Received signal"); | |
applicationShutdown.RequestShutdown(); | |
}, TaskCreationOptions.LongRunning); | |
} | |
} |
This file contains hidden or 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
[Unit] | |
Description=ASP.NET 5 application | |
[Service] | |
WorkingDirectory=/opt/something | |
User=not-root | |
ExecStart=/bin/bash kestrel | |
Environment=TEMP=/tmp | |
Type=notify | |
NotifyAccess=main | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or 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
public void Configure( | |
IApplicationLifetime applicationLifetime, | |
IApplicationShutdown applicationShutdown | |
) | |
{ | |
applicationLifetime.NotifySystemd(); | |
applicationShutdown.OnUnixSignals(); | |
} |
This file contains hidden or 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
using System.Runtime.InteropServices; | |
using Microsoft.AspNet.Hosting; | |
public static class SystemdStartupNotifier | |
{ | |
public static void NotifySystemd(this IApplicationLifetime applicationLifetime) | |
{ | |
applicationLifetime.ApplicationStarted.Register(handler); | |
} | |
private static void handler() | |
{ | |
sd_notify(0, "READY=1"); | |
} | |
[DllImport("libsystemd.so.0")] | |
private static extern int sd_notify(int unset_environment, string state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment