BullMQ Pro with NestJS
BullMQ Pro integrates with NestJS through @taskforcesh/nestjs-bullmq-pro, a module based on @nestjs/bullmq that registers the BullMQ Pro classes, so that your processors and services can use groups, batches and the other Pro features. If you use the open source version of BullMQ, see BullMQ with NestJS.
Installation
yarn add @taskforcesh/nestjs-bullmq-proINFO
BullMQ-Pro needs a token, as explained in the install section.
Once the installation process is complete, we can import the BullModule into the root AppModule.
import { Module } from '@nestjs/common';
import { BullModule } from '@taskforcesh/nestjs-bullmq-pro';
@Module({
imports: [
BullModule.forRoot({
connection: {
host: 'localhost',
port: 6379,
},
}),
],
})
export class AppModule {}To register a queue, import the BullModule.registerQueue() dynamic module, as follows:
BullModule.registerQueue({
name: 'queueName',
});To register a flow producer, import the BullModule.registerFlowProducer() dynamic module, as follows:
BullModule.registerFlowProducer({
name: 'flowProducerName',
});Processors
To register a processor, you may need to use Processor decorator:
import {
Processor,
WorkerHost,
OnWorkerEvent,
} from '@taskforcesh/nestjs-bullmq-pro';
import { JobPro } from '@taskforcesh/bullmq-pro';
@Processor('queueName')
class TestProcessor extends WorkerHost {
async process(job: JobPro<any, any, string>): Promise<any> {
// do some stuff
}
@OnWorkerEvent('completed')
onCompleted() {
// do some stuff
}
}And then register it as a provider:
@Module({
imports: [
BullModule.registerQueue({
name: 'queueName',
connection: {
host: '0.0.0.0',
port: 6380,
},
}),
BullModule.registerFlowProducer({
name: 'flowProducerName',
connection: {
host: '0.0.0.0',
port: 6380,
},
}),
],
providers: [TestProcessor],
})
export class AppModule {}Example
A working example is available here.