Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): rank a cancelled redrive behind untried rows

Yudhi Armyndharis6a8f0ab31e63
diff --git a/src/modules/webhook/webhook-redrive.service.spec.ts b/src/modules/webhook/webhook-redrive.service.spec.ts
index 08933b8cd..c4e9dbc99 100644
--- a/src/modules/webhook/webhook-redrive.service.spec.ts
+++ b/src/modules/webhook/webhook-redrive.service.spec.ts
@@ -150,6 +150,29 @@ describe('WebhookRedriveService', () => {
expect((await failures.findOneByOrFail({ id: row.id })).attempts).toBe(4);
});
+ it('moves a cancelled replay behind other rows without discarding its retained payload', async () => {
+ const cancelled = await addFailure({ attempts: 1, payload: { id: 'cancelled' } });
+ const next = await addFailure({ attempts: 1, createdAt: new Date(Date.now() - 30_000) });
+ const deliver = delivery.redeliver.getMockImplementation()!;
+ delivery.redeliver.mockImplementation((...args) =>
+ args[3] === cancelled.idempotencyKey ? Promise.resolve('cancelled') : deliver(...args),
+ );
+
+ expect(await service.redrive({ limit: 1 })).toMatchObject({ skipped: 1, failed: 0, remaining: 2 });
+ expect(await service.redrive({ limit: 1 })).toMatchObject({ delivered: 1, skipped: 0, remaining: 1 });
+ expect(delivery.redeliver.mock.calls.map(call => call[3])).toEqual([cancelled.idempotencyKey, next.idempotencyKey]);
+ const retained = await failures
+ .createQueryBuilder('failure')
+ .addSelect('failure.payload')
+ .where('failure.id = :id', { id: cancelled.id })
+ .getOneOrFail();
+ expect(retained).toMatchObject({
+ attempts: 2,
+ payload: cancelled.payload,
+ idempotencyKey: cancelled.idempotencyKey,
+ });
+ });
+
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[] = [];
@@ -370,7 +393,7 @@ describe('WebhookRedriveService', () => {
.mockRejectedValueOnce(new Error('receiver failed'));
expect(await service.redrive({})).toMatchObject({ redriven: 1, enqueued: 1, failed: 1, skipped: 1, remaining: 3 });
- expect(await failures.countBy({ attempts: 4 })).toBe(1);
+ expect(await failures.countBy({ attempts: 4 })).toBe(2);
});
it('continues the batch when recording a failed replay fails', async () => {
diff --git a/src/modules/webhook/webhook-redrive.service.ts b/src/modules/webhook/webhook-redrive.service.ts
index 9819a6dc4..e5ac65aa7 100644
--- a/src/modules/webhook/webhook-redrive.service.ts
+++ b/src/modules/webhook/webhook-redrive.service.ts
@@ -223,8 +223,10 @@ export class WebhookRedriveService implements OnModuleDestroy {
});
if (outcome === 'delivered') result.delivered++;
else if (outcome === 'enqueued') result.enqueued++;
- else if (outcome === 'cancelled') result.skipped++;
- else {
+ else if (outcome === 'cancelled') {
+ result.skipped++;
+ await this.bumpAttempts(row);
+ } else {
result.failed++;
// A failure the delivery path records raises this row's attempts itself. One it refuses before
// sending (a body over the payload cap after the hooks) leaves the row as it was, so the attempt is
@@ -262,7 +264,7 @@ export class WebhookRedriveService implements OnModuleDestroy {
return current !== null && current.attempts === row.attempts;
}
- /** Count the failed replay on the row. Best-effort: the row is already on record either way. */
+ /** Count a replay on its retained row so failed or cancelled rows cannot pin the next batch. */
private async bumpAttempts(row: WebhookDeliveryFailure): Promise<void> {
try {
await this.failureRepository.increment({ id: row.id }, 'attempts', 1);