Manually fetching jobs

If you want to manually fetch the jobs from the queue instead of letting the automatic processor taking care of it, this pattern is for you.

Manually transitioning states for jobs can be done with a few simple methods.

  1. Adding a job to the 'waiting' queue. Grab the queue and call add.

import Queue from 'bull';

const queue = new Queue({
  limiter: {
    max: 5,
    duration: 5000,
    bounceBack: true // important
  },
  ...queueOptions
});
queue.add({ random_attr: 'random_value' });
  1. Pulling a job from 'waiting' and moving it to 'active'.

const job: Job = await queue.getNextJob();
  1. Move the job to the 'failed' queue if something goes wrong.

const (nextJobData, nextJobId) = await job.moveToFailed(
  {
    message: 'Call to external service failed!',
  },
  true,
);
  1. Move the job to the 'completed' queue.

const (nextJobData, nextJobId) = await job.moveToCompleted('succeeded', true);
  1. Return the next job if one is returned.

if (nextJobdata) {
  return Job.fromJSON(queue, nextJobData, nextJobId);
}

Note

By default the lock duration for a job that has been returned by getNextJob or moveToCompleted is 30 seconds. If it takes more time than that the job will be automatically marked as stalled and depending on the max stalled options be moved back to the wait state or marked as failed. In order to avoid this you must use job.extendLock(duration) in order to give you some more time before the lock expires. It is recommended to extend the lock when half the lock time has passsed.

Last updated

Copyright (c) Taskforce.sh Inc.