Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(queue): pin plugin init before the queue workers start

Yudhi Armyndharisa168f6fdb314
diff --git a/src/app.module.ts b/src/app.module.ts
index 840a3bda1..3a71685a9 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -266,7 +266,12 @@ if (dashboardServingEnabled && dashboardBuildPresent) {
// Core modules
HooksModule, // Global hook system for plugin integration
- PluginsModule, // Global plugin system
+ // Global plugin system, kept ahead of ...queueModules. Nest runs the init hooks of global modules in the
+ // order it first meets them among the imports, and PluginLoaderService.onModuleInit must mark the plugins
+ // it restores before BullRegistrar starts the workers that take queued ingress jobs. This line and
+ // QueueModule's own import of PluginsModule each keep it ahead of BullModule; test/queue-on.e2e-spec.ts
+ // fails when neither does.
+ PluginsModule,
LoggerModule,
CacheModule,
StorageModule,
diff --git a/src/modules/queue/queue.module.ts b/src/modules/queue/queue.module.ts
index 88e7074ff..c9464a37d 100644
--- a/src/modules/queue/queue.module.ts
+++ b/src/modules/queue/queue.module.ts
@@ -41,8 +41,9 @@ BullModule.queueClass = ShutdownSafeQueue;
WebhookOutboxModule,
// Required for WebhookProcessor/IngressProcessor to inject HookManager
HooksModule,
- // Required for IngressProcessor to inject PluginLoaderService (already @Global(), imported
- // explicitly for clarity, matching HooksModule above).
+ // Required for IngressProcessor to inject PluginLoaderService (already @Global()). Imported ahead of
+ // BullModule so the loader's init hook, which marks the plugins being restored, runs before the
+ // workers start (see app.module.ts).
PluginsModule,
BullModule.forRootAsync({
useFactory: queueRootOptions,
diff --git a/test/queue-on.e2e-spec.ts b/test/queue-on.e2e-spec.ts
index b71307cf7..e5afb953d 100644
--- a/test/queue-on.e2e-spec.ts
+++ b/test/queue-on.e2e-spec.ts
@@ -53,7 +53,7 @@ import { ConfigService } from '@nestjs/config';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getQueueToken } from '@nestjs/bullmq';
import { Repository } from 'typeorm';
-import { Queue } from 'bullmq';
+import { Queue, Worker } from 'bullmq';
import IORedis from 'ioredis';
import request from 'supertest';
import { App } from 'supertest/types';
@@ -119,6 +119,7 @@ describeQueueOn('Queued dispatch paths (e2e, QUEUE_ENABLED=true)', () => {
let received: Array<{ headers: http.IncomingHttpHeaders; body: Record<string, unknown> }>;
let receiver: http.Server;
let receiverUrl: string;
+ let bootOrder: { loaderInit: number[]; workerRun: number[] };
const prevSsrf = process.env.WEBHOOK_SSRF_PROTECT;
// Chatwoot-style signed ingress route, mirroring the integration-fabric suite byte-for-byte so the
@@ -150,6 +151,9 @@ describeQueueOn('Queued dispatch paths (e2e, QUEUE_ENABLED=true)', () => {
// The 127.0.0.1 receiver would trip the SSRF guard at registration AND at delivery time; disable
// it for this suite like the webhooks suite does (restored in afterAll).
process.env.WEBHOOK_SSRF_PROTECT = 'false';
+ // Both call through; jest numbers every mock call in one sequence, which gives the boot order.
+ const loaderInit = jest.spyOn(PluginLoaderService.prototype, 'onModuleInit');
+ const workerRun = jest.spyOn(Worker.prototype, 'run');
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
@@ -175,6 +179,12 @@ describeQueueOn('Queued dispatch paths (e2e, QUEUE_ENABLED=true)', () => {
}),
);
await app.init();
+ bootOrder = {
+ loaderInit: [...loaderInit.mock.invocationCallOrder],
+ workerRun: [...workerRun.mock.invocationCallOrder],
+ };
+ loaderInit.mockRestore();
+ workerRun.mockRestore();
// Both queues resolve from the same container the workers consume — a boot that fails here is
// itself the tripwire signal (IngressEnqueueService refuses to boot queue-on without the queue).
@@ -243,6 +253,14 @@ describeQueueOn('Queued dispatch paths (e2e, QUEUE_ENABLED=true)', () => {
expect(webhookQueue).toBeDefined();
});
+ it('marks the plugins it will restore before any queue worker starts', () => {
+ // A worker running first could take a queued ingress job for a plugin not yet marked as restoring and
+ // spend its only attempt on a missing plugin worker. The order comes from AppModule's imports.
+ expect(bootOrder.loaderInit).toHaveLength(1);
+ expect(bootOrder.workerRun.length).toBeGreaterThan(0);
+ expect(bootOrder.loaderInit[0]).toBeLessThan(Math.min(...bootOrder.workerRun));
+ });
+
it('enqueues the ingress delivery and the worker dispatches it', async () => {
// Unique per run: the enqueue derives the jobId from deliveryId and BullMQ dedups a repeated jobId
// while the previous job still lingers (auto-evicted, 1h window) — a fixed id would make a fast local