diff rlgwebd.js @ 36:a0387f112bcf

RLG-Web: autosave idle games. If a game has been idle for too long, close it.
author John "Elwin" Edwards <elwin@sdf.org>
date Wed, 06 Jun 2012 10:01:18 -0700
parents 57f4b36ef06b
children 353be34de307
line wrap: on
line diff
--- a/rlgwebd.js	Tue Jun 05 22:33:41 2012 -0700
+++ b/rlgwebd.js	Wed Jun 06 10:01:18 2012 -0700
@@ -20,6 +20,7 @@
 var dropToGID = 501;
 var serveStaticRoot = "/var/www/"; // inside the chroot
 var passwdfile = "/dgldir/dgl-login";
+var playtimeout = 3600000; // Idle time before games are autosaved, in ms
 
 /* Global state */
 var sessions = {};
@@ -201,7 +202,7 @@
   };
   this.cleanup = function () {
     /* Call this when the child is dead. */
-    if (this.alive)
+    if (ss.alive)
       return;
     ss.record.end();
     /* Give the client a chance to read any leftover data. */
@@ -318,6 +319,23 @@
   return true;
 }
 
+function reaper() {
+  var now = new Date();
+  function reapcheck(session) {
+    if (!session.alive)
+      return;
+    fs.fstat(session.record.fd, function (err, stats) {
+      if (!err && now - stats.mtime > playtimeout) {
+        tslog("Reaping %s", session.sessid);
+        session.close();
+      }
+    });
+  }
+  for (var sessid in sessions) {
+    reapcheck(sessions[sessid]);
+  }
+}
+
 function login(req, res, formdata) {
   if (!allowlogin) {
     sendError(res, 6, null);
@@ -754,5 +772,6 @@
   httpServer = http.createServer(webHandler);
   httpServer.listen(httpPort);
   tslog('rlgwebd running on port %d', httpPort); 
+  setInterval(reaper, playtimeout / 4);
 });