Since I don't have any reputation I wanted to reply about running the CLI worker without exposing endpoints, you can certainly do that.
I use pm2 to run multiple workers.
Notice in the worker.ts I use createApplicationContext and don't run app.listen(...)
worker.ts
import { NestFactory } from '@nestjs/core'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { WorkersModule } from './services/queue/workers-email/workers.module'
async function bootstrap() {
const app = await NestFactory.createApplicationContext(WorkersModule)
const config = app.get(ConfigService)
ConfigModule.forRoot({ isGlobal: true })
app.useLogger(
config.get<string>('NODE_ENV') === 'development'
? ['log', 'debug', 'error', 'verbose', 'warn']
: ['log', 'error', 'warn'],
)
}
bootstrap()
worker.module.ts
import { BullModule } from '@nestjs/bullmq'
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { redisFactory } from '../../../factories/redis.factory'
import { EmailProcessor } from './email.workers.processor'
import { EQueue } from '../../../entities/enum/job.enum'
import { TypeOrmModule } from '@nestjs/typeorm'
@Module({
imports: [
BullModule.forRootAsync({
imports: [ConfigModule],
useFactory: redisFactory,
inject: [ConfigService],
}),
BullModule.registerQueueAsync({ name: 'queue-name' }),
],
providers: [
ConfigService,
workerProcessor,
],
})
export class WorkersModule {}
workers.processor.ts
import { OnWorkerEvent, Processor, WorkerHost } from '@nestjs/bullmq'
import { Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Job } from 'bullmq'
@Processor('queue-name')
export class workerProcessor extends WorkerHost {
private logger = new Logger('processor')
constructor(private config: ConfigService) {
super()
}
async process(job: Job<any, any, string>): Promise<any> {
... process here ...
}
@OnWorkerEvent('completed')
onCompleted(job: Job<anu, any, string>) {
this.logger.log(`Job ${job.id} ${job.name.toUpperCase()} Completed`)
}
@OnWorkerEvent('failed')
onFailed(job: Job<any, any, string>) {
this.logger.error(`Job ${job.id} ${job.name.toUpperCase()} Failed`)
}
}