From c4a39c81636287e1635cd6e3d9e2a8105d6205df Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 26 Oct 2014 20:10:57 -0400 Subject: [PATCH] use processSortedSet instead of getting all users --- src/batch.js | 2 +- src/categories/delete.js | 6 +--- src/threadTools.js | 2 +- src/user/delete.js | 6 +--- src/user/jobs.js | 64 +++++++++++++++++++--------------------- 5 files changed, 34 insertions(+), 46 deletions(-) diff --git a/src/batch.js b/src/batch.js index 1c3c75627a..096c1d7147 100644 --- a/src/batch.js +++ b/src/batch.js @@ -44,7 +44,7 @@ var async = require('async'), done = true; return next(); } - process(err, ids, function(err) { + process(ids, function(err) { if (err) { return next(err); } diff --git a/src/categories/delete.js b/src/categories/delete.js index 6328817ecc..efc814bd27 100644 --- a/src/categories/delete.js +++ b/src/categories/delete.js @@ -9,11 +9,7 @@ var async = require('async'), module.exports = function(Categories) { Categories.purge = function(cid, callback) { - batch.processSortedSet('categories:' + cid + ':tid', function(err, tids, next) { - if (err) { - return callback(err); - } - + batch.processSortedSet('categories:' + cid + ':tid', function(tids, next) { async.eachLimit(tids, 10, function(tid, next) { threadTools.purge(tid, 0, next); }, next); diff --git a/src/threadTools.js b/src/threadTools.js index 22ceeafa07..ef5763e540 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -72,7 +72,7 @@ var winston = require('winston'), } ThreadTools.purge = function(tid, uid, callback) { - batch.processSortedSet('tid:' + tid + ':posts', function(err, pids, next) { + batch.processSortedSet('tid:' + tid + ':posts', function(pids, next) { async.eachLimit(pids, 10, posts.purge, next); }, {alwaysStartAt: 0}, function(err) { if (err) { diff --git a/src/user/delete.js b/src/user/delete.js index 8c34fe6542..3c7d3796f3 100644 --- a/src/user/delete.js +++ b/src/user/delete.js @@ -34,11 +34,7 @@ module.exports = function(User) { } function deleteSortedSetElements(set, deleteMethod, callback) { - batch.processSortedSet(set, function(err, ids, next) { - if (err) { - return callback(err); - } - + batch.processSortedSet(set, function(ids, next) { async.eachLimit(ids, 10, deleteMethod, next); }, {alwaysStartAt: 0}, callback); } diff --git a/src/user/jobs.js b/src/user/jobs.js index d06a8386cd..dbc6b1501b 100644 --- a/src/user/jobs.js +++ b/src/user/jobs.js @@ -10,7 +10,8 @@ var db = require('../database'), user = require('../user'), topics = require('../topics'), emailer = require('../emailer'), - meta = require('../meta'); + meta = require('../meta'), + batch = require('../batch'); module.exports = function(User) { User.startJobs = function() { @@ -27,42 +28,48 @@ module.exports = function(User) { return winston.log('[user/jobs] Did not send daily digests because subscription system is disabled.'); } - async.parallel({ - recent: function(next) { - topics.getLatestTopics(0, 0, 10, 'day', next); - }, - uids: function(next) { - db.getSortedSetRange('users:joindate', 0, -1, next); - } - }, function(err, data) { + topics.getLatestTopics(0, 0, 10, 'day', function(err, topics) { if (err) { return winston.error('[user/jobs] Could not send daily digests: ' + err.message); } - User.getMultipleUserSettings(data.uids, function(err, userSettings) { - if (err) { - return winston.error('[user/jobs] Could not send daily digests: ' + err.message); - } + batch.processSortedSet('users:joindate', function(uids, next) { + User.getMultipleUserSettings(uids, function(err, userSettings) { + if (err) { + return next(err); + } - var subscribed = userSettings.filter(function(setting) { - return setting.dailyDigestFreq === 'daily'; - }).map(function(setting) { - return setting.uid; - }); + var subscribed = userSettings.filter(function(setting) { + return setting.dailyDigestFreq === 'daily'; + }).map(function(setting) { + return setting.uid; + }); + + if (!subscribed.length) { + return next(); + } - sendEmails(subscribed, data.recent.topics); + sendEmails(subscribed, topics, next); + }); + }, function(err) { + if (err) { + winston.error('[user/jobs] Could not send daily digests: ' + err.message); + } else { + winston.info('[user/jobs] Daily Digests sent!'); + } }); }); }; - function sendEmails(uids, recentTopics) { + function sendEmails(uids, recentTopics, callback) { var now = new Date(); User.getMultipleUserFields(uids, ['uid', 'username', 'lastonline'], function(err, users) { if (err) { - return winston.error('[user/jobs] Could not send daily digests: ' + err.message); + winston.error('[user/jobs] Could not send daily digests: ' + err.message); + return callback(err); } - // Consider using eachLimit, but *only* if people complain about email relays choking -- otherwise we're ok. + async.eachLimit(users, 100, function(userObj, next) { user.notifications.getDailyUnread(userObj.uid, function(err, notifications) { if (err) { @@ -70,18 +77,14 @@ module.exports = function(User) { return next(err); } - // Remove expired notifications notifications = notifications.filter(Boolean); - // Turn relative URLs into absolute ones for(var i=0; i