Skip to content

Commit 3fdb3fa

Browse files
committed
fix: Remove rabbitmq queues when delete instances
1 parent ba58497 commit 3fdb3fa

File tree

7 files changed

+49
-95
lines changed

7 files changed

+49
-95
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.5.3 (develop)
2+
3+
### Fixed
4+
5+
* Remove rabbitmq queues when delete instances
16

27
# 1.5.2 (2023-09-28 17:56)
38

Extras/appsmith/manager.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/libs/amqp.server.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,30 @@ export const initQueues = (instanceName: string, events: string[]) => {
7171
amqp.bindQueue(queueName, exchangeName, event);
7272
});
7373
};
74+
75+
export const removeQueues = (instanceName: string, events: string[]) => {
76+
if (!events || !events.length) return;
77+
78+
const channel = getAMQP();
79+
80+
const queues = events.map((event) => {
81+
return `${event.replace(/_/g, '.').toLowerCase()}`;
82+
});
83+
84+
const exchangeName = instanceName ?? 'evolution_exchange';
85+
86+
queues.forEach((event) => {
87+
const amqp = getAMQP();
88+
89+
amqp.assertExchange(exchangeName, 'topic', {
90+
durable: true,
91+
autoDelete: false,
92+
});
93+
94+
const queueName = `${instanceName}.${event}`;
95+
96+
amqp.deleteQueue(queueName);
97+
});
98+
99+
channel.deleteExchange(exchangeName);
100+
};

src/whatsapp/controllers/instance.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ export class InstanceController {
534534
throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected');
535535
}
536536
try {
537+
this.waMonitor.waInstances[instanceName]?.removeRabbitmqQueues();
538+
537539
if (instance.state === 'connecting') {
538540
this.logger.verbose('logging out instance: ' + instanceName);
539541

src/whatsapp/guards/instance.guard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export async function instanceLoggedGuard(req: Request, _: Response, next: NextF
6565
}
6666

6767
if (waMonitor.waInstances[instance.instanceName]) {
68+
waMonitor.waInstances[instance.instanceName]?.removeRabbitmqQueues();
6869
delete waMonitor.waInstances[instance.instanceName];
6970
}
7071
}

src/whatsapp/services/monitor.service.ts

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { join } from 'path';
77
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
88
import { Logger } from '../../config/logger.config';
99
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
10-
// inserido por francis inicio
1110
import { NotFoundException } from '../../exceptions';
12-
// inserido por francis fim
1311
import { dbserver } from '../../libs/db.connect';
1412
import { RedisCache } from '../../libs/redis.client';
1513
import {
@@ -66,76 +64,19 @@ export class WAMonitoringService {
6664
await this.waInstances[instance]?.client?.logout('Log out instance: ' + instance);
6765
this.waInstances[instance]?.client?.ws?.close();
6866
this.waInstances[instance]?.client?.end(undefined);
67+
this.waInstances[instance]?.removeRabbitmqQueues();
6968
delete this.waInstances[instance];
7069
} else {
70+
this.waInstances[instance]?.removeRabbitmqQueues();
7171
delete this.waInstances[instance];
7272
this.eventEmitter.emit('remove.instance', instance, 'inner');
7373
}
7474
}
7575
}, 1000 * 60 * time);
7676
}
7777
}
78-
/* ocultado por francis inicio
79-
public async instanceInfo(instanceName?: string) {
80-
this.logger.verbose('get instance info');
81-
82-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
83-
84-
const instances: any[] = await Promise.all(
85-
Object.entries(this.waInstances).map(async ([key, value]) => {
86-
const status = value?.connectionStatus?.state || 'unknown';
87-
88-
if (status === 'unknown') {
89-
return null;
90-
}
91-
92-
if (status === 'open') {
93-
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
94-
}
9578

96-
const instanceData: any = {
97-
instance: {
98-
instanceName: key,
99-
owner: value.wuid,
100-
profileName: (await value.getProfileName()) || 'not loaded',
101-
profilePictureUrl: value.profilePictureUrl,
102-
profileStatus: (await value.getProfileStatus()) || '',
103-
status: status,
104-
},
105-
};
106-
107-
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
108-
instanceData.instance.serverUrl = urlServer;
109-
instanceData.instance.apikey = (await this.repository.auth.find(key))?.apikey;
110-
111-
const findChatwoot = await this.waInstances[key].findChatwoot();
112-
if (findChatwoot && findChatwoot.enabled) {
113-
instanceData.instance.chatwoot = {
114-
...findChatwoot,
115-
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
116-
};
117-
}
118-
}
119-
120-
return instanceData;
121-
}),
122-
).then((results) => results.filter((instance) => instance !== null));
123-
124-
this.logger.verbose('return instance info: ' + instances.length);
125-
126-
if (instanceName) {
127-
const instance = instances.find((i) => i.instance.instanceName === instanceName);
128-
return instance || [];
129-
}
130-
131-
return instances;
132-
}
133-
134-
ocultado por francis fim */
135-
136-
// inserido por francis inicio
137-
138-
public async instanceInfo(instanceName?: string) {
79+
public async instanceInfo(instanceName?: string) {
13980
this.logger.verbose('get instance info');
14081
if (instanceName && !this.waInstances[instanceName]) {
14182
throw new NotFoundException(`Instance "${instanceName}" not found`);
@@ -210,17 +151,6 @@ public async instanceInfo(instanceName?: string) {
210151
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
211152
}
212153

213-
214-
215-
// inserido por francis fim
216-
217-
218-
219-
220-
221-
222-
223-
224154
private delInstanceFiles() {
225155
this.logger.verbose('cron to delete instance files started');
226156
setInterval(async () => {

src/whatsapp/services/whatsapp.service.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import EventEmitter2 from 'eventemitter2';
4040
import fs, { existsSync, readFileSync } from 'fs';
4141
import Long from 'long';
4242
import NodeCache from 'node-cache';
43-
import { getMIMEType } from 'node-mime-types';
4443
import { release } from 'os';
4544
import { join } from 'path';
4645
import P from 'pino';
@@ -66,7 +65,7 @@ import {
6665
import { Logger } from '../../config/logger.config';
6766
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
6867
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../exceptions';
69-
import { getAMQP } from '../../libs/amqp.server';
68+
import { getAMQP, removeQueues } from '../../libs/amqp.server';
7069
import { dbserver } from '../../libs/db.connect';
7170
import { RedisCache } from '../../libs/redis.client';
7271
import { getIO } from '../../libs/socket.server';
@@ -495,6 +494,14 @@ export class WAStartupService {
495494
return data;
496495
}
497496

497+
public async removeRabbitmqQueues() {
498+
this.logger.verbose('Removing rabbitmq');
499+
500+
if (this.localRabbitmq.enabled) {
501+
removeQueues(this.instanceName, this.localRabbitmq.events);
502+
}
503+
}
504+
498505
private async loadTypebot() {
499506
this.logger.verbose('Loading typebot');
500507
const data = await this.repository.typebot.find(this.instanceName);
@@ -2308,37 +2315,18 @@ export class WAStartupService {
23082315
mediaMessage.fileName = arrayMatch[1];
23092316
this.logger.verbose('File name: ' + mediaMessage.fileName);
23102317
}
2311-
// *inserido francis inicio
23122318
let mimetype: string;
2313-
// *inserido francis final
2314-
23152319

23162320
if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) {
23172321
mediaMessage.fileName = 'image.png';
2318-
// inserido francis inicio
23192322
mimetype = 'image/png';
2320-
// inserido francis inicio
2321-
23222323
}
23232324

23242325
if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {
23252326
mediaMessage.fileName = 'video.mp4';
2326-
// inserido francis inicio
23272327
mimetype = 'video/mp4';
2328-
// inserido francis final
23292328
}
23302329

2331-
// ocultado francis inicio
2332-
// let mimetype: string;
2333-
2334-
2335-
// if (isURL(mediaMessage.media)) {
2336-
// mimetype = getMIMEType(mediaMessage.media);
2337-
// } else {
2338-
// mimetype = getMIMEType(mediaMessage.fileName);
2339-
// }
2340-
// ocultado francis final
2341-
23422330
this.logger.verbose('Mimetype: ' + mimetype);
23432331

23442332
prepareMedia[mediaType].caption = mediaMessage?.caption;
@@ -2714,6 +2702,7 @@ export class WAStartupService {
27142702

27152703
public async markMessageAsRead(data: ReadMessageDto) {
27162704
this.logger.verbose('Marking message as read');
2705+
27172706
try {
27182707
const keys: proto.IMessageKey[] = [];
27192708
data.read_messages.forEach((read) => {

0 commit comments

Comments
 (0)