Skip to content

Instantly share code, notes, and snippets.

@Tragetaschen
Last active August 29, 2015 14:17
Show Gist options
  • Save Tragetaschen/18e59107934333b8a3ea to your computer and use it in GitHub Desktop.
Save Tragetaschen/18e59107934333b8a3ea to your computer and use it in GitHub Desktop.
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);
}
}
}
[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
public void Configure(IApplicationShutdown applicationShutdown)
{
applicationShutdown.OnCancelKeyPress();
applicationShutdown.OnUnixSignals();
SystemdNotification.Do(); // Stay tuned for a new PR in Hosting
}
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