notifications.prune

run every hour
dont get more than 500 notifications
v1.18.x
barisusakli 11 years ago
parent 273db693e7
commit 2252ab7479

@ -41,6 +41,10 @@ module.exports = function(db, module) {
db.del(key, callback);
};
module.deleteAll = function(keys, callback) {
async.each(keys, module.delete, callback);
};
module.get = function(key, callback) {
db.get(key, function(err, value) {
callback(false, value);

@ -74,6 +74,11 @@ module.exports = function(db, module) {
db.collection('objects').remove({_key:key}, callback);
};
module.deleteAll = function(keys, callback) {
callback = callback || helpers.noop;
db.collection('objects').remove({_key: {$in: keys}}, callback);
};
module.get = function(key, callback) {
module.getObjectField(key, 'value', callback);
};

@ -73,6 +73,14 @@ module.exports = function(redisClient, module) {
redisClient.del(key, callback);
};
module.deleteAll = function(keys, callback) {
var multi = redisClient.multi();
for(var i=0; i<keys.length; ++i) {
multi.del(keys[i]);
}
multi.exec(callback);
};
module.get = function(key, callback) {
redisClient.get(key, callback);
};

@ -5,6 +5,7 @@ var async = require('async'),
cron = require('cron').CronJob,
nconf = require('nconf'),
S = require('string'),
_ = require('underscore'),
db = require('./database'),
utils = require('../public/src/utils'),
@ -20,7 +21,7 @@ var async = require('async'),
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.init] Registering jobs.');
}
new cron('0 0 * * *', Notifications.prune, null, true);
new cron('0 * * * *', Notifications.prune, null, true);
};
Notifications.get = function(nid, callback) {
@ -297,6 +298,8 @@ var async = require('async'),
if (err) {
return winston.error(err.message);
}
var totalNidCount = nids.length;
nids = _.sortBy(nids, function(num) { return parseInt(num, 10); }).slice(0, 500);
var keys = nids.map(function(nid) {
return 'notifications:' + nid;
@ -307,25 +310,26 @@ var async = require('async'),
return winston.error(err.message);
}
var expiredNids = notifs.filter(function(notif) {
return notif && parseInt(notif.datetime, 10) < cutoffTime;
}).map(function(notif) {
return notif.nid;
var expiredNids = nids.filter(function(nids, index) {
return !notifs[index].nid || parseInt(notifs[index].datetime, 10) < cutoffTime;
}).filter(Boolean);
keys = expiredNids.map(function(nid) {
return 'notifications:' + nid;
});
async.eachLimit(expiredNids, 50, function(nid, next) {
numPruned = expiredNids.length;
events.log('Notification pruning. Total nids = ' + totalNidCount + '. Expired Nids = ' + numPruned);
async.parallel([
function(next) {
db.setRemove('notifications', nid, next);
db.setRemove('notifications', expiredNids, next);
},
function(next) {
db.delete('notifications:' + nid, next);
db.deleteAll(keys, next);
}
], function(err) {
numPruned++;
next(err);
});
}, function(err) {
if (err) {
return winston.error('Encountered error pruning notifications: ' + err.message);
}

Loading…
Cancel
Save