Retrying job in nestjs bullmq

200 Views Asked by At

I am new to bullmq in nestjs so I have code as following:

@Processor(POST_QUEUES.PARSE_POST_QUEUE, { concurrency: 4 })
export class ParsePostConsumer extends WorkerHost {
  constructor(
    private readonly telegramService: TelegramAccountService,
    private readonly postService: PostService,
  ) {
    super();
  }

  public async process(job: Job<ParsePostJobData>) {
    try {
      const tgAccount = await this.telegramService.getAccountForWork({
        cooldownTimeSinceLastUse: ApiCooldownTime.PARSE_POST_COOLDOWN,
      });

      const postsData =(
        await this.postService.getChannelPosts(
          tgAccount,
        )
      );


      await this.telegramService.setAccountAvailable(tgAccount);



    } catch (error) {
      await handleJobError(job, error, ApiCooldownTime.PARSE_POST_COOLDOWN);
    }
}

And my handleJobError function as following:

export async function handleJobError(job: Job, error: any, cooldownTime: number) {
  if (error instanceof AccountNotFoundException) {
    await job.moveToDelayed(Date.now() + cooldownTime);
  } else if (error instanceof AccountLockFailException) {
    
    await job.moveToDelayed(Date.now() + 1);

  }
  throw error;
}

So, what I want here is to retry my job for particular exceptions, for one exception retry immediately, for another retry after particular time. I do not think my code is correct above as when the job moves to delayed there is following exception I get:

Error: Missing lock for job 145. failed
    at Scripts.finishedErrors (/node_modules/bullmq/src/classes/scripts.ts:338:16)
    at Job.moveToFailed (/node_modules/bullmq/src/classes/job.ts:662:26)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at handleFailed (/node_modules/bullmq/src/classes/worker.ts:661:11)
    at Worker.retryIfFailed (/node_modules/bullmq/src/classes/worker.ts:879:16)

Could someone show me the better way of retrying jobs with the behaviour I want above

0

There are 0 best solutions below