Add --auto-exit CLI param
All checks were successful
continuous-integration/laminar-elessar Build 35 succeeded in 47 seconds .

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) public int Compare(Reminder x, Reminder y)
{ {
Console.WriteLine($"\t{x} / {y} = {x.Time.CompareTo(y.Time)}");
return 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.IO;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using RhinoReminds.Utilities;
using S22.Xmpp; using S22.Xmpp;
using S22.Xmpp.Client; using S22.Xmpp.Client;
using S22.Xmpp.Im; using S22.Xmpp.Im;
@ -20,6 +21,8 @@ namespace RhinoReminds
public string AvatarFilepath = string.Empty; public string AvatarFilepath = string.Empty;
public string PidFile = null; public string PidFile = null;
public bool ExitOnModify = false;
public string Password = null; public string Password = null;
} }
@ -59,6 +62,7 @@ namespace RhinoReminds
Console.WriteLine(" --domain {domain} Set the domain users are allowed to originate at. Defaults to any domain."); 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(" --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(" --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();
Console.WriteLine("Environment Variables:"); Console.WriteLine("Environment Variables:");
Console.WriteLine(" XMPP_JID The JID to login to"); Console.WriteLine(" XMPP_JID The JID to login to");
@ -83,6 +87,10 @@ namespace RhinoReminds
settings.PidFile = args[++i]; settings.PidFile = args[++i];
break; break;
case "--auto-exit":
settings.ExitOnModify = true;
break;
default: default:
Console.Error.WriteLine($"Error: Unknown argument '{args[i]}'."); Console.Error.WriteLine($"Error: Unknown argument '{args[i]}'.");
return 14; return 14;
@ -135,6 +143,10 @@ namespace RhinoReminds
Console.WriteLine("************************************"); Console.WriteLine("************************************");
Console.WriteLine($"[Program] Running {Version}"); Console.WriteLine($"[Program] Running {Version}");
if (settings.ExitOnModify) {
ExitWatcher.EnableExitOnModify();
}
ClientListener client = new ClientListener(settings.Jid, settings.Password) { ClientListener client = new ClientListener(settings.Jid, settings.Password) {
ReminderFilePath = settings.Filepath ReminderFilePath = settings.Filepath
}; };
@ -151,8 +163,7 @@ namespace RhinoReminds
} }
// Connect to the server & start listening // Connect to the server & start listening
// Make sure the program doesn't exit whilst we're connected // Make sure the program doesn't exit whilst we're connected
try try {
{
client.Start().Wait(); client.Start().Wait();
} catch (Exception) { } catch (Exception) {
// Ensure we tidy up after ourselves by deleting the PID file // Ensure we tidy up after ourselves by deleting the PID file

View file

@ -69,6 +69,7 @@
<Compile Include="SimpleXmppClient.cs" /> <Compile Include="SimpleXmppClient.cs" />
<Compile Include="CompareReminders.cs" /> <Compile Include="CompareReminders.cs" />
<Compile Include="Utilities\EmbeddedFiles.cs" /> <Compile Include="Utilities\EmbeddedFiles.cs" />
<Compile Include="Utilities\ExitWatcher.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <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 User=root
WorkingDirectory=/srv/kraggwapple WorkingDirectory=/srv/kraggwapple
ExecStart=/srv/kraggwapple/start_service.sh ExecStart=/srv/kraggwapple/start_service.sh
Restart=on-failure Restart=always
# Other Restart options: or always, on-abort, etc # Other Restart options: or always, on-abort, etc
# Delay restarts by 60 seconds # Delay restarts by 60 seconds
RestartSec=60 RestartSec=60

View file

@ -12,4 +12,4 @@ export XMPP_PASSWORD;
# Create the pidfile directory # Create the pidfile directory
mkdir /run/rhinoreminds; chmod 0700 /run/rhinoreminds; chown rhinoreminds:rhinoreminds /run/rhinoreminds; 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'