I have a nestjs
application which is exposing a few REST API
s. One of the API
s triggers a job which processes some tasks. The problem is that when the job gets triggered the application stops serving REST requests which leads to health check failures from load balancer. I followed the method given at the end of the README to start a separate child process for processing jobs. But, the job doesn't start in a child process and the API
requests stall.
Here's my Job:
import {
BullQueueEvents,
OnQueueActive,
OnQueueEvent,
Process,
Processor,
} from 'nest-bull';
import { Job } from 'bull';
import { Logger } from '@nestjs/common';
import { AService } from './a-service';
import { AJobInterface } from '../AJobInterface';
@Processor({ name: 'a_queue' })
export class AJob {
private readonly logger = new Logger('AQueue');
constructor(private readonly service: AService) {}
@Process({
name: 'app',
concurrency: 1
})
processApp(job: Job<AJobInterface>) {
console.log('CHILD: ', process.pid);
const { jobId } = job.data;
return this.service.process(jobId);
}
@OnQueueActive()
onActive(job: Job) {
this.logger.log(
`Processing job ${job.id} of type ${job.name} with data ${JSON.stringify(
job.data,
)}...`,
);
}
@OnQueueEvent(BullQueueEvents.COMPLETED)
onCompleted(job: Job) {
this.logger.log(
`Completed job ${job.id} of type ${job.name} with result ${job.returnvalue}`,
);
}
}
Here's my app.module.ts:
import { Module, OnModuleInit } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './db/module';
import { BullModule } from 'nest-bull';
import { AJob } from './worker/a-job';
import { AService } from './worker/a-service';
import { join } from 'path';
@Module({
imports: [
TypeOrmModule.forRoot(),
DatabaseModule,
BullModule.register({
name: 'a_queue',
processors: [ join(__dirname, 'worker/a-job.js') ],
options: {
redis: {
host: process.env.REDIS_URL || '127.0.0.1',
port: 6379,
showFriendlyErrorStack: true,
},
settings: {
lockDuration: 300000,
stalledInterval: 300000
},
},
}),
],
controllers: [AppController],
providers: [AppService, AJob, AService],
})
export class AppModule implements OnModuleInit {
onModuleInit() {
console.log('MAIN: ', process.pid);
}
}
Is there anything that I'm doing wrong?
Sorry for posting the answer so late. It turns out that the setup of having a worker in a child process was not possible. I ended up having a separate
worker.module.ts
and a separateworker.ts
and creating two separate processes for API and worker.worker.module.ts
:worker.ts
:While
app.module.ts
now looks like this:and the corresponding
app.ts
: