From 66814b403eae02484cc7dcdfadc2d89f0cbb1459 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 30 Jul 2024 11:15:21 -0300 Subject: [PATCH] chore: Update monitor service and instance controller Refactored the instance controller and monitor service to improve the connection handling and management of WhatsApp instances. - Modified the instance controller to close the WebSocket connection and restart the instance instead of reloading it. - Updated the monitor service to remove the instance from the `waInstances` object and emit an event to clean up and remove the instance data. - Fixed an issue where the instance was not being removed from the `waInstances` object. Files affected: - CHANGELOG.md - src/api/controllers/instance.controller.ts - src/api/services/monitor.service.ts --- CHANGELOG.md | 4 ++-- src/api/controllers/instance.controller.ts | 9 ++++----- src/api/services/monitor.service.ts | 12 ++++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55f55729..8dc820a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ -# 2.0.4-beta (beta) +# 2.0.4-rc (release candidate) ### Features -* New manager v2.0.0 +* New manager v2.0 ### Fixed diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index ecb78e40..72e64265 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -594,11 +594,11 @@ export class InstanceController { switch (state) { case 'open': if (this.configService.get('CHATWOOT').ENABLED) instance.clearCacheChatwoot(); - // await instance.reloadConnection(); - await instance.client?.ws?.close(); - await delay(2000); - return await this.connectionState({ instanceName }); + this.logger.info('Connection closed, restarting instance' + instanceName); + instance.client?.ws?.close(); + instance.client?.end(new Error('restart')); + return await this.connectToWhatsapp({ instanceName }); default: return await this.connectionState({ instanceName }); } @@ -690,7 +690,6 @@ export class InstanceController { this.logger.error(error); } - delete this.waMonitor.waInstances[instanceName]; this.eventEmitter.emit('remove.instance', instanceName, 'inner'); return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } }; } catch (error) { diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index acdbc4e6..44bb77ed 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -336,18 +336,18 @@ export class WAMonitoringService { private removeInstance() { this.eventEmitter.on('remove.instance', async (instanceName: string) => { - try { - this.waInstances[instanceName] = undefined; - } catch (error) { - this.logger.error(error); - } - try { this.cleaningUp(instanceName); this.cleaningStoreData(instanceName); } finally { this.logger.warn(`Instance "${instanceName}" - REMOVED`); } + + try { + delete this.waInstances[instanceName]; + } catch (error) { + this.logger.error(error); + } }); this.eventEmitter.on('logout.instance', async (instanceName: string) => { try {