Make rabbitMQ per events

This commit is contained in:
Judson Cairo 2024-03-04 12:16:26 -03:00
parent 6b930058d1
commit 7dd589fb6c
6 changed files with 73 additions and 39 deletions

View File

@ -47,6 +47,7 @@ REDIS_URI=redis://redis:6379
REDIS_PREFIX_KEY=evdocker REDIS_PREFIX_KEY=evdocker
RABBITMQ_ENABLED=false RABBITMQ_ENABLED=false
RABBITMQ_GLOBAL_EVENT_QUEUE=false
RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672 RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672
WEBSOCKET_ENABLED=false WEBSOCKET_ENABLED=false

View File

@ -62,6 +62,7 @@ ENV REDIS_URI=redis://redis:6379
ENV REDIS_PREFIX_KEY=evolution ENV REDIS_PREFIX_KEY=evolution
ENV RABBITMQ_ENABLED=false ENV RABBITMQ_ENABLED=false
ENV RABBITMQ_GLOBAL_EVENT_QUEUE=false
ENV RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672 ENV RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672
ENV WEBSOCKET_ENABLED=false ENV WEBSOCKET_ENABLED=false

View File

@ -71,6 +71,7 @@ export type Redis = {
export type Rabbitmq = { export type Rabbitmq = {
ENABLED: boolean; ENABLED: boolean;
GLOBAL_EVENT_QUEUE: boolean;
URI: string; URI: string;
}; };
@ -282,6 +283,7 @@ export class ConfigService {
}, },
RABBITMQ: { RABBITMQ: {
ENABLED: process.env?.RABBITMQ_ENABLED === 'true', ENABLED: process.env?.RABBITMQ_ENABLED === 'true',
GLOBAL_EVENT_QUEUE: process.env?.RABBITMQ_GLOBAL_EVENT_QUEUE === 'true',
URI: process.env.RABBITMQ_URI || '', URI: process.env.RABBITMQ_URI || '',
}, },
SQS: { SQS: {

View File

@ -83,6 +83,7 @@ REDIS:
RABBITMQ: RABBITMQ:
ENABLED: false ENABLED: false
GLOBAL_EVENT_QUEUE: false
URI: "amqp://guest:guest@localhost:5672" URI: "amqp://guest:guest@localhost:5672"
SQS: SQS:

View File

@ -1,6 +1,6 @@
import * as amqp from 'amqplib/callback_api'; import * as amqp from 'amqplib/callback_api';
import { configService, Rabbitmq } from '../config/env.config'; import { configService, HttpServer, Rabbitmq } from '../config/env.config';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
const logger = new Logger('AMQP'); const logger = new Logger('AMQP');
@ -9,8 +9,8 @@ let amqpChannel: amqp.Channel | null = null;
export const initAMQP = () => { export const initAMQP = () => {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const uri = configService.get<Rabbitmq>('RABBITMQ').URI; const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
amqp.connect(uri, (error, connection) => { amqp.connect(rabbitConfig.URI, (error, connection) => {
if (error) { if (error) {
reject(error); reject(error);
return; return;
@ -45,6 +45,7 @@ export const getAMQP = (): amqp.Channel | null => {
export const initQueues = (instanceName: string, events: string[]) => { export const initQueues = (instanceName: string, events: string[]) => {
if (!instanceName || !events || !events.length) return; if (!instanceName || !events || !events.length) return;
const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
const queues = events.map((event) => { const queues = events.map((event) => {
return `${event.replace(/_/g, '.').toLowerCase()}`; return `${event.replace(/_/g, '.').toLowerCase()}`;
@ -60,7 +61,7 @@ export const initQueues = (instanceName: string, events: string[]) => {
assert: true, assert: true,
}); });
const queueName = `${instanceName}.${event}`; const queueName = rabbitConfig.GLOBAL_EVENT_QUEUE ? event : `${instanceName}.${event}`;
amqp.assertQueue(queueName, { amqp.assertQueue(queueName, {
durable: true, durable: true,
@ -76,6 +77,7 @@ export const initQueues = (instanceName: string, events: string[]) => {
export const removeQueues = (instanceName: string, events: string[]) => { export const removeQueues = (instanceName: string, events: string[]) => {
if (!events || !events.length) return; if (!events || !events.length) return;
const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
const channel = getAMQP(); const channel = getAMQP();
@ -94,10 +96,64 @@ export const removeQueues = (instanceName: string, events: string[]) => {
assert: true, assert: true,
}); });
const queueName = `${instanceName}.${event}`; const queueName = rabbitConfig.GLOBAL_EVENT_QUEUE ? event : `${instanceName}.${event}`;
amqp.deleteQueue(queueName); amqp.deleteQueue(queueName);
}); });
channel.deleteExchange(exchangeName); channel.deleteExchange(exchangeName);
}; };
interface SendEventData {
instanceName: string;
wuid: string;
event: string;
apiKey?: string;
data: any;
}
export const sendEventData = ({ data, event, wuid, apiKey, instanceName }: SendEventData) => {
const exchangeName = instanceName ?? 'evolution_exchange';
amqpChannel.assertExchange(exchangeName, 'topic', {
durable: true,
autoDelete: false,
assert: true,
});
const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
const queueName = rabbitConfig.GLOBAL_EVENT_QUEUE ? event : `${instanceName}.${event}`;
amqpChannel.assertQueue(queueName, {
durable: true,
autoDelete: false,
arguments: { 'x-queue-type': 'quorum' },
});
amqpChannel.bindQueue(queueName, exchangeName, event);
const serverUrl = configService.get<HttpServer>('SERVER').URL;
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
const now = localISOTime;
const message = {
event,
instance: instanceName,
data,
server_url: serverUrl,
date_time: now,
sender: wuid,
};
if (apiKey) {
message['apikey'] = apiKey;
}
logger.log({
queueName,
exchangeName,
event,
});
amqpChannel.publish(exchangeName, event, Buffer.from(JSON.stringify(message)));
};

View File

@ -20,7 +20,7 @@ import {
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { ROOT_DIR } from '../../config/path.config'; import { ROOT_DIR } from '../../config/path.config';
import { NotFoundException } from '../../exceptions'; import { NotFoundException } from '../../exceptions';
import { getAMQP, removeQueues } from '../../libs/amqp.server'; import { getAMQP, removeQueues, sendEventData } from '../../libs/amqp.server';
import { getIO } from '../../libs/socket.server'; import { getIO } from '../../libs/socket.server';
import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server'; import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server';
import { ChamaaiRaw, IntegrationRaw, ProxyRaw, RabbitmqRaw, SettingsRaw, SqsRaw, TypebotRaw } from '../models'; import { ChamaaiRaw, IntegrationRaw, ProxyRaw, RabbitmqRaw, SettingsRaw, SqsRaw, TypebotRaw } from '../models';
@ -685,40 +685,13 @@ export class WAStartupService {
if (amqp) { if (amqp) {
if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) { if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) {
const exchangeName = this.instanceName ?? 'evolution_exchange'; sendEventData({
amqp.assertExchange(exchangeName, 'topic', {
durable: true,
autoDelete: false,
assert: true,
});
const queueName = `${this.instanceName}.${event}`;
amqp.assertQueue(queueName, {
durable: true,
autoDelete: false,
arguments: {
'x-queue-type': 'quorum',
},
});
amqp.bindQueue(queueName, exchangeName, event);
const message = {
event,
instance: this.instance.name,
data, data,
server_url: serverUrl, event,
date_time: now, instanceName: this.instanceName,
sender: this.wuid, wuid: this.wuid,
}; apiKey: expose && instanceApikey ? instanceApikey : undefined,
});
if (expose && instanceApikey) {
message['apikey'] = instanceApikey;
}
amqp.publish(exchangeName, event, Buffer.from(JSON.stringify(message)));
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) { if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
const logData = { const logData = {