Skip to content

Commit d9c04fc

Browse files
Merge pull request #1906 from andres99x/enhancmenet/check-chatwoot-cron-id
fix: Prevent Duplicate Cron Jobs for Chatwoot Message Sync
2 parents d6a76a0 + 613d486 commit d9c04fc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,7 +4385,22 @@ export class BaileysStartupService extends ChannelStartupService {
43854385
const prepare = (message: any) => this.prepareMessage(message);
43864386
this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare);
43874387

4388+
// Generate ID for this cron task and store in cache
4389+
const cronId = cuid();
4390+
const cronKey = `chatwoot:syncLostMessages`;
4391+
await this.chatwootService.getCache()?.hSet(cronKey, this.instance.name, cronId);
4392+
43884393
const task = cron.schedule('0,30 * * * *', async () => {
4394+
// Check ID before executing (only if cache is available)
4395+
const cache = this.chatwootService.getCache();
4396+
if (cache) {
4397+
const storedId = await cache.hGet(cronKey, this.instance.name);
4398+
if (storedId && storedId !== cronId) {
4399+
this.logger.info(`Stopping syncChatwootLostMessages cron - ID mismatch: ${cronId} vs ${storedId}`);
4400+
task.stop();
4401+
return;
4402+
}
4403+
}
43894404
this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare);
43904405
});
43914406
task.start();

0 commit comments

Comments
 (0)