Question: What are the job options in BullMQ and how can they be used?

Answer

In BullMQ, a job is a unit of work that can be processed by a worker. When creating a new job in BullMQ, there are several options you can specify to configure its behavior:

  1. delay: This option allows you to delay the execution of the job by a certain amount of milliseconds.

  2. attempts: This option lets you specify the number of attempts to perform if the job fails.

  3. backoff: This option can be used to control the backoff strategies on retries.

  4. lifo: If set to true, the job will be placed last in the queue (last in, first out).

  5. timeout: This represents the max amount of time in milliseconds a job can be active. If exceeded, the job fails.

  6. removeOnComplete: If true, removes the job when successfully completed.

  7. removeOnFail: If true, removes the job when it fails after all attempts.

  8. stackTraceLimit: Limits the amount of stack trace lines stored in case of failure.

Here's an example on how to use these options when adding a job:

const Queue = require('bullmq').Queue; const queue = new Queue('my-queue'); queue.add( 'my-job', { foo: 'bar' }, // Job data { delay: 5000, attempts: 3, backoff: { type: 'exponential', delay: 5000 }, lifo: false, timeout: 20000, removeOnComplete: true, removeOnFail: true, stackTraceLimit: 5 } );

This will add a job named 'my-job' with some data, and configure it with the provided options.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.