Refactor utils and organize routes/schema
This commit is contained in:
21
utils/cron.js
Normal file
21
utils/cron.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const config = require('../config');
|
||||
let Session = require('../schema/session.model');
|
||||
const maxSessionLength = config.maxSessionLength;
|
||||
|
||||
// purgeSessions() purge sessions that have existed for longer than maxSessionLength
|
||||
const purgeSessions = () => {
|
||||
console.log("Purging old sessions...");
|
||||
Session.find({}, (err, arr) => {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
let timeDifference = new Date().getTime() - arr[i].date;
|
||||
let dayDifference = timeDifference / (1000 * 3600 * 24);
|
||||
if (dayDifference > maxSessionLength) {
|
||||
arr[i].delete().catch(e => {
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.purgeSessions = purgeSessions;
|
||||
Reference in New Issue
Block a user