Add --auto-exit CLI param
continuous-integration/laminar-elessar Build 35 succeeded in 47 seconds . Details

This commit is contained in:
Starbeamrainbowlabs 2019-03-28 23:04:52 +00:00
parent dced63f02c
commit eeea59eaff
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
6 changed files with 59 additions and 9 deletions

View File

@ -11,7 +11,6 @@ namespace RhinoReminds
public int Compare(Reminder x, Reminder y)
{
Console.WriteLine($"\t{x} / {y} = {x.Time.CompareTo(y.Time)}");
return x.Time.CompareTo(y.Time);
}
}

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using RhinoReminds.Utilities;
using S22.Xmpp;
using S22.Xmpp.Client;
using S22.Xmpp.Im;
@ -20,6 +21,8 @@ namespace RhinoReminds
public string AvatarFilepath = string.Empty;
public string PidFile = null;
public bool ExitOnModify = false;
public string Password = null;
}
@ -54,11 +57,12 @@ namespace RhinoReminds
Console.WriteLine(" mono RhinoReminds.exe {options}");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -h --help Show this message");
Console.WriteLine($" -f --file Specify where to save reminders (default: {settings.Filepath})");
Console.WriteLine(" -h --help Show this message");
Console.WriteLine($" -f --file Specify where to save reminders (default: {settings.Filepath})");
Console.WriteLine(" --domain {domain} Set the domain users are allowed to originate at. Defaults to any domain.");
Console.WriteLine(" --avatar Update the XMPP account's avatar to the specified image. By default the avatar is not updated.");
Console.WriteLine(" --pidfile Save our process ID to the specified file, and delete it on exit");
Console.WriteLine(" --avatar Update the XMPP account's avatar to the specified image. By default the avatar is not updated.");
Console.WriteLine(" --pidfile Save our process ID to the specified file, and delete it on exit");
Console.WriteLine(" --auto-exit Watch for changes to the executable on disk and automatically quit with an exit code of 0 if a modification is detected. Useful for integration with a service manager and continuous deployment.");
Console.WriteLine();
Console.WriteLine("Environment Variables:");
Console.WriteLine(" XMPP_JID The JID to login to");
@ -83,6 +87,10 @@ namespace RhinoReminds
settings.PidFile = args[++i];
break;
case "--auto-exit":
settings.ExitOnModify = true;
break;
default:
Console.Error.WriteLine($"Error: Unknown argument '{args[i]}'.");
return 14;
@ -135,6 +143,10 @@ namespace RhinoReminds
Console.WriteLine("************************************");
Console.WriteLine($"[Program] Running {Version}");
if (settings.ExitOnModify) {
ExitWatcher.EnableExitOnModify();
}
ClientListener client = new ClientListener(settings.Jid, settings.Password) {
ReminderFilePath = settings.Filepath
};
@ -151,8 +163,7 @@ namespace RhinoReminds
}
// Connect to the server & start listening
// Make sure the program doesn't exit whilst we're connected
try
{
try {
client.Start().Wait();
} catch (Exception) {
// Ensure we tidy up after ourselves by deleting the PID file

View File

@ -69,6 +69,7 @@
<Compile Include="SimpleXmppClient.cs" />
<Compile Include="CompareReminders.cs" />
<Compile Include="Utilities\EmbeddedFiles.cs" />
<Compile Include="Utilities\ExitWatcher.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

View File

@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace RhinoReminds.Utilities
{
public static class ExitWatcher
{
private static int restartDelayMs = 5 * 1000;
private static string EntryExecutablePath => Assembly.GetEntryAssembly().Location;
private static FileSystemWatcher watcher;
private static bool exitSequenceStarted = false;
public static void EnableExitOnModify() {
Console.WriteLine("[Program/ExitWatcher] Enabling auto-exit on modification.");
watcher = new FileSystemWatcher(Path.GetDirectoryName(EntryExecutablePath), "*.exe");
watcher.Changed += (object sender, FileSystemEventArgs e) => doExit();
watcher.EnableRaisingEvents = true;
}
private static void doExit() {
// Don't bother if we're already exiting on a different thread or something else weird
if (exitSequenceStarted) return;
exitSequenceStarted = true;
watcher.EnableRaisingEvents = false; // We're only interested in the first event raised
Console.WriteLine($"[Program/ExitWatcher] Modification detected, waiting {restartDelayMs}ms...");
Thread.Sleep(restartDelayMs);
Console.WriteLine($"[Program/ExitWatcher] Wait complete, exiting with code 0");
Environment.Exit(0);
}
}
}

View File

@ -14,7 +14,7 @@ PIDFile=/run/rhinoreminds/rhinoreminds.pid
User=root
WorkingDirectory=/srv/kraggwapple
ExecStart=/srv/kraggwapple/start_service.sh
Restart=on-failure
Restart=always
# Other Restart options: or always, on-abort, etc
# Delay restarts by 60 seconds
RestartSec=60

View File

@ -12,4 +12,4 @@ export XMPP_PASSWORD;
# Create the pidfile directory
mkdir /run/rhinoreminds; chmod 0700 /run/rhinoreminds; chown rhinoreminds:rhinoreminds /run/rhinoreminds;
sudo -E -u rhinoreminds bash -c '/usr/bin/mono ../bin/RhinoReminds.exe --domain starbeamrainbowlabs.com --avatar avatar.png & echo "$!" >/run/rhinoreminds/rhinoreminds.pid; disown'
sudo -E -u rhinoreminds bash -c '/usr/bin/mono ../bin/RhinoReminds.exe --auto-exit --domain starbeamrainbowlabs.com --avatar avatar.png & echo "$!" >/run/rhinoreminds/rhinoreminds.pid; disown'