From 43cc6d3608180ececbf1910d1e06bed1e22ed43f Mon Sep 17 00:00:00 2001 From: Andres Pache Date: Wed, 3 Sep 2025 12:16:00 -0300 Subject: [PATCH 1/2] check cronId before executing syncChatwootLostMessages --- .../channel/whatsapp/whatsapp.baileys.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index c70ab6f6e..32ae48fad 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -4357,7 +4357,18 @@ export class BaileysStartupService extends ChannelStartupService { const prepare = (message: any) => this.prepareMessage(message); this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare); + // Generate ID for this cron task and store in cache + const cronId = cuid(); + const cronKey = `chatwoot:syncLostMessages`; + await this.chatwootService.getCache()?.hSet(cronKey, this.instance.name, cronId); + const task = cron.schedule('0,30 * * * *', async () => { + // Check ID before executing (only if cache is available) + const cache = this.chatwootService.getCache(); + if (cache) { + const storedId = await cache.hGet(cronKey, this.instance.name); + if (storedId && storedId !== cronId) return; + } this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare); }); task.start(); From 613d486fc243e323cc89c4f8ce903e5544a90de2 Mon Sep 17 00:00:00 2001 From: Andres Pache Date: Wed, 3 Sep 2025 18:35:30 -0300 Subject: [PATCH 2/2] stop tasks with missmatch cronId --- .../channel/whatsapp/whatsapp.baileys.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 32ae48fad..5b0d57502 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -4367,7 +4367,11 @@ export class BaileysStartupService extends ChannelStartupService { const cache = this.chatwootService.getCache(); if (cache) { const storedId = await cache.hGet(cronKey, this.instance.name); - if (storedId && storedId !== cronId) return; + if (storedId && storedId !== cronId) { + this.logger.info(`Stopping syncChatwootLostMessages cron - ID mismatch: ${cronId} vs ${storedId}`); + task.stop(); + return; + } } this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare); });