mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 03:42:23 -06:00
refactor: optimize retry loop and robustify cache error handling
This commit is contained in:
@@ -1653,6 +1653,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
let retries = 0;
|
let retries = 0;
|
||||||
const maxRetries = 3;
|
const maxRetries = 3;
|
||||||
|
const retryDelay = 500; // 500ms delay to avoid blocking for too long
|
||||||
|
|
||||||
while (retries < maxRetries) {
|
while (retries < maxRetries) {
|
||||||
const messages = (await this.prismaRepository.$queryRaw`
|
const messages = (await this.prismaRepository.$queryRaw`
|
||||||
@@ -1669,7 +1670,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
retries++;
|
retries++;
|
||||||
if (retries < maxRetries) {
|
if (retries < maxRetries) {
|
||||||
await delay(2000);
|
await delay(retryDelay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1679,6 +1680,13 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sync the incoming key.remoteJid with the stored one.
|
||||||
|
// This mutation is safe and necessary because Baileys events might use LIDs while we store Phone JIDs (or vice versa).
|
||||||
|
// Normalizing ensuring downstream logic uses the identifier that exists in our database.
|
||||||
|
if (findMessage?.key?.remoteJid && key.remoteJid !== findMessage.key.remoteJid) {
|
||||||
|
key.remoteJid = findMessage.key.remoteJid;
|
||||||
|
}
|
||||||
if (findMessage?.key?.remoteJid && findMessage.key.remoteJid !== key.remoteJid) {
|
if (findMessage?.key?.remoteJid && findMessage.key.remoteJid !== key.remoteJid) {
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
`Updating key.remoteJid from ${key.remoteJid} to ${findMessage.key.remoteJid} based on stored message`,
|
`Updating key.remoteJid from ${key.remoteJid} to ${findMessage.key.remoteJid} based on stored message`,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { prismaRepository } from '@api/server.module';
|
import { prismaRepository } from '@api/server.module';
|
||||||
import { configService, Database } from '@config/env.config';
|
import { configService, Database } from '@config/env.config';
|
||||||
import { Logger } from '@config/logger.config';
|
import { Logger } from '@config/logger.config';
|
||||||
|
import { Prisma } from '@prisma/client';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
const logger = new Logger('OnWhatsappCache');
|
const logger = new Logger('OnWhatsappCache');
|
||||||
@@ -170,7 +171,11 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
|||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// Check for unique constraint violation (Prisma error code P2002)
|
// Check for unique constraint violation (Prisma error code P2002)
|
||||||
if (error.code === 'P2002' && error.meta?.target?.includes('remoteJid')) {
|
if (
|
||||||
|
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||||
|
error.code === 'P2002' &&
|
||||||
|
(error.meta?.target as string[])?.includes('remoteJid')
|
||||||
|
) {
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`[saveOnWhatsappCache] Race condition detected for ${remoteJid}, updating existing record instead.`,
|
`[saveOnWhatsappCache] Race condition detected for ${remoteJid}, updating existing record instead.`,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user