Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Returning early in the cron task may mask underlying issues.

Adding a log statement when the task is skipped due to ID mismatch will help with future debugging and monitoring.

Suggested change
if (cache) {
const storedId = await cache.hGet(cronKey, this.instance.name);
if (storedId && storedId !== cronId) return;
}
if (cache) {
const storedId = await cache.hGet(cronKey, this.instance.name);
if (storedId && storedId !== cronId) {
this.logger?.info?.(
`[ChatwootSyncLostMessages] Skipping cron task for instance "${this.instance.name}" due to ID mismatch (storedId: ${storedId}, cronId: ${cronId})`
);
return;
}
}

this.chatwootService.syncLostMessages({ instanceName: this.instance.name }, chatwootConfig, prepare);
});
task.start();
Expand Down