mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2026-01-23 20:12:48 -06:00
feat/force to save all evolution events in a single SQS queue
This commit is contained in:
@@ -98,6 +98,7 @@ SQS_ACCOUNT_ID=
|
|||||||
SQS_REGION=
|
SQS_REGION=
|
||||||
|
|
||||||
SQS_GLOBAL_ENABLED=false
|
SQS_GLOBAL_ENABLED=false
|
||||||
|
SQS_GLOBAL_FORCE_SINGLE_QUEUE=false
|
||||||
SQS_GLOBAL_APPLICATION_STARTUP=false
|
SQS_GLOBAL_APPLICATION_STARTUP=false
|
||||||
SQS_GLOBAL_CALL=false
|
SQS_GLOBAL_CALL=false
|
||||||
SQS_GLOBAL_CHATS_DELETE=false
|
SQS_GLOBAL_CHATS_DELETE=false
|
||||||
|
|||||||
@@ -118,8 +118,11 @@ export class SqsController extends EventController implements EventControllerInt
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(sqsEvents) && sqsEvents.includes(we)) {
|
if (Array.isArray(sqsEvents) && sqsEvents.includes(we)) {
|
||||||
const eventFormatted = `${event.replace('.', '_').toLowerCase()}`;
|
|
||||||
const prefixName = sqsConfig.GLOBAL_ENABLED ? sqsConfig.GLOBAL_PREFIX_NAME : instanceName;
|
const prefixName = sqsConfig.GLOBAL_ENABLED ? sqsConfig.GLOBAL_PREFIX_NAME : instanceName;
|
||||||
|
const eventFormatted =
|
||||||
|
sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE
|
||||||
|
? 'singlequeue'
|
||||||
|
: `${event.replace('.', '_').toLowerCase()}`;
|
||||||
const queueName = `${prefixName}_${eventFormatted}.fifo`;
|
const queueName = `${prefixName}_${eventFormatted}.fifo`;
|
||||||
|
|
||||||
const sqsUrl = `https://sqs.${sqsConfig.REGION}.amazonaws.com/${sqsConfig.ACCOUNT_ID}/${queueName}`;
|
const sqsUrl = `https://sqs.${sqsConfig.REGION}.amazonaws.com/${sqsConfig.ACCOUNT_ID}/${queueName}`;
|
||||||
@@ -199,12 +202,15 @@ export class SqsController extends EventController implements EventControllerInt
|
|||||||
|
|
||||||
private async saveQueues(prefixName: string, events: string[], enable: boolean) {
|
private async saveQueues(prefixName: string, events: string[], enable: boolean) {
|
||||||
if (enable) {
|
if (enable) {
|
||||||
|
const sqsConfig = configService.get<Sqs>('SQS');
|
||||||
const eventsFinded = await this.listQueues(prefixName);
|
const eventsFinded = await this.listQueues(prefixName);
|
||||||
console.log('eventsFinded', eventsFinded);
|
console.log('eventsFinded', eventsFinded);
|
||||||
|
|
||||||
for (const event of events) {
|
for (const event of events) {
|
||||||
const normalizedEvent = event.toLowerCase();
|
const normalizedEvent =
|
||||||
|
sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE
|
||||||
|
? 'singlequeue'
|
||||||
|
: event.toLowerCase();
|
||||||
if (eventsFinded.includes(normalizedEvent)) {
|
if (eventsFinded.includes(normalizedEvent)) {
|
||||||
this.logger.info(`A queue para o evento "${normalizedEvent}" já existe. Ignorando criação.`);
|
this.logger.info(`A queue para o evento "${normalizedEvent}" já existe. Ignorando criação.`);
|
||||||
continue;
|
continue;
|
||||||
@@ -212,7 +218,7 @@ export class SqsController extends EventController implements EventControllerInt
|
|||||||
|
|
||||||
const queueName = `${prefixName}_${normalizedEvent}.fifo`;
|
const queueName = `${prefixName}_${normalizedEvent}.fifo`;
|
||||||
try {
|
try {
|
||||||
const isGlobalEnabled = configService.get<Sqs>('SQS').GLOBAL_ENABLED;
|
const isGlobalEnabled = sqsConfig.GLOBAL_ENABLED;
|
||||||
const createCommand = new CreateQueueCommand({
|
const createCommand = new CreateQueueCommand({
|
||||||
QueueName: queueName,
|
QueueName: queueName,
|
||||||
Attributes: {
|
Attributes: {
|
||||||
@@ -226,6 +232,10 @@ export class SqsController extends EventController implements EventControllerInt
|
|||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this.logger.error(`Erro ao criar queue ${queueName}: ${err.message}`);
|
this.logger.error(`Erro ao criar queue ${queueName}: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ export type Nats = {
|
|||||||
export type Sqs = {
|
export type Sqs = {
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
GLOBAL_ENABLED: boolean;
|
GLOBAL_ENABLED: boolean;
|
||||||
|
GLOBAL_FORCE_SINGLE_QUEUE: boolean;
|
||||||
GLOBAL_PREFIX_NAME: string;
|
GLOBAL_PREFIX_NAME: string;
|
||||||
ACCESS_KEY_ID: string;
|
ACCESS_KEY_ID: string;
|
||||||
SECRET_ACCESS_KEY: string;
|
SECRET_ACCESS_KEY: string;
|
||||||
@@ -500,6 +501,7 @@ export class ConfigService {
|
|||||||
SQS: {
|
SQS: {
|
||||||
ENABLED: process.env?.SQS_ENABLED === 'true',
|
ENABLED: process.env?.SQS_ENABLED === 'true',
|
||||||
GLOBAL_ENABLED: process.env?.SQS_GLOBAL_ENABLED === 'true',
|
GLOBAL_ENABLED: process.env?.SQS_GLOBAL_ENABLED === 'true',
|
||||||
|
GLOBAL_FORCE_SINGLE_QUEUE: process.env?.SQS_GLOBAL_FORCE_SINGLE_QUEUE === 'true',
|
||||||
GLOBAL_PREFIX_NAME: process.env?.SQS_GLOBAL_PREFIX_NAME || 'global',
|
GLOBAL_PREFIX_NAME: process.env?.SQS_GLOBAL_PREFIX_NAME || 'global',
|
||||||
ACCESS_KEY_ID: process.env.SQS_ACCESS_KEY_ID || '',
|
ACCESS_KEY_ID: process.env.SQS_ACCESS_KEY_ID || '',
|
||||||
SECRET_ACCESS_KEY: process.env.SQS_SECRET_ACCESS_KEY || '',
|
SECRET_ACCESS_KEY: process.env.SQS_SECRET_ACCESS_KEY || '',
|
||||||
|
|||||||
Reference in New Issue
Block a user