From c7ff4c9e933a146669e3465a444a41e2dc2cbf6b Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 12 Aug 2020 17:01:59 +0100 Subject: [PATCH] Moved email sending to the background job queue no issue - moves the meat of `pendingEmailHandler()` code into a new function `sendEmailJob()` that is passed over to the new job service - lets the server keep processing email generation and sending when it receives a shutdown request rather than halting processing mid-send and ending up in a partial state --- core/server/services/mega/mega.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index 3d398bfa4e..ba6b10a621 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -7,6 +7,7 @@ const {events, i18n} = require('../../lib/common'); const logging = require('../../../shared/logging'); const membersService = require('../members'); const bulkEmailService = require('../bulk-email'); +const jobService = require('../jobs'); const models = require('../../models'); const postEmailSerializer = require('./post-email-serializer'); @@ -184,18 +185,8 @@ async function handleUnsubscribeRequest(req) { } } -async function pendingEmailHandler(emailModel, options) { - // CASE: do not send email if we import a database - // TODO: refactor post.published events to never fire on importing - if (options && options.importing) { - return; - } +async function sendEmailJob({emailModel, options}) { const postModel = await models.Post.findOne({id: emailModel.get('post_id')}, {withRelated: ['authors']}); - - if (emailModel.get('status') !== 'pending') { - return; - } - let meta = []; let error = null; let startEmailSend = null; @@ -262,7 +253,7 @@ async function pendingEmailHandler(emailModel, options) { status: batchStatus, meta: JSON.stringify(successes), error: error, - error_data: JSON.stringify(failures) // NOTE:need to discuss how we store this + error_data: JSON.stringify(failures) // NOTE: need to discuss how we store this }, { id: emailModel.id }); @@ -271,6 +262,20 @@ async function pendingEmailHandler(emailModel, options) { } } +async function pendingEmailHandler(emailModel, options) { + // CASE: do not send email if we import a database + // TODO: refactor post.published events to never fire on importing + if (options && options.importing) { + return; + } + + if (emailModel.get('status') !== 'pending') { + return; + } + + return jobService.addJob(sendEmailJob, {emailModel, options}); +} + const statusChangedHandler = (emailModel, options) => { const emailRetried = emailModel.wasChanged() && emailModel.get('status') === 'pending'