mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
refactor sending jobs to worker server
This commit is contained in:
@@ -4,6 +4,7 @@ import { TAVERN_ID } from '../models/group'; // eslint-disable-line import/no-cy
|
|||||||
import { encrypt } from './encryption';
|
import { encrypt } from './encryption';
|
||||||
import logger from './logger';
|
import logger from './logger';
|
||||||
import common from '../../common';
|
import common from '../../common';
|
||||||
|
import { sendJob } from './worker';
|
||||||
|
|
||||||
const IS_PROD = nconf.get('IS_PROD');
|
const IS_PROD = nconf.get('IS_PROD');
|
||||||
const EMAIL_SERVER = {
|
const EMAIL_SERVER = {
|
||||||
@@ -156,29 +157,14 @@ export async function sendTxn (mailingInfoArray, emailType, variables, personalV
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (IS_PROD && mailingInfoArray.length > 0) {
|
if (IS_PROD && mailingInfoArray.length > 0) {
|
||||||
return got.post(`${EMAIL_SERVER.url}/job`, {
|
return sendJob('email', {
|
||||||
retry: 5, // retry the http request to the email server 5 times
|
data: {
|
||||||
timeout: 60000, // wait up to 60s before timing out
|
emailType,
|
||||||
username: EMAIL_SERVER.auth.user,
|
to: mailingInfoArray,
|
||||||
password: EMAIL_SERVER.auth.password,
|
variables,
|
||||||
json: {
|
personalVariables,
|
||||||
type: 'email',
|
|
||||||
data: {
|
|
||||||
emailType,
|
|
||||||
to: mailingInfoArray,
|
|
||||||
variables,
|
|
||||||
personalVariables,
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
priority: 'high',
|
|
||||||
attempts: 5,
|
|
||||||
backoff: { delay: 10 * 60 * 1000, type: 'fixed' },
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}).json().catch(err => logger.error(err, {
|
});
|
||||||
extraMessage: 'Error while sending an email.',
|
|
||||||
emailType,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
38
website/server/libs/worker.js
Normal file
38
website/server/libs/worker.js
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import got from 'got';
|
||||||
|
import nconf from 'nconf';
|
||||||
|
import logger from './logger';
|
||||||
|
|
||||||
|
const IS_PROD = nconf.get('IS_PROD');
|
||||||
|
const EMAIL_SERVER = {
|
||||||
|
url: nconf.get('EMAIL_SERVER_URL'),
|
||||||
|
auth: {
|
||||||
|
user: nconf.get('EMAIL_SERVER_AUTH_USER'),
|
||||||
|
password: nconf.get('EMAIL_SERVER_AUTH_PASSWORD'),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function sendJob (type, config) {
|
||||||
|
if (IS_PROD) {
|
||||||
|
const { data, options } = config;
|
||||||
|
const usedOptions = {
|
||||||
|
priority: 'high',
|
||||||
|
backoff: { delay: 10 * 60 * 1000, type: 'exponential' },
|
||||||
|
...options,
|
||||||
|
};
|
||||||
|
|
||||||
|
return got.post(`${EMAIL_SERVER.url}/job`, {
|
||||||
|
retry: 5, // retry the http request to the email server 5 times
|
||||||
|
timeout: 60000, // wait up to 60s before timing out
|
||||||
|
username: EMAIL_SERVER.auth.user,
|
||||||
|
password: EMAIL_SERVER.auth.password,
|
||||||
|
json: {
|
||||||
|
type,
|
||||||
|
data,
|
||||||
|
options: usedOptions,
|
||||||
|
},
|
||||||
|
}).json().catch(err => logger.error(err, {
|
||||||
|
extraMessage: 'Error while sending an email.',
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user