Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(bulk): keep sent results when a batch run fails on a write
Yudhi Armyndharisadecb1bc445e
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.tsindex 8e8a5a85a..40c1a4a7c 100644--- a/src/modules/message/bulk-message.service.spec.ts+++ b/src/modules/message/bulk-message.service.spec.ts@@ -434,6 +434,25 @@ describe('BulkMessageService.processBatch', () => {expect(inFlightMarkers().has('b1')).toBe(false);});+ // Only this run knows what it sent: a FAILED written without that would report delivered items as+ // pending, and a re-issue of the unsent items would send them again.+ it('records what the run sent when a progress write throws after a send', async () => {+ repo.findOne.mockResolvedValue(makeBatch(3));+ repo.update+ .mockResolvedValueOnce({ affected: 1 }) // start transition+ .mockRejectedValueOnce(new Error('database unavailable')); // progress write after item 0 was sent++ await expect(runProcessBatch()).rejects.toThrow('database unavailable');++ expect(engine.sendTextMessage).toHaveBeenCalledTimes(1);+ const [criteria, partial] = (repo.update.mock.calls as Array<[unknown, Partial<MessageBatch>]>).at(-1)!;+ expect(criteria).toEqual({ id: 'b1', status: In([BatchStatus.PENDING, BatchStatus.PROCESSING]) });+ expect(partial.status).toBe(BatchStatus.FAILED);+ expect(partial.results?.[0].status).toBe(BatchMessageStatus.SENT);+ expect(partial.progress).toMatchObject({ sent: 1, pending: 2 });+ expect(partial.currentIndex).toBe(1);+ });+it('still rethrows the run error when failing the batch throws too', async () => {repo.findOne.mockRejectedValueOnce(new Error('database unavailable'));repo.update.mockRejectedValueOnce(new Error('still unavailable'));diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.tsindex 8d9569bff..9da97a312 100644--- a/src/modules/message/bulk-message.service.ts+++ b/src/modules/message/bulk-message.service.ts@@ -181,13 +181,25 @@ export class BulkMessageService implements OnModuleInit, OnApplicationBootstrap* a batch that finalized in between must keep its real status, progress and results. Returns* whether the row was still unfinished and is now FAILED. Without `messages` (the run failed* before it could read the row) the stored payloads are left as they are.+ *+ * `withRunState` is for the run's own failure path only: that run holds the progress, results and+ * currentIndex of what it sent, newer than the row. A reap must not write them back, since what it+ * read can be older than a progress write the batch's still-running node made since.*/- private async failOrphanedBatch(batch: Pick<MessageBatch, 'id'> & Partial<MessageBatch>): Promise<boolean> {+ private async failOrphanedBatch(+ batch: Pick<MessageBatch, 'id'> & Partial<MessageBatch>,+ withRunState = false,+ ): Promise<boolean> {const set: QueryDeepPartialEntity<MessageBatch> = { status: BatchStatus.FAILED, completedAt: new Date() };if (batch.messages) {this.stripBatchMediaPayloads(batch.messages);set.messages = batch.messages as QueryDeepPartialEntity<MessageBatch>['messages'];}+ if (withRunState) {+ if (batch.progress) set.progress = batch.progress;+ if (batch.results) set.results = batch.results;+ if (batch.currentIndex !== undefined) set.currentIndex = batch.currentIndex;+ }const failed = await this.batchRepository.update({ id: batch.id, status: In([BatchStatus.PENDING, BatchStatus.PROCESSING]) },set,@@ -432,8 +444,9 @@ export class BulkMessageService implements OnModuleInit, OnApplicationBootstrap// A throw here (a DB error on the pickup read, the start transition or a progress write) would// leave the row PENDING or PROCESSING, and nothing else moves it on while this process lives:// the reapers run only at boot and on a session takeover. Best effort, since the database that- // just failed may fail again.- await this.failOrphanedBatch(batch ?? { id: batchDbId }).catch((failError: unknown) => {+ // just failed may fail again. The row gets what this run sent, so delivered items are not+ // reported as pending.+ await this.failOrphanedBatch(batch ?? { id: batchDbId }, true).catch((failError: unknown) => {this.logger.error(`Could not mark batch ${batchDbId} FAILED after its run threw: ${String(failError)}`);});throw error;