mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-11 09:53:32 +03:00
c99f40957e
no issue - Increased default mailgun retry limit to 5 - Handling retry logic closer to SDK layer gives less future manual handling - Allowed failing request to be passed through to the caller - To be able to handle failed requests more gracefully in the future we need all available error information to be given to the caller - The previous method with `Promise.all` would have rejected a whole batch without providing details on each specific batch. - Limited data returned with a failed message to batch values - Added better error handling on mega layer - Added new column to store failed batch info - Added reference to mailgan error docs - Refactored batch emailer to respond with instances of an object - It's hard to reason about the response type of bulk mailer when multiple object types can be returned - This gives more clarity and ability to check with `instanceof` check
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
const {URL} = require('url');
|
|
const mailgun = require('mailgun-js');
|
|
const common = require('../../lib/common');
|
|
const configService = require('../../config');
|
|
const settingsCache = require('../settings/cache');
|
|
|
|
function createMailgun(config) {
|
|
const baseUrl = new URL(config.baseUrl);
|
|
|
|
return mailgun({
|
|
apiKey: config.apiKey,
|
|
domain: config.domain,
|
|
protocol: baseUrl.protocol,
|
|
host: baseUrl.host,
|
|
port: baseUrl.port,
|
|
endpoint: baseUrl.pathname,
|
|
retry: 5
|
|
});
|
|
}
|
|
|
|
function getInstance() {
|
|
const bulkEmailConfig = configService.get('bulkEmail');
|
|
const bulkEmailSetting = settingsCache.get('bulk_email_settings');
|
|
const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun);
|
|
const hasMailgunSetting = !!(bulkEmailSetting && bulkEmailSetting.apiKey && bulkEmailSetting.baseUrl && bulkEmailSetting.domain);
|
|
if (!hasMailgunConfig && !hasMailgunSetting) {
|
|
common.logging.warn(`Bulk email service is not configured`);
|
|
} else {
|
|
try {
|
|
let mailgunConfig = hasMailgunConfig ? bulkEmailConfig.mailgun : bulkEmailSetting;
|
|
return createMailgun(mailgunConfig);
|
|
} catch (err) {
|
|
common.logging.warn(`Bulk email service is not configured`);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = {
|
|
getInstance: getInstance
|
|
};
|