Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): keep a locally running batch out of the takeover reap
Yudhi Armyndharise9391328ac48
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.tsindex 64ea3730a..591f60c87 100644--- a/src/modules/message/bulk-message.service.spec.ts+++ b/src/modules/message/bulk-message.service.spec.ts@@ -135,6 +135,23 @@ describe('BulkMessageService.onApplicationBootstrap', () => {expect(JSON.stringify(mine.messages)).not.toContain('x'.repeat(64));});+ it('reapProcessingBatches leaves alone a PROCESSING batch this process is still running', async () => {+ // Adopted session: the claim moved here before the engine finished initializing, and a bulk+ // request routed here in that window started a batch of its own before the reap ran.+ const running = { id: 'b-live', sessionId: 'sess-a', status: BatchStatus.PROCESSING, messages: [] };+ const orphan = { id: 'b-dead', sessionId: 'sess-a', status: BatchStatus.PROCESSING, messages: [] };+ repo.find.mockResolvedValue([running, orphan]);+ (service as unknown as { processingBatches: Map<string, boolean> }).processingBatches.set('b-live', true);++ const reaped = await service.reapProcessingBatches('sess-a', 'session adopted from a lapsed node');++ expect(reaped).toBe(1);+ expect(running.status).toBe(BatchStatus.PROCESSING);+ expect(orphan.status).toBe(BatchStatus.FAILED);+ expect(repo.save).toHaveBeenCalledTimes(1);+ expect(repo.save).toHaveBeenCalledWith(orphan);+ });+/*** A batch is only ever driven by the process holding its session's engine, so on a second replica* "PROCESSING" does not mean "abandoned". Reaping a peer's batch tells the caller their senddiff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.tsindex 959a0a460..9ea155236 100644--- a/src/modules/message/bulk-message.service.ts+++ b/src/modules/message/bulk-message.service.ts@@ -166,9 +166,15 @@ export class BulkMessageService implements OnApplicationBootstrap {* Same policy as the boot reaper and for the same reason: the dead node's already-sent messages* are unknowable, so resuming risks double-sends — FAILED with the payloads stripped is the* honest terminal state, and the caller can re-issue the batch knowingly.+ *+ * A batch this process is running itself is not orphaned: the adoption claims the session before+ * its engine finishes initializing, so a bulk request routed here in that window starts a batch+ * that is already PROCESSING by the time the caller reaps. Only the dead holder's batches go.*/async reapProcessingBatches(sessionId: string, reason: string): Promise<number> {- const processing = await this.batchRepository.find({ where: { status: BatchStatus.PROCESSING, sessionId } });+ const processing = (+ await this.batchRepository.find({ where: { status: BatchStatus.PROCESSING, sessionId } })+ ).filter(batch => !this.processingBatches.has(batch.id));for (const batch of processing) {await this.failOrphanedBatch(batch);}