Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): log unreadable rows as fields, rank them after readable

Yudhi Armyndharis16d0ce9b2792
diff --git a/src/modules/integration/redrive.service.spec.ts b/src/modules/integration/redrive.service.spec.ts
index 29e7412b0..370b25d61 100644
--- a/src/modules/integration/redrive.service.spec.ts
+++ b/src/modules/integration/redrive.service.spec.ts
@@ -15,6 +15,7 @@ describe('RedriveService', () => {
find: jest.fn(),
findOneBy: jest.fn(),
update: jest.fn().mockResolvedValue(undefined),
+ increment: jest.fn().mockResolvedValue(undefined),
count: jest.fn().mockResolvedValue(0),
};
events = { update: jest.fn().mockResolvedValue(undefined) };
@@ -43,6 +44,8 @@ describe('RedriveService', () => {
expect(res.redriven).toBe(1);
expect(ingressEnqueue.enqueue).toHaveBeenCalledTimes(1);
expect(repo.update).toHaveBeenCalledWith({ id: 'f2' }, { redriven: true });
+ // The unreadable row goes behind the readable ones, so a batch of them cannot starve the rest.
+ expect(repo.increment).toHaveBeenCalledWith({ id: 'f1' }, 'attempts', 1);
});
it('re-enqueues non-redriven inbound failures for an instance and marks them redriven', async () => {
diff --git a/src/modules/integration/redrive.service.ts b/src/modules/integration/redrive.service.ts
index 405114582..99d6c1aff 100644
--- a/src/modules/integration/redrive.service.ts
+++ b/src/modules/integration/redrive.service.ts
@@ -108,11 +108,14 @@ export class RedriveService implements OnModuleDestroy {
const row = await this.repo.findOneBy({ id });
if (row) rows.push(row);
} catch (rowError) {
- this.logger.error('A dead letter could not be read and is skipped', {
- id,
- error: rowError instanceof Error ? rowError.message : String(rowError),
- action: 'ingress_redrive_row_unreadable',
- });
+ this.logger.error(
+ 'A dead letter could not be read and is skipped',
+ rowError instanceof Error ? rowError.message : String(rowError),
+ { id, action: 'ingress_redrive_row_unreadable' },
+ );
+ // Counted as a failed replay, by id and so without reading the payload: left at attempts 0 it
+ // heads every window for good, and a batch of them would starve the rows behind it.
+ await this.repo.increment({ id }, 'attempts', 1).catch(() => undefined);
}
}
}
diff --git a/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts b/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts
index bcb372297..c335e4b6e 100644
--- a/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts
+++ b/src/modules/webhook/webhook-outbox.service.sqlite.spec.ts
@@ -1,4 +1,5 @@
import 'reflect-metadata';
+import { LoggerService } from '../../common/services/logger.service';
import { DataSource } from 'typeorm';
import { Session } from '../session/entities/session.entity';
import { Webhook } from './entities/webhook.entity';
@@ -92,4 +93,24 @@ describe('WebhookOutboxService.findStale() past an unreadable payload (SQLite)',
// The cursor passes the unreadable row at the end of the page, or a page of them would be read again forever.
expect(scannedThrough).toBe('00000000-0000-4000-8000-000000000003');
});
+
+ it('logs each unreadable row with its id and action as fields, not as the trace', async () => {
+ const error = jest.spyOn(LoggerService.prototype, 'error').mockImplementation(() => undefined);
+ try {
+ await insertRaw('00000000-0000-4000-8000-000000000001', 'k1', '{oops', '2026-01-01 00:00:00');
+
+ await outbox.findStale(new Date('2026-02-01T00:00:00Z'), 10);
+
+ expect(error).toHaveBeenCalledWith(
+ expect.stringContaining('could not be read'),
+ expect.any(String),
+ expect.objectContaining({
+ id: '00000000-0000-4000-8000-000000000001',
+ action: 'webhook_outbox_row_unreadable',
+ }),
+ );
+ } finally {
+ error.mockRestore();
+ }
+ });
});
diff --git a/src/modules/webhook/webhook-outbox.service.ts b/src/modules/webhook/webhook-outbox.service.ts
index 0fa101b6f..c1179a081 100644
--- a/src/modules/webhook/webhook-outbox.service.ts
+++ b/src/modules/webhook/webhook-outbox.service.ts
@@ -183,11 +183,11 @@ export class WebhookOutboxService implements OnModuleInit, OnModuleDestroy {
const row = await this.outbox.findOneBy({ id });
if (row) rows.push(row);
} catch (rowError) {
- this.logger.error('A stranded delivery could not be read and is skipped', {
- id,
- error: rowError instanceof Error ? rowError.message : String(rowError),
- action: 'webhook_outbox_row_unreadable',
- });
+ this.logger.error(
+ 'A stranded delivery could not be read and is skipped',
+ rowError instanceof Error ? rowError.message : String(rowError),
+ { id, action: 'webhook_outbox_row_unreadable' },
+ );
}
}
const last = ids.at(-1);