Skip to content

Commit 23cd6d2

Browse files
committed
feat/force to save all evolution events in a single SQS queue
1 parent cca929e commit 23cd6d2

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ SQS_ACCOUNT_ID=
9898
SQS_REGION=
9999

100100
SQS_GLOBAL_ENABLED=false
101+
SQS_GLOBAL_FORCE_SINGLE_QUEUE=false
101102
SQS_GLOBAL_APPLICATION_STARTUP=false
102103
SQS_GLOBAL_CALL=false
103104
SQS_GLOBAL_CHATS_DELETE=false

src/api/integrations/event/sqs/sqs.controller.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ export class SqsController extends EventController implements EventControllerInt
118118
}
119119

120120
if (Array.isArray(sqsEvents) && sqsEvents.includes(we)) {
121-
const eventFormatted = `${event.replace('.', '_').toLowerCase()}`;
122121
const prefixName = sqsConfig.GLOBAL_ENABLED ? sqsConfig.GLOBAL_PREFIX_NAME : instanceName;
122+
const eventFormatted =
123+
sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE
124+
? 'singlequeue'
125+
: `${event.replace('.', '_').toLowerCase()}`;
123126
const queueName = `${prefixName}_${eventFormatted}.fifo`;
124127

125128
const sqsUrl = `https://sqs.${sqsConfig.REGION}.amazonaws.com/${sqsConfig.ACCOUNT_ID}/${queueName}`;
@@ -199,20 +202,23 @@ export class SqsController extends EventController implements EventControllerInt
199202

200203
private async saveQueues(prefixName: string, events: string[], enable: boolean) {
201204
if (enable) {
205+
const sqsConfig = configService.get<Sqs>('SQS');
202206
const eventsFinded = await this.listQueues(prefixName);
203207
console.log('eventsFinded', eventsFinded);
204208

205209
for (const event of events) {
206-
const normalizedEvent = event.toLowerCase();
207-
210+
const normalizedEvent =
211+
sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE
212+
? 'singlequeue'
213+
: event.toLowerCase();
208214
if (eventsFinded.includes(normalizedEvent)) {
209215
this.logger.info(`A queue para o evento "${normalizedEvent}" já existe. Ignorando criação.`);
210216
continue;
211217
}
212218

213219
const queueName = `${prefixName}_${normalizedEvent}.fifo`;
214220
try {
215-
const isGlobalEnabled = configService.get<Sqs>('SQS').GLOBAL_ENABLED;
221+
const isGlobalEnabled = sqsConfig.GLOBAL_ENABLED;
216222
const createCommand = new CreateQueueCommand({
217223
QueueName: queueName,
218224
Attributes: {
@@ -226,6 +232,10 @@ export class SqsController extends EventController implements EventControllerInt
226232
} catch (err: any) {
227233
this.logger.error(`Erro ao criar queue ${queueName}: ${err.message}`);
228234
}
235+
236+
if (sqsConfig.GLOBAL_ENABLED && sqsConfig.GLOBAL_FORCE_SINGLE_QUEUE) {
237+
break;
238+
}
229239
}
230240
}
231241
}

src/config/env.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export type Nats = {
115115
export type Sqs = {
116116
ENABLED: boolean;
117117
GLOBAL_ENABLED: boolean;
118+
GLOBAL_FORCE_SINGLE_QUEUE: boolean;
118119
GLOBAL_PREFIX_NAME: string;
119120
ACCESS_KEY_ID: string;
120121
SECRET_ACCESS_KEY: string;
@@ -500,6 +501,7 @@ export class ConfigService {
500501
SQS: {
501502
ENABLED: process.env?.SQS_ENABLED === 'true',
502503
GLOBAL_ENABLED: process.env?.SQS_GLOBAL_ENABLED === 'true',
504+
GLOBAL_FORCE_SINGLE_QUEUE: process.env?.SQS_GLOBAL_FORCE_SINGLE_QUEUE === 'true',
503505
GLOBAL_PREFIX_NAME: process.env?.SQS_GLOBAL_PREFIX_NAME || 'global',
504506
ACCESS_KEY_ID: process.env.SQS_ACCESS_KEY_ID || '',
505507
SECRET_ACCESS_KEY: process.env.SQS_SECRET_ACCESS_KEY || '',

0 commit comments

Comments
 (0)