Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
perf(dependencies): made rabbitmq, redis optional
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar authored Aug 17, 2020
1 parent d1d377b commit 1aa671f
Show file tree
Hide file tree
Showing 53 changed files with 1,525 additions and 879 deletions.
3 changes: 0 additions & 3 deletions email-verifier/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ PORT=4100
# MongoDB
MONGO_URL=mongodb://localhost/erxes-email-verifier

# RabbitMQ
RABBITMQ_HOST=amqp://localhost

TRUE_MAIL_API_KEY=

CLEAR_OUT_PHONE_API_KEY=
Expand Down
10 changes: 1 addition & 9 deletions engages-email-sender/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@ MAIN_API_DOMAIN=http://localhost:3300

# MongoDB
MONGO_URL=mongodb://localhost/erxes-engages
TEST_MONGO_URL=mongodb://localhost/erxes-engages-test

# RabbitMQ
RABBITMQ_HOST=amqp://localhost

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
TEST_MONGO_URL=mongodb://localhost/erxes-engages-test
1 change: 1 addition & 0 deletions engages-email-sender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"body-parser": "^1.17.1",
"debug": "^4.1.1",
"dotenv": "^4.0.0",
"erxes-message-broker": "^1.0.3",
"express": "^4.16.4",
"meteor-random": "^0.0.3",
"mongoose": "5.7.5",
Expand Down
9 changes: 3 additions & 6 deletions engages-email-sender/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { filterXSS } from 'xss';
import configs from './api/configs';
import deliveryReports from './api/deliveryReports';
import telnyx from './api/telnyx';
import { initRedis } from './redisClient';

// load environment variables
dotenv.config();

import { connect } from './connection';
import { debugBase, debugInit } from './debuggers';
import { initConsumer } from './messageQueue';
import { initBroker } from './messageBroker';
import { trackEngages } from './trackers/engageTracker';

const app = express();
Expand Down Expand Up @@ -51,11 +50,9 @@ const { PORT } = process.env;
app.listen(PORT, () => {
// connect to mongo database
connect().then(async () => {
initConsumer().catch(e => {
debugBase(`Error ocurred during rabbitmq init ${e.message}`);
initBroker().catch(e => {
debugBase(`Error ocurred during message broker init ${e.message}`);
});

initRedis();
});

debugInit(`Engages server is running on port ${PORT}`);
Expand Down
36 changes: 36 additions & 0 deletions engages-email-sender/src/messageBroker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as dotenv from 'dotenv';
import messageBroker from 'erxes-message-broker';
import { debugBase } from './debuggers';
import { Logs } from './models';
import { sendSms, start } from './sender';

dotenv.config();

let client;

export const initBroker = async () => {
client = await messageBroker({ name: 'logger', RABBITMQ_HOST: process.env.RABBITMQ_HOST });

const { consumeQueue } = client;

// listen for rpc queue =========
consumeQueue('erxes-api:engages-notification', async ({ action, data }) => {
debugBase(`Receiving queue data from erxes-api`, data);

if (action === 'sendEngage') {
await start(data);
}

if (action === 'writeLog') {
await Logs.createLog(data.engageMessageId, 'regular', data.msg);
}

if (action === 'sendEngageSms') {
await sendSms(data);
}
});
};

export default function() {
return client;
}
66 changes: 0 additions & 66 deletions engages-email-sender/src/messageQueue.ts

This file was deleted.

84 changes: 0 additions & 84 deletions engages-email-sender/src/redisClient.ts

This file was deleted.

4 changes: 2 additions & 2 deletions engages-email-sender/src/trackers/engageTracker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as AWS from 'aws-sdk';
import { debugBase } from '../debuggers';
import { sendMessage } from '../messageQueue';
import messageBroker from '../messageBroker';
import { Configs, DeliveryReports, Stats } from '../models';
import { ISESConfig } from '../models/Configs';

Expand Down Expand Up @@ -55,7 +55,7 @@ const handleMessage = async message => {
const rejected = await DeliveryReports.updateOrCreateReport(mailHeaders, type);

if (rejected === 'reject') {
await sendMessage('engagesNotification', {
await messageBroker().sendMessage('engagesNotification', {
action: 'setDoNotDisturb',
data: { customerId: mail.customerId },
});
Expand Down
17 changes: 1 addition & 16 deletions engages-email-sender/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as AWS from 'aws-sdk';
import * as nodemailer from 'nodemailer';
import { debugBase } from './debuggers';
import Configs, { ISESConfig } from './models/Configs';
import { get, set } from './redisClient';
import { getApi } from './trackers/engageTracker';

export const createTransporter = async () => {
Expand Down Expand Up @@ -155,40 +154,26 @@ export const getValueAsString = async name => {
return entry.value;
};

export const resetConfigsCache = () => {
set('configs_erxes_engages', '');
};

export const updateConfigs = async (configsMap): Promise<void> => {
const prevSESConfigs = await Configs.getSESConfigs();

await Configs.updateConfigs(configsMap);

const updatedSESConfigs = await Configs.getSESConfigs();

resetConfigsCache();

if (JSON.stringify(prevSESConfigs) !== JSON.stringify(updatedSESConfigs)) {
await subscribeEngage();
}
};

export const getConfigs = async () => {
const configsCache = await get('configs_erxes_engages');

if (configsCache && configsCache !== '{}') {
return JSON.parse(configsCache);
}

export const getConfigs = async (): Promise<any> => {
const configsMap = {};
const configs = await Configs.find({});

for (const config of configs) {
configsMap[config.code] = config.value;
}

set('configs_erxes_engages', JSON.stringify(configsMap));

return configsMap;
};

Expand Down
Loading

0 comments on commit 1aa671f

Please sign in to comment.