From 8bd6f854786c86a19163ce8039c7addc65b5def2 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 14 Feb 2014 10:49:43 -0500 Subject: [PATCH] first pass, #999 --- src/notifications.js | 86 +++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 58 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index e1d5f3ed35..c614dd0272 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -212,70 +212,40 @@ var async = require('async'), var cutoffTime = cutoff.getTime(); - async.parallel({ - "inboxes": function(next) { - db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { - if(err) { - return next(err); + db.getSetMembers('notifications', function(err, nids) { + async.filter(nids, function(nid, next) { + db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) { + if (parseInt(datetime, 10) < cutoffTime) { + next(true); + } else { + next(false); } - uids = uids.map(function(uid) { - return 'uid:' + uid + ':notifications:unread'; - }); - next(null, uids); - }); - }, - "expiredNids": function(next) { - db.getSetMembers('notifications', function(err, nids) { - async.filter(nids, function(nid, next) { - db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) { - if (parseInt(datetime, 10) < cutoffTime) { - next(true); - } else { - next(false); - } - }); - }, function(expiredNids) { - next(null, expiredNids); - }); }); - } - }, function(err, results) { - if(err) { - if (process.env.NODE_ENV === 'development') { - winston.error('[notifications.prune] Ran into trouble pruning expired notifications. Stack trace to follow.'); - winston.error(err.stack); - } - return; - } - - async.eachSeries(results.expiredNids, function(nid, next) { - - db.sortedSetsScore(results.inboxes, nid, function(err, results) { - if(err) { - return next(err); - } - - // If the notification is not present in any inbox, delete it altogether - var expired = results.every(function(present) { - return present === null; - }); - - if (expired) { - destroy(nid); + }, function(expiredNids) { + async.each(expiredNids, function(nid, next) { + async.parallel([ + function(next) { + db.setRemove('notifications', nid, next) + }, + function(next) { + db.delete('notifications:' + nid, next) + } + ], function(err) { numPruned++; + next(err); + }); + }, function(err) { + if (!err) { + if (process.env.NODE_ENV === 'development') { + winston.info('[notifications.prune] Notification pruning completed. ' + numPruned + ' expired notification' + (numPruned !== 1 ? 's' : '') + ' removed.'); + } + var diff = process.hrtime(start); + events.log('Pruning notifications took : ' + (diff[0] * 1e3 + diff[1] / 1e6) + ' ms'); + } else { + winston.error('Encountered error pruning notifications: ' + err.message); } - - next(); }); - }, function(err) { - if (process.env.NODE_ENV === 'development') { - winston.info('[notifications.prune] Notification pruning completed. ' + numPruned + ' expired notification' + (numPruned !== 1 ? 's' : '') + ' removed.'); - } - var diff = process.hrtime(start); - events.log('Pruning notifications took : ' + (diff[0] * 1e3 + diff[1] / 1e6) + ' ms'); - }); - }); };