first pass, #999

v1.18.x
Julian Lam 11 years ago
parent 28731e2dbd
commit 8bd6f85478

@ -212,70 +212,40 @@ var async = require('async'),
var cutoffTime = cutoff.getTime(); var cutoffTime = cutoff.getTime();
async.parallel({ db.getSetMembers('notifications', function(err, nids) {
"inboxes": function(next) { async.filter(nids, function(nid, next) {
db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
if(err) { if (parseInt(datetime, 10) < cutoffTime) {
return next(err); 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(expiredNids) {
}, function(err, results) { async.each(expiredNids, function(nid, next) {
if(err) { async.parallel([
if (process.env.NODE_ENV === 'development') { function(next) {
winston.error('[notifications.prune] Ran into trouble pruning expired notifications. Stack trace to follow.'); db.setRemove('notifications', nid, next)
winston.error(err.stack); },
} function(next) {
return; db.delete('notifications:' + nid, next)
} }
], function(err) {
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);
numPruned++; 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');
}); });
}); });
}; };

Loading…
Cancel
Save