Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): retry a row whose read failed on a busy database

Yudhi Armyndharis084b9a3f8baf
diff --git a/src/modules/integration/ingress-reconciler.service.spec.ts b/src/modules/integration/ingress-reconciler.service.spec.ts
index f1fa145e9..c5f54f7b5 100644
--- a/src/modules/integration/ingress-reconciler.service.spec.ts
+++ b/src/modules/integration/ingress-reconciler.service.spec.ts
@@ -172,6 +172,28 @@ describe('IngressReconcilerService.sweep', () => {
expect(third.skipped).toBe(OPTS.batchSize);
});
+ it('reads a row again on the next sweep when the read failed because the database was busy', async () => {
+ // Not a row that cannot be read: a lock or a dropped connection says nothing about its payload, and the
+ // delivery is one the reconciler exists to replay within minutes.
+ const id = await insertEvent();
+ const real = events.findOneBy.bind(events);
+ let failed = false;
+ jest.spyOn(events, 'findOneBy').mockImplementation(((where: never) => {
+ if (!failed) {
+ failed = true;
+ return Promise.reject(Object.assign(new Error('SQLITE_BUSY: database is locked'), { code: 'SQLITE_BUSY' }));
+ }
+ return real(where);
+ }) as never);
+
+ const first = await service.sweep(OPTS);
+ expect(first).toMatchObject({ replayed: 0, skipped: 1 });
+
+ const second = await service.sweep(OPTS);
+ expect(second).toMatchObject({ replayed: 1, failed: 0 });
+ expect((await stored(id)).dispatchState).toBe('dispatched');
+ });
+
it('skips a pending row whose payload is gone (nothing to replay from) without burning its budget', async () => {
const id = await insertEvent({ payload: null });
diff --git a/src/modules/integration/ingress-reconciler.service.ts b/src/modules/integration/ingress-reconciler.service.ts
index 9779d4b08..7e952a0a5 100644
--- a/src/modules/integration/ingress-reconciler.service.ts
+++ b/src/modules/integration/ingress-reconciler.service.ts
@@ -19,6 +19,7 @@ import { PluginInstanceService } from './plugin-instance.service';
import { PluginLoaderService } from '../../core/plugins/plugin-loader.service';
import { IngressJobData } from '../queue/processors/ingress.processor';
import { createLogger } from '../../common/services/logger.service';
+import { isTransientDbError } from '../../common/utils/db-errors';
import { resolveNonNegativeIntEnv } from '../../config/configuration';
export interface IngressReconcilerOptions {
@@ -189,7 +190,9 @@ export class IngressReconcilerService implements OnModuleInit, OnModuleDestroy {
id,
action: 'ingress_reconcile_row_unreadable',
});
- this.rememberUnreadable(id, now);
+ // Set aside only a row whose payload could not be read. A read that failed because the database
+ // said "not now" (a lock, a dropped connection) is looked at again by the next sweep, as before.
+ if (!isTransientDbError(err)) this.rememberUnreadable(id, now);
stats.skipped++;
continue;
}