# HG changeset patch
# User John "Elwin" Edwards
# Date 1454033826 18000
# Node ID ea28353d620a9112c17278618db8a6b758fe50b9
# Parent  5041ec1fb426c8bd6fbc9af26749deb54a424042
Make sure games have saved and exited before stopping the server.

This should make the systemd version safe for production, though
it can't yet deal with a game that hangs and doesn't exit.

diff -r 5041ec1fb426 -r ea28353d620a rlgwebd
--- a/rlgwebd	Thu Jan 14 21:31:23 2016 -0500
+++ b/rlgwebd	Thu Jan 28 21:17:06 2016 -0500
@@ -279,6 +279,10 @@
   gamemux.emit('end', this.gname, this.pname);
   delete sessions[tag];
   tslog("Game %s ended.", tag);
+  /* Was that the last game? */
+  if (!allowlogin && Object.keys(sessions).length == 0) {
+    shutdown();
+  }
 };
 
 function DglSession(filename) {
@@ -1168,13 +1172,15 @@
       sessions[tag].close();
     }
     progressWatcher.stdin.end("\n");
-    setTimeout(shutdown, 2000);
+    if (Object.keys(sessions).length == 0)
+      setTimeout(shutdown, 500);
+    /* Otherwise wait for the games to end */
   }
 }
 
 process.on("exit", function () {
   for (var tag in sessions) {
-    sessions[tag].term.kill('SIGHUP');
+    sessions[tag].term.kill('SIGTERM');
   }
   tslog("Quitting...");
   return;