Node.js Bee-queue tutorial – NPM package for Queues

In the Node.js Bee-queue tutorial, I have covered he key features of bee-queue include job prioritization, concurrency control, and it’s well-suited for distributed applications. Bee-Queue is a lightweight, high-performance Redis-backed job queue for Node.js. It is designed with simplicity and efficiency in mind and is suitable for jobs that need to be processed in the background without blocking the main application flow.

Node.js Bee-queue tutorial

Table of Contents

 

Installation

To start using the npm package bee-queue in your Node.js project, you first need to install it via npm:

npm install bee-queue

Creating Jobs

To create a job in bee-queue, you set up a queue, create a job with data, and save it. Here’s a simple example:

const Queue = require('bee-queue');
const myQueue = new Queue('example');

function createJob(data) {
  const job = myQueue.createJob(data);
  job.save();
  job.on('succeeded', (result) => {
    console.log(`Job ${job.id} succeeded with result: ${result}`);
  });
}

createJob({x: 2, y: 3});  // Function call with data

Output:

`Job 1 succeeded with result: 5`

Repeated Jobs

To set up repeated jobs, you need to define the job and instruct the queue on how often it should repeat:

// This functionality is not a part of bee-queue by default. You will need to implement a scheduler.
// The following is a conceptual example.
function createRepeatedJob(data, interval) {
  setInterval(() => {
    const job = myQueue.createJob(data);
    job.save();
  }, interval);
}

createRepeatedJob({ task: 'heartbeat' }, 60000);  // Call function to create a job every minute

Delayed Jobs

To create a delayed job, you must specify the delay before your job starts processing:

function createDelayedJob(data, delay) {
  const job = myQueue.createJob(data).delayUntil(Date.now() + delay);
  job.save();
}

createDelayedJob({ email: '[email protected]' }, 5000);  // Call function to delay job by 5 seconds

Setting Job Priority

You can set a priority for each job, which determines the order in which jobs are processed:

function createJobWithPriority(data, priority) {
  const job = myQueue.createJob(data).priority(priority);
  job.save();
}

createJobWithPriority({ email: '[email protected]' }, 'high');  // Call function with high priority

Managing Concurrency

bee-queue allows you to control the number of concurrent jobs being processed:

const queueWithConcurrency = new Queue('concurrent', { 
  settings: { 
    activateDelayedJobs: true,
    concurrency: 5
  } 
});

queueWithConcurrency.process(function (job, done) {
  console.log(`Processing job ${job.id}`);
  done();
});

Global Events

Global events can be listened to for various queue activities. Here’s how you do it:

myQueue.on('ready', () => {
  console.log('Queue is ready!');
});

myQueue.on('error', (err) => {
  console.log('A queue error happened:', err);
});

myQueue.on('succeeded', (job, result) => {
  console.log(`Job ${job.id} succeeded with result: ${result}`);
});

 

Pausing and Resuming Queues

bee-queue allows you to pause and resume the processing of jobs:

async function pauseQueue() {
  await myQueue.pause(false, true);
}

async function resumeQueue() {
  await myQueue.resume();
}

pauseQueue(); // Pauses the queue without waiting for ongoing jobs
resumeQueue(); // Resumes job processing in the queue

Job Retries

You can set a number of retries for a job in case it fails:

function createJobWithRetries(data, retries) {
  const job = myQueue.createJob(data).retries(retries);
  job.save();
}

createJobWithRetries({ task: 'important' }, 3);  // Call function that retries job up to 3 times

Troubleshooting Tips

If you encounter issues while using bee-queue, check for these common problems:

  • Ensure Redis server is running and accessible.
  • Check for the correct version of Node.js and Redis compatibility.
  • Validate that there are no errors in your job processors.
  • Check the documentation or issues on the bee-queue GitHub repository for similar problems and solutions.

Summary

In this tutorial, we have discussed how to use bee-queue for queue management in Node.js applications, covering features such as creating, delaying, and prioritizing jobs; setting up concurrency and global events; rate limiting; as well as pausing, resuming, and retrying jobs. Bee-queue is a powerful yet simple tool that can help you efficiently manage background tasks in your Node.js projects.

References