Skip to content

Commit

Permalink
feat: dont load all subscribers at once
Browse files Browse the repository at this point in the history
increase batch to 500
  • Loading branch information
barisusakli committed Jul 21, 2020
1 parent 23a9a33 commit 2a5f8ab
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/user/digest.js
Expand Up @@ -29,13 +29,14 @@ Digest.execute = async function (payload) {
return;
}
try {
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. Sending emails; this may take some time...');
await Digest.send({
interval: payload.interval,
subscribers: subscribers,
});
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. Sending emails; this may take some time...');
winston.info('[user/jobs] Digest (' + payload.interval + ') complete.');
} catch (err) {
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err.stack);
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')\n' + err.stack);
throw err;
}
};
Expand Down Expand Up @@ -81,7 +82,10 @@ Digest.getSubscribers = async function (interval) {
});
subUids = await user.bans.filterBanned(subUids);
subscribers = subscribers.concat(subUids);
}, { interval: 1000 });
}, {
interval: 1000,
batch: 500,
});

const results = await plugins.fireHook('filter:digest.subscribers', {
interval: interval,
Expand All @@ -91,15 +95,13 @@ Digest.getSubscribers = async function (interval) {
};

Digest.send = async function (data) {
var emailsSent = 0;
let emailsSent = 0;
if (!data || !data.subscribers || !data.subscribers.length) {
return emailsSent;
}
const now = new Date();

const users = await user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline']);

async.eachLimit(users, 100, async function (userObj) {
await async.eachLimit(data.subscribers, 100, async function (uid) {
const userObj = await user.getUserFields(uid, ['uid', 'username', 'userslug', 'lastonline']);
let [notifications, topicsData] = await Promise.all([
user.notifications.getUnreadInterval(userObj.uid, data.interval),
getTermTopics(data.interval, userObj.uid, 0, 9),
Expand Down Expand Up @@ -128,6 +130,7 @@ Digest.send = async function (data) {
return topicObj;
});
emailsSent += 1;
const now = Date.now();
try {
await emailer.send('digest', userObj.uid, {
subject: '[[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
Expand All @@ -145,9 +148,8 @@ Digest.send = async function (data) {
if (data.interval !== 'alltime') {
await db.sortedSetAdd('digest:delivery', now.getTime(), userObj.uid);
}
}, function () {
winston.info('[user/jobs] Digest (' + data.interval + ') sending completed. ' + emailsSent + ' emails sent.');
});
winston.info('[user/jobs] Digest (' + data.interval + ') sending completed. ' + emailsSent + ' emails sent.');
};

Digest.getDeliveryTimes = async (start, stop) => {
Expand Down

0 comments on commit 2a5f8ab

Please sign in to comment.