0

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?

Niroj
  • 23
  • 2
  • 8
  • Why don't you call `productUpdate` at the end of `queueInventoryUpdateJob` ? – omidh May 19 '21 at 17:00
  • You mean inventoryUpdate at the end of queueProductUpdateJob. Actually I need to process productUpdate first and then inventoryUpdate. I tried that but 2nd process jobs running simultaneously as the product import might have 100+ rows of data. – Niroj May 19 '21 at 17:09
  • So it might take more than 30 minutes? That's why another process runs? – omidh May 19 '21 at 17:15
  • Yes, I might take if the data rows are more. But what I'm looking for help is, whenever `product` job runs its should run `inventory` job to update those products stock. – Niroj May 19 '21 at 17:25

1 Answers1

-1

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

ehsan mandegar
  • 319
  • 1
  • 8