From: Benjamin Auder Date: Wed, 12 Feb 2020 13:48:46 +0000 (+0100) Subject: Soften removal conditions in CRON tasks (no challenge removal at all) X-Git-Url: https://git.auder.net/pieces/current/img/cross.svg?a=commitdiff_plain;h=fd41e587ca01d709551e0cc04bb94fb5e769fe97;p=vchess.git Soften removal conditions in CRON tasks (no challenge removal at all) --- diff --git a/server/bin/www b/server/bin/www index b3a2c732..9fd23a44 100755 --- a/server/bin/www +++ b/server/bin/www @@ -27,12 +27,10 @@ var server = http.createServer(app); var cron = require('node-cron'); var UserModel = require("../models/User"); -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 + // Remove unlogged users and unstarted games every 24h UserModel.cleanUsersDb(); - ChallengeModel.removeOld(); GameModel.cleanGamesDb(); }); diff --git a/server/models/Challenge.js b/server/models/Challenge.js index 75cab3ab..4aae427a 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -98,28 +98,6 @@ const ChallengeModel = }); }); }, - - // 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, added " + - "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; diff --git a/server/models/Game.js b/server/models/Game.js index 16c050cd..70f32be2 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -277,28 +277,21 @@ const GameModel = const day = 86400000; db.serialize(function() { let query = - "SELECT id,score,created " + + "SELECT id,created " + "FROM Games "; db.all(query, (err,games) => { games.forEach(g => { query = - "SELECT max(played) AS lastMaj " + + "SELECT count(*) as nbMoves, max(played) AS lastMaj " + "FROM Moves " + "WHERE gid = " + g.id; - db.get(query, (err2,updated) => { - if (!updated.lastMaj) + db.get(query, (err2,mstats) => { + // Remove games still not really started, + // with no action in the last 3 months: + if ((mstats.nbMoves == 0 && tsNow - g.created > 91*day) || + (mstats.nbMoves == 1 && tsNow - mstats.lastMaj > 91*day)) { - if (tsNow - g.created > 7*day) - return GameModel.remove(g.id); - } - else //at least one move - { - const lastMaj = updated.lastMaj; - if (g.score != "*" && tsNow - lastMaj > 7*day || - g.score == "*" && tsNow - lastMaj > 91*day) - { - GameModel.remove(g.id); - } + return GameModel.remove(g.id); } }); });