Skip to content

Commit ad917d4

Browse files
author
antonio-abrantes
committed
fix: delete instance cleanup on baileys failure
1 parent af5122c commit ad917d4

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

src/api/controllers/instance.controller.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export class InstanceController {
339339
};
340340
} catch (error) {
341341
this.logger.error(error);
342-
return { error: true, message: error.toString() };
342+
return { error: true, message: error?.message ?? String(error) };
343343
}
344344
}
345345

@@ -386,7 +386,7 @@ export class InstanceController {
386386
};
387387
} catch (error) {
388388
this.logger.error(error);
389-
return { error: true, message: error.toString() };
389+
return { error: true, message: error?.message ?? String(error) };
390390
}
391391
}
392392

@@ -445,33 +445,41 @@ export class InstanceController {
445445

446446
return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
447447
} catch (error) {
448-
throw new InternalServerErrorException(error.toString());
448+
throw new InternalServerErrorException(error?.message ?? String(error));
449449
}
450450
}
451451

452452
public async deleteInstance({ instanceName }: InstanceDto) {
453453
const { instance } = await this.connectionState({ instanceName });
454+
455+
const waInstances = this.waMonitor.waInstances[instanceName];
456+
454457
try {
455-
const waInstances = this.waMonitor.waInstances[instanceName];
456458
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
459+
} catch (error) {
460+
this.logger.warn(`clearCacheChatwoot failed for "${instanceName}": ${error?.message ?? String(error)}`);
461+
}
457462

458-
if (instance.state === 'connecting' || instance.state === 'open') {
459-
await this.logout({ instanceName });
460-
}
461-
463+
if (instance.state === 'connecting' || instance.state === 'open') {
462464
try {
463-
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
464-
instanceName,
465-
instanceId: waInstances.instanceId,
466-
});
465+
await this.logout({ instanceName });
467466
} catch (error) {
468-
this.logger.error(error);
467+
this.logger.warn(
468+
`Logout failed for "${instanceName}": ${error?.message ?? String(error)}. Continuing cleanup.`,
469+
);
469470
}
471+
}
470472

471-
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
472-
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
473+
try {
474+
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
475+
instanceName,
476+
instanceId: waInstances?.instanceId,
477+
});
473478
} catch (error) {
474-
throw new BadRequestException(error.toString());
479+
this.logger.error(error);
475480
}
481+
482+
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
483+
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
476484
}
477485
}

src/api/services/monitor.service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,20 +393,29 @@ export class WAMonitoringService {
393393
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
394394
try {
395395
await this.waInstances[instanceName]?.sendDataWebhook(Events.REMOVE_INSTANCE, null);
396+
} catch (error) {
397+
this.logger.warn(
398+
`sendDataWebhook REMOVE_INSTANCE failed for "${instanceName}": ${error?.message ?? String(error)}`,
399+
);
400+
}
396401

397-
this.clearDelInstanceTime(instanceName);
402+
this.clearDelInstanceTime(instanceName);
398403

399-
this.cleaningUp(instanceName);
400-
this.cleaningStoreData(instanceName);
401-
} finally {
402-
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
404+
try {
405+
await this.cleaningUp(instanceName);
406+
} catch (error) {
407+
this.logger.warn(`cleaningUp failed for "${instanceName}": ${error?.message ?? String(error)}`);
403408
}
404409

405410
try {
406-
delete this.waInstances[instanceName];
411+
await this.cleaningStoreData(instanceName);
407412
} catch (error) {
408-
this.logger.error(error);
413+
this.logger.warn(`cleaningStoreData failed for "${instanceName}": ${error?.message ?? String(error)}`);
409414
}
415+
416+
delete this.waInstances[instanceName];
417+
418+
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
410419
});
411420
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
412421
try {

0 commit comments

Comments
 (0)