Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(queue): share one WebhookOutboxService between queue and webhook

Yudhi Armyndharis0384dd206567
diff --git a/src/modules/queue/queue.module.spec.ts b/src/modules/queue/queue.module.spec.ts
index ca6550049..5874468e1 100644
--- a/src/modules/queue/queue.module.spec.ts
+++ b/src/modules/queue/queue.module.spec.ts
@@ -4,6 +4,9 @@ import { getQueueToken } from '@nestjs/bullmq';
import { QUEUE_NAMES } from './queue-names';
import { QueueModule, WEBHOOK_QUEUE_JOB_OPTIONS } from './queue.module';
import { ShutdownSafeQueue } from './shutdown-safe-queue';
+import { WebhookModule } from '../webhook/webhook.module';
+import { WebhookOutboxModule } from '../webhook/webhook-outbox.module';
+import { WebhookOutboxService } from '../webhook/webhook-outbox.service';
// A stub base keeps the factory from opening a Redis connection; ShutdownSafeQueue still extends it.
jest.mock('bullmq', () => ({
@@ -49,3 +52,16 @@ describe('WEBHOOK_QUEUE_JOB_OPTIONS', () => {
expect(opts.age).toBeGreaterThan(0);
});
});
+
+// The processors and the webhook module both need the outbox. Providing the service in each module made
+// two instances with queue on, and its startup prune and daily timer ran twice.
+describe('WebhookOutboxService wiring', () => {
+ it('is provided by one module and imported by the queue and webhook modules', () => {
+ for (const owner of [QueueModule, WebhookModule]) {
+ expect(Reflect.getMetadata('providers', owner)).not.toContain(WebhookOutboxService);
+ expect(Reflect.getMetadata('imports', owner)).toContain(WebhookOutboxModule);
+ }
+ expect(Reflect.getMetadata('providers', WebhookOutboxModule)).toEqual([WebhookOutboxService]);
+ expect(Reflect.getMetadata('exports', WebhookOutboxModule)).toEqual([WebhookOutboxService]);
+ });
+});
diff --git a/src/modules/queue/queue.module.ts b/src/modules/queue/queue.module.ts
index c96480f6f..6a479524e 100644
--- a/src/modules/queue/queue.module.ts
+++ b/src/modules/queue/queue.module.ts
@@ -10,8 +10,7 @@ import { QUEUE_NAMES } from './queue-names';
import { queueConnectionOptions } from './redis-connection';
import { ShutdownSafeQueue } from './shutdown-safe-queue';
import { Webhook } from '../webhook/entities/webhook.entity';
-import { WebhookOutboxService } from '../webhook/webhook-outbox.service';
-import { WebhookOutboxEvent } from '../webhook/entities/webhook-outbox-event.entity';
+import { WebhookOutboxModule } from '../webhook/webhook-outbox.module';
import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';
import { IntegrationDeliveryFailure } from '../integration/entities/integration-delivery-failure.entity';
import { IngressEvent } from '../integration/entities/ingress-event.entity';
@@ -40,10 +39,9 @@ BullModule.queueClass = ShutdownSafeQueue;
// Required for WebhookProcessor to inject Repository<Webhook> + Repository<WebhookDeliveryFailure>;
// IngressProcessor to inject Repository<IntegrationDeliveryFailure> + Repository<IngressEvent> (all on
// the 'data' connection).
- TypeOrmModule.forFeature(
- [Webhook, WebhookDeliveryFailure, WebhookOutboxEvent, IntegrationDeliveryFailure, IngressEvent],
- 'data',
- ),
+ TypeOrmModule.forFeature([Webhook, WebhookDeliveryFailure, IntegrationDeliveryFailure, IngressEvent], 'data'),
+ // Required for WebhookProcessor to inject the one WebhookOutboxService the webhook module also uses.
+ WebhookOutboxModule,
// Required for WebhookProcessor/IngressProcessor to inject HookManager
HooksModule,
// Required for IngressProcessor to inject PluginLoaderService (already @Global(), imported
@@ -78,7 +76,7 @@ BullModule.queueClass = ShutdownSafeQueue;
adapter: BullMQAdapter,
}),
],
- providers: [WebhookProcessor, IngressProcessor, WebhookOutboxService],
+ providers: [WebhookProcessor, IngressProcessor],
exports: [BullModule],
})
export class QueueModule {}
diff --git a/src/modules/webhook/webhook-outbox.module.ts b/src/modules/webhook/webhook-outbox.module.ts
new file mode 100644
index 000000000..ab22b5bbd
--- /dev/null
+++ b/src/modules/webhook/webhook-outbox.module.ts
@@ -0,0 +1,16 @@
+import { Module } from '@nestjs/common';
+import { TypeOrmModule } from '@nestjs/typeorm';
+import { WebhookOutboxEvent } from './entities/webhook-outbox-event.entity';
+import { WebhookOutboxService } from './webhook-outbox.service';
+
+/**
+ * The one owner of WebhookOutboxService, which both WebhookModule and QueueModule need. QueueModule
+ * cannot import WebhookModule (WebhookModule imports it), and providing the service in each made two
+ * instances: the startup prune and the daily prune timer ran twice.
+ */
+@Module({
+ imports: [TypeOrmModule.forFeature([WebhookOutboxEvent], 'data')],
+ providers: [WebhookOutboxService],
+ exports: [WebhookOutboxService],
+})
+export class WebhookOutboxModule {}
diff --git a/src/modules/webhook/webhook.module.ts b/src/modules/webhook/webhook.module.ts
index 6c538f6e8..197d53676 100644
--- a/src/modules/webhook/webhook.module.ts
+++ b/src/modules/webhook/webhook.module.ts
@@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Webhook } from './entities/webhook.entity';
import { WebhookDeliveryFailure } from './entities/webhook-delivery-failure.entity';
import { WebhookOutboxEvent } from './entities/webhook-outbox-event.entity';
-import { WebhookOutboxService } from './webhook-outbox.service';
+import { WebhookOutboxModule } from './webhook-outbox.module';
import { WebhookReconcilerService } from './webhook-reconciler.service';
import { Session } from '../session/entities/session.entity';
import { WebhookService } from './webhook.service';
@@ -26,17 +26,12 @@ if (process.env.QUEUE_ENABLED === 'true') {
@Module({
imports: [
TypeOrmModule.forFeature([Webhook, WebhookDeliveryFailure, WebhookOutboxEvent, Session], 'data'),
+ WebhookOutboxModule,
EngineModule,
...queueModules,
],
controllers: [WebhookController, WebhooksListController],
- providers: [
- WebhookService,
- WebhookDeliveryService,
- WebhookOutboxService,
- WebhookReconcilerService,
- WebhookRedriveService,
- ],
+ providers: [WebhookService, WebhookDeliveryService, WebhookReconcilerService, WebhookRedriveService],
exports: [WebhookService],
})
export class WebhookModule {}