Nestjs Schedule execute the queue one after another

3.4k Views Asked by At

I've 2 methods for importing products and inventory from json/csv. I've implemented the NestJS Bull Module for queuing the jobs. Both importing processes are running asynchronously and working fine. But Now I want to process the products import queue completely first and then only process the inventory import queue.

jobs.module.ts

BullModule.registerQueueAsync({
  name: 'default',
  inject: [ConfigService],
  useFactory: async (
    configService: ConfigService,
  ): Promise<BullModuleOptions> => ({
    redis: {
      ...
    },
    defaultJobOptions: {
      attempts: 1,
    },
  }),
})

jobs.service.ts

constructor(@InjectQueue('default') private defaultQueue: Queue) { }

@Cron(CronExpression.EVERY_30_MINUTES)
async queueProductUpdateJob() {
    await this.defaultQueue.add('productUpdate');
}

@Cron(CronExpression.EVERY_30_MINUTES)
async queueInventoryUpdateJob() {
    await this.defaultQueue.add('inventoryUpdate');
}

jobs.processor.ts

@Process('productUpdate')
async productUpdate(job: Job<unknown>){
  console.log('data: ', JSON.stringify(job.data));
  await this.updateProductService.importProducts(job.data);
  return {};
}

@Process('inventoryUpdate')
async inventoryUpdate(job: Job<unknown>){
  console.log('data: ', JSON.stringify(job.data));
  await this.updateInventoryService.importInventory(job.data);
  return {};
}

How can we I process the queueProductUpdateJob job completely first then only process queueInventoryUpdateJob later?

1

There are 1 best solutions below

0
On

i think if you change jobs.service.ts like this it will be ok: (because both of them corn every 30 minutes)

jobs.service.ts

constructor(@InjectQueue('default') private defaultQueue: Queue) { }

@Cron(CronExpression.EVERY_30_MINUTES)
async queueProductInventoryUpdateJob() {
    await this.defaultQueue.add('productUpdate');
    await this.defaultQueue.add('inventoryUpdate');
}

or you can do it by using "import { queue } from 'async';" (if want tell me to explain more) it will make a queue and do jobs after each other