Skip to content

Commit daed3fa

Browse files
author
antonio-abrantes
committed
fix: delete instance cleanup on baileys failure
1 parent 7a55a2b commit daed3fa

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

src/api/controllers/instance.controller.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export class InstanceController {
345345
};
346346
} catch (error) {
347347
this.logger.error(error);
348-
return { error: true, message: error.toString() };
348+
return { error: true, message: error?.message ?? String(error) };
349349
}
350350
}
351351

@@ -392,7 +392,7 @@ export class InstanceController {
392392
};
393393
} catch (error) {
394394
this.logger.error(error);
395-
return { error: true, message: error.toString() };
395+
return { error: true, message: error?.message ?? String(error) };
396396
}
397397
}
398398

@@ -453,47 +453,41 @@ export class InstanceController {
453453

454454
return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
455455
} catch (error) {
456-
throw new InternalServerErrorException(error.toString());
456+
throw new InternalServerErrorException(error?.message ?? String(error));
457457
}
458458
}
459459

460460
public async deleteInstance({ instanceName }: InstanceDto) {
461461
const { instance } = await this.connectionState({ instanceName });
462+
463+
const waInstances = this.waMonitor.waInstances[instanceName];
464+
462465
try {
463-
const waInstances = this.waMonitor.waInstances[instanceName];
464466
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
467+
} catch (error) {
468+
this.logger.warn(`clearCacheChatwoot failed for "${instanceName}": ${error?.message ?? String(error)}`);
469+
}
465470

466-
if (instance.state === 'connecting' || instance.state === 'open') {
467-
try {
468-
await this.logout({ instanceName });
469-
} catch (error) {
470-
// logout can throw "Connection Closed" when the underlying Baileys
471-
// socket is already dead but waInstances[name] still exists. We
472-
// must continue to the remove.instance emit below — that is the
473-
// only path that purges the in-memory entry and runs cleaningUp().
474-
// Without this catch, the stale entry persists until the entire
475-
// process restarts.
476-
this.logger.warn({
477-
message: 'logout failed during deleteInstance — proceeding with cleanup',
478-
instanceName,
479-
error,
480-
});
481-
}
482-
}
483-
471+
if (instance.state === 'connecting' || instance.state === 'open') {
484472
try {
485-
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
486-
instanceName,
487-
instanceId: waInstances.instanceId,
488-
});
473+
await this.logout({ instanceName });
489474
} catch (error) {
490-
this.logger.error(error);
475+
this.logger.warn(
476+
`Logout failed for "${instanceName}": ${error?.message ?? String(error)}. Continuing cleanup.`,
477+
);
491478
}
479+
}
492480

493-
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
494-
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
481+
try {
482+
waInstances?.sendDataWebhook(Events.INSTANCE_DELETE, {
483+
instanceName,
484+
instanceId: waInstances?.instanceId,
485+
});
495486
} catch (error) {
496-
throw new BadRequestException(error.toString());
487+
this.logger.error(error);
497488
}
489+
490+
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
491+
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
498492
}
499493
}

src/api/services/monitor.service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,20 +397,29 @@ export class WAMonitoringService {
397397
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
398398
try {
399399
await this.waInstances[instanceName]?.sendDataWebhook(Events.REMOVE_INSTANCE, null);
400+
} catch (error) {
401+
this.logger.warn(
402+
`sendDataWebhook REMOVE_INSTANCE failed for "${instanceName}": ${error?.message ?? String(error)}`,
403+
);
404+
}
400405

401-
this.clearDelInstanceTime(instanceName);
406+
this.clearDelInstanceTime(instanceName);
402407

403-
this.cleaningUp(instanceName);
404-
this.cleaningStoreData(instanceName);
405-
} finally {
406-
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
408+
try {
409+
await this.cleaningUp(instanceName);
410+
} catch (error) {
411+
this.logger.warn(`cleaningUp failed for "${instanceName}": ${error?.message ?? String(error)}`);
407412
}
408413

409414
try {
410-
delete this.waInstances[instanceName];
415+
await this.cleaningStoreData(instanceName);
411416
} catch (error) {
412-
this.logger.error(error);
417+
this.logger.warn(`cleaningStoreData failed for "${instanceName}": ${error?.message ?? String(error)}`);
413418
}
419+
420+
delete this.waInstances[instanceName];
421+
422+
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
414423
});
415424
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
416425
try {

0 commit comments

Comments
 (0)