Last active
August 29, 2015 14:17
-
-
Save Tragetaschen/18e59107934333b8a3ea 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; | |
namespace Example | |
{ | |
public static class ApplicationShutdownExtensionMethods | |
{ | |
public static void OnCancelKeyPress(this IApplicationShutdown applicationShutdown) | |
{ | |
Console.WriteLine("To terminate, press CTRL+C"); | |
Console.CancelKeyPress += delegate | |
{ | |
applicationShutdown.RequestShutdown(); | |
Console.WriteLine("Received cancel key press"); | |
}; | |
} | |
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=Example | |
[Service] | |
WorkingDirectory=/path/to/kestrel/script | |
User=someone | |
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(IApplicationShutdown applicationShutdown) | |
{ | |
applicationShutdown.OnCancelKeyPress(); | |
applicationShutdown.OnUnixSignals(); | |
SystemdNotification.Do(); // Stay tuned for a new PR in Hosting | |
} |
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; | |
using Microsoft.Framework.Runtime; | |
namespace Example | |
{ | |
public static class SystemdNotification | |
{ | |
public static void Do() | |
{ | |
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