diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 21f913d8..79fedd93 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -1,6 +1,6 @@ import { Logger } from '@config/logger.config'; import { BaileysEventMap, MessageUpsertType, proto } from 'baileys'; -import { catchError, concatMap, EMPTY, from, Subject, Subscription, tap } from 'rxjs'; +import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs'; type MessageUpsertPayload = BaileysEventMap['messages.upsert']; type MountProps = { @@ -25,7 +25,15 @@ export class BaileysMessageProcessor { this.processorLogs.log(`Processing batch of ${messages.length} messages`); }), concatMap(({ messages, type, requestId, settings }) => - from(onMessageReceive({ messages, type, requestId }, settings)), + from(onMessageReceive({ messages, type, requestId }, settings)).pipe( + retryWhen((errors) => + errors.pipe( + tap((error) => this.processorLogs.warn(`Retrying message batch due to error: ${error.message}`)), + delay(1000), // 1 segundo de delay + take(3), // Máximo 3 tentativas + ), + ), + ), ), catchError((error) => { this.processorLogs.error(`Error processing message batch: ${error}`);