Skip to content

Commit

Permalink
Fixed emails sending when scheduled post is published
Browse files Browse the repository at this point in the history
no issue

- the schedules controller wraps the post creation in a transaction
- we need to pass that transaction through to all other queries, especially on sqlite where a non-transaction query inside a transaction will lock up because there's only 1 connection available
- updates our model method calls to pass through the transaction options
- switches the members service `list()` call to a direct model `findAll()` call to avoid going through our pagination plugin because the raw knex query does not respect the transacting option
  • Loading branch information
kevinansfield committed Nov 26, 2019
1 parent b9dd0d2 commit 90905b0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions core/server/services/mega/mega.js
@@ -1,3 +1,4 @@
const _ = require('lodash');
const url = require('url');
const moment = require('moment');
const common = require('../../lib/common');
Expand Down Expand Up @@ -51,15 +52,24 @@ const sendTestEmail = async (postModel, toEmails) => {
* @param {object} postModel Post Model Object
*/
const addEmail = async (postModel, options) => {
const {members} = await membersService.api.members.list(Object.assign({filter: 'subscribed:true'}, {limit: 'all'}));
const {emailTmpl, emails} = await getEmailData(postModel, members);
const knexOptions = _.pick(options, ['transacting', 'forUpdate']);

// TODO: this is using the Member model directly rather than the members
// service because the service is hardcoded to Member.findPage and our
// pagination plugin does not currently work with transactions
const members = await models.Member
.findAll(Object.assign({filter: 'subscribed:true'}, knexOptions))
.map(member => member.toJSON(options));

const {emailTmpl, emails} = getEmailData(postModel, members);

// NOTE: don't create email object when there's nobody to send the email to
if (!emails.length) {
return null;
}

const postId = postModel.get('id');
const existing = await models.Email.findOne({post_id: postId});
const existing = await models.Email.findOne({post_id: postId}, knexOptions);

if (!existing) {
return models.Email.add({
Expand All @@ -70,7 +80,7 @@ const addEmail = async (postModel, options) => {
html: emailTmpl.html,
plaintext: emailTmpl.plaintext,
submitted_at: moment().toDate()
}, {transacting: options.transacting});
}, knexOptions);
} else {
return existing;
}
Expand Down

0 comments on commit 90905b0

Please sign in to comment.