Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): read a retained payload as its replay starts
Yudhi Armyndharis289d41ca2651
diff --git a/src/modules/webhook/webhook-redrive.service.spec.ts b/src/modules/webhook/webhook-redrive.service.spec.tsindex 1611406af..3bf6a309d 100644--- a/src/modules/webhook/webhook-redrive.service.spec.ts+++ b/src/modules/webhook/webhook-redrive.service.spec.ts@@ -129,6 +129,23 @@ describe('WebhookRedriveService', () => {expect(await service.redrive({})).toMatchObject({ delivered: 0, remaining: 0 });});+ it('reads each retained payload only as its replay starts, never the whole batch at once', async () => {+ for (let i = 0; i < 6; i++) await addFailure({ payload: { id: `m${i}`, body: 'x'.repeat(200) } });+ const statements: string[] = [];+ jest.spyOn(ds.logger, 'logQuery').mockImplementation(query => void statements.push(query));++ await service.redrive({});++ // A batch holds up to 500 rows but runs four replays at a time; loading the bodies with the batch+ // held every one of them in memory for the whole call.+ const selectList = (statement: string) => /^SELECT\s+(.*?)\s+FROM\s/is.exec(statement)?.[1] ?? '';+ const payloadReads = statements.filter(+ statement => /webhook_delivery_failures/i.test(statement) && /payload/i.test(selectList(statement)),+ );+ expect(payloadReads).toHaveLength(6);+ expect(delivery.redeliver).toHaveBeenCalledTimes(6);+ });+it('selects eligible subscriptions before the batch limit, past more than 100 old unavailable rows', async () => {const disabled = await webhooks.save(webhooks.create({ ...primary, id: randomUUID(), active: false }));const unsubscribed = await webhooks.save(diff --git a/src/modules/webhook/webhook-redrive.service.ts b/src/modules/webhook/webhook-redrive.service.tsindex 219c53831..e3fd67ade 100644--- a/src/modules/webhook/webhook-redrive.service.ts+++ b/src/modules/webhook/webhook-redrive.service.ts@@ -165,8 +165,8 @@ export class WebhookRedriveService implements OnModuleDestroy {? []: await query.clone()- // `payload` is select: false on the entity; replay explicitly reads it.- .addSelect('failure.payload')+ // The payload is not selected here: a batch holds up to MAX_WEBHOOK_REDRIVE_LIMIT rows but runs+ // REDRIVE_CONCURRENCY replays at a time, so each body is read as its own replay starts.// Failed replays move behind rows with fewer attempts, so a bad receiver cannot pin a batch..orderBy('failure.attempts', 'ASC').addOrderBy('failure.createdAt', 'ASC')@@ -204,14 +204,15 @@ export class WebhookRedriveService implements OnModuleDestroy {return;}try {- const outcome = await this.delivery.redeliver(- webhook,- row.sessionId,- row.event,- row.idempotencyKey,- row.payload ?? {},- { singleAttempt: true },- );+ const payload = await this.readPayload(row.id);+ if (payload === null) {+ // The row was removed, or lost its copy, between the batch and this replay.+ result.skipped++;+ return;+ }+ const outcome = await this.delivery.redeliver(webhook, row.sessionId, row.event, row.idempotencyKey, payload, {+ singleAttempt: true,+ });if (outcome === 'delivered') result.delivered++;else if (outcome === 'enqueued') result.enqueued++;else if (outcome === 'cancelled') result.skipped++;@@ -231,6 +232,17 @@ export class WebhookRedriveService implements OnModuleDestroy {}}+ /** The stored event data of one row, or null when the row or its copy is gone. `payload` is select: false. */+ private async readPayload(id: string): Promise<Record<string, unknown> | null> {+ const row = await this.failureRepository+ .createQueryBuilder('failure')+ .select('failure.id')+ .addSelect('failure.payload')+ .where('failure.id = :id', { id })+ .getOne();+ return row?.payload ?? null;+ }+/** Count the failed replay on the row. Best-effort: the row is already on record either way. */private async bumpAttempts(row: WebhookDeliveryFailure): Promise<void> {try {