Implement cleaning methods (CRON tasks)
authorBenjamin Auder <benjamin.auder@somewhere>
Thu, 16 Jan 2020 17:46:59 +0000 (18:46 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Thu, 16 Jan 2020 17:46:59 +0000 (18:46 +0100)
server/bin/www
server/db/create.sql
server/models/Challenge.js
server/models/Game.js
server/models/User.js

index 94f39e1..4475199 100755 (executable)
@@ -31,8 +31,8 @@ var ChallengeModel = require("../models/Challenge");
 var GameModel = require("../models/Game");
 cron.schedule('0 0 0 * * *', function() {
        // Remove some old users, challenges and games every 24h
-       UserModel.cleanUsersDb(); //TODO: write this
-       ChallengeModel.removeOld(); //TODO: this too
+       UserModel.cleanUsersDb();
+       ChallengeModel.removeOld();
        GameModel.cleanGamesDb();
 });
 
index a8c8d68..9a8f785 100644 (file)
@@ -13,6 +13,7 @@ create table Users (
   loginToken varchar,
   loginTime datetime,
   sessionToken varchar,
+  created datetime,
   notify boolean
 );
 
index ba805aa..01de6b5 100644 (file)
@@ -92,24 +92,28 @@ const ChallengeModel =
       });
     });
   },
-}
 
-// TODO: adapt
-// Remove challenges older than 1 month, and 1to1 older than 36h
-//exports.removeOld = function()
-//{
-//     var tsNow = new Date().getTime();
-//     // 86400000 = 24 hours in milliseconds
-//     var day = 86400000;
-//     db.challenges.find({}, (err,challengeArray) => {
-//             challengeArray.forEach( c => {
-//                     if (c._id.getTimestamp() + 30*day < tsNow //automatch
-//                             || (!!c.to && c._id.getTimestamp() + 1.5*day < tsNow)) //1 to 1
-//                     {
-//                             db.challenges.remove({"_id": c._id});
-//                     }
-//             });
-//     });
-//}
+  // Remove challenges older than 1 month, and 1to1 older than 2 days
+  removeOld: function()
+  {
+    const tsNow = Date.now();
+    // 86400000 = 24 hours in milliseconds
+    const day = 86400000;
+    db.serialize(function() {
+      const query =
+        "SELECT id, target " +
+        "FROM Challenges";
+      db.all(query, (err, challenges) => {
+        challenges.forEach(c => {
+          if ((!c.target && tsNow - c.added > 30*day) ||
+            (!!c.target && tsNow - c.added > 2*day))
+          {
+            db.run("DELETE FROM CHallenges WHERE id = " + c.id);
+          }
+        });
+      });
+    });
+  },
+}
 
 module.exports = ChallengeModel;
index 3d2184b..77bbd27 100644 (file)
@@ -8,6 +8,7 @@ var db = require("../utils/database");
  *   fen: varchar (current position)
  *   timeControl: string
  *   score: varchar (result)
+ *   created: datetime
  *
  * Structure table Players:
  *   gid: ref game id
index c2e7883..ee4b056 100644 (file)
@@ -12,6 +12,7 @@ var sendEmail = require('../utils/mailer');
  *   loginTime: datetime (validity)
  *   sessionToken: token in cookies for authentication
  *   notify: boolean (send email notifications for corr games)
+ *   created: datetime
  */
 
 const UserModel =
@@ -40,8 +41,8 @@ const UserModel =
                db.serialize(function() {
                        const insertQuery =
                                "INSERT INTO Users " +
-                               "(name, email, notify) VALUES " +
-                               "('" + name + "', '" + email + "', " + notify + ")";
+                               "(name, email, notify, created) VALUES " +
+                               "('" + name + "', '" + email + "', " + notify + "," + Date.now() + ")";
                        db.run(insertQuery, err => {
                                if (!!err)
                                        return callback(err);
@@ -139,26 +140,29 @@ const UserModel =
         res.json(err || {});
       });
     });
-  }
-}
+  },
+
+  ////////////
+  // CLEANING
 
-// TODO: adapt
-//exports.cleanUsersDb = function()
-//{
-//     var tsNow = new Date().getTime();
-//     // 86400000 = 24 hours in milliseconds
-//     var day = 86400000;
-//
-//     db.users.find({}, (err,userArray) => {
-//             userArray.forEach( u => {
-//                     if ((u.sessionTokens.length==0 &&
-//                                     u._id.getTimestamp().getTime() + day < tsNow) //unlogged
-//                             || u.updated + 365*day < tsNow) //inactive for one year
-//                     {
-//                             db.users.remove({"_id": u._id});
-//                     }
-//             });
-//     });
-//}
+  cleanUsersDb: function()
+  {
+    const tsNow = Date.now();
+    // 86400000 = 24 hours in milliseconds
+    const day = 86400000;
+    db.serialize(function() {
+      const query =
+        "SELECT id, sessionToken, created " +
+        "FROM Users";
+      db.all(query, (err, users) => {
+        users.forEach(u => {
+          // Remove unlogged users for >1 day
+          if (!u.sessionToken && tsNow - u.created > day)
+            db.run("DELETE FROM Users WHERE id = " + u.id);
+        });
+      });
+    });
+  },
+}
 
 module.exports = UserModel;