Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added ability to register workers manually #2038

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions packages/bullmq/e2e/module.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
QueueEventsHost,
QueueEventsListener,
WorkerHost,
BullRegistrator,
} from '../lib';

jest.setTimeout(10000);
Expand Down Expand Up @@ -677,6 +678,148 @@ describe('BullModule', () => {
});
});

describe('manual registration', () => {
const queueName = 'a_queue';

it('should manually register workers - forRoot', (done) => {
const processorCalledSpy = jest.fn();
const queueCompletedEventSpy = jest.fn();
const workerCompletedEventSpy = jest.fn();

@QueueEventsListener(queueName)
class EventsListener extends QueueEventsHost {
@OnQueueEvent('completed')
onCompleted() {
queueCompletedEventSpy();
}
}

@Processor(queueName)
class TestProcessor extends WorkerHost {
async process(job: Job<any, any, string>): Promise<any> {
await new Promise((resolve) => setTimeout(resolve, 1500));
processorCalledSpy();
}

@OnWorkerEvent('completed')
onCompleted() {
workerCompletedEventSpy();
}
}

Test.createTestingModule({
imports: [
BullModule.forRoot({
connection: {
host: '0.0.0.0',
port: 6380,
},
extraOptions: {
manualRegistration: true,
},
}),
BullModule.registerQueue({
name: queueName,
}),
],
providers: [EventsListener, TestProcessor],
})
.compile()
.then(async (testingModule) => {
await testingModule.init();

expect(() => testingModule.get(TestProcessor).worker).toThrow(
'"Worker" has not yet been initialized. Make sure to interact with worker instances after the "onModuleInit" lifecycle hook is triggered for example, in the "onApplicationBootstrap" hook, or if "manualRegistration" is set to true make sure to call "BullRegistrator.register()"',
);
expect(() => testingModule.get(EventsListener).queueEvents).toThrow(
'"QueueEvents" class has not yet been initialized. Make sure to interact with queue events instances after the "onModuleInit" lifecycle hook is triggered, for example, in the "onApplicationBootstrap" hook, or if "manualRegistration" is set to true make sure to call "BullRegistrator.register()"',
);

const bullRegistrator = testingModule.get(BullRegistrator);
bullRegistrator.register();

const processorWorker = testingModule.get(TestProcessor).worker;
const queueEvents = testingModule.get(EventsListener).queueEvents;

expect(processorWorker).toBeDefined();
expect(queueEvents).toBeDefined();

await testingModule.close();
done();
});
});

it('should manually register workers - forRootAsync', (done) => {
const processorCalledSpy = jest.fn();
const queueCompletedEventSpy = jest.fn();
const workerCompletedEventSpy = jest.fn();

@QueueEventsListener(queueName)
class EventsListener extends QueueEventsHost {
@OnQueueEvent('completed')
onCompleted() {
queueCompletedEventSpy();
}
}

@Processor(queueName)
class TestProcessor extends WorkerHost {
async process(job: Job<any, any, string>): Promise<any> {
await new Promise((resolve) => setTimeout(resolve, 1500));
processorCalledSpy();
}

@OnWorkerEvent('completed')
onCompleted() {
workerCompletedEventSpy();
}
}

Test.createTestingModule({
imports: [
BullModule.forRootAsync({
useFactory: () => ({
connection: {
host: '0.0.0.0',
port: 6380,
},
}),
extraOptions: {
manualRegistration: true,
},
}),
BullModule.registerQueue({
name: queueName,
}),
],
providers: [EventsListener, TestProcessor],
})
.compile()
.then(async (testingModule) => {
await testingModule.init();

expect(() => testingModule.get(TestProcessor).worker).toThrow(
'"Worker" has not yet been initialized. Make sure to interact with worker instances after the "onModuleInit" lifecycle hook is triggered for example, in the "onApplicationBootstrap" hook, or if "manualRegistration" is set to true make sure to call "BullRegistrator.register()"',
);
expect(() => testingModule.get(EventsListener).queueEvents).toThrow(
'"QueueEvents" class has not yet been initialized. Make sure to interact with queue events instances after the "onModuleInit" lifecycle hook is triggered, for example, in the "onApplicationBootstrap" hook, or if "manualRegistration" is set to true make sure to call "BullRegistrator.register()"',
);

const bullRegistrator = testingModule.get(BullRegistrator);
bullRegistrator.register();

const processorWorker = testingModule.get(TestProcessor).worker;
const queueEvents = testingModule.get(EventsListener).queueEvents;

expect(processorWorker).toBeDefined();
expect(queueEvents).toBeDefined();

await testingModule.close();
done();
});
});
});

describe('handles all kind of valid processors providers', () => {
@Processor('test_processor_registering')
class MyProcessorA extends WorkerHost {
Expand Down
4 changes: 2 additions & 2 deletions packages/bullmq/lib/bull.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { NestQueueOptions } from './interfaces/queue-options.interface';
import { NestWorkerOptions } from './interfaces/worker-options.interface';

@Injectable()
export class BullExplorer implements OnModuleInit {
export class BullExplorer {
private static _workerClass: Type = Worker;
private readonly logger = new Logger('BullModule');
private readonly injector = new Injector();
Expand All @@ -46,7 +46,7 @@ export class BullExplorer implements OnModuleInit {
private readonly metadataScanner: MetadataScanner,
) {}

onModuleInit() {
register() {
this.registerWorkers();
this.registerQueueEventListeners();
}
Expand Down
Loading