🐛 Fixed batches can have an empty "to" field (#16064)

fixes https://github.com/TryGhost/Team/issues/2246

This solution adds some retries when fetching the recipients for a
batch. For an unknown reason the recipients can be empty (while they
aren't in the database). This should fix the issue for now until we find
more information about the root cause.
This commit is contained in:
Simon Backx 2023-01-05 15:29:03 +01:00 committed by GitHub
parent bfe1915924
commit cf5c64f96b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,12 @@ const postEmailSerializer = require('../mega/post-email-serializer');
const configService = require('../../../shared/config');
const settingsCache = require('../../../shared/settings-cache');
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const messages = {
error: 'The email service received an error from mailgun and was unable to send.'
};
@ -145,14 +151,27 @@ module.exports = {
});
}
// get recipient rows via knex to avoid costly bookshelf model instantiation
const recipientRows = await models.EmailRecipient
.getFilteredCollectionQuery({filter: `batch_id:${emailBatchId}`});
// Patch to prevent saving the related email model
await emailBatchModel.save({status: 'submitting'}, {...knexOptions, patch: true});
try {
// get recipient rows via knex to avoid costly bookshelf model instantiation
let recipientRows = await models.EmailRecipient.getFilteredCollectionQuery({filter: `batch_id:${emailBatchId}`}, knexOptions);
// For an unknown reason, the returned recipient rows is sometimes an empty array
// refs https://github.com/TryGhost/Team/issues/2246
let counter = 0;
while (recipientRows.length === 0 && counter < 5) {
logging.info('[sendEmailJob] Found zero recipients [retries:' + counter + '] for email batch ' + emailBatchId);
counter += 1;
await sleep(200);
recipientRows = await models.EmailRecipient.getFilteredCollectionQuery({filter: `batch_id:${emailBatchId}`}, knexOptions);
}
if (counter > 0) {
logging.info('[sendEmailJob] Recovered recipients [retries:' + counter + '] for email batch ' + emailBatchId + ' - ' + recipientRows.length + ' recipients found');
}
// Load newsletter data on email
await emailBatchModel.relations.email.getLazyRelation('newsletter', {require: false, ...knexOptions});