Comprehensive Guide to Kue Job Queue for Node.js

Introduction to Kue

Kue is a priority job queue backed by Redis, with a built-in UI, for Node.js. It enables you to manage job queues and supports delayed jobs, retries, and more. In this guide, we’ll cover some of the essential APIs provided by Kue and share practical code examples.

Installation

First, install Kue using npm:

  
  npm install kue
  

Basic Kue APIs

Creating a Queue

Initialize a redis-backed Kue job queue:

  
  const kue = require('kue');
  const queue = kue.createQueue();
  

Creating a Job

Create a new job in the queue:

  
  const job = queue.create('email', {
    title: 'Welcome email for new user',
    to: 'user@example.com',
    template: 'welcome-email'
  }).save((err) => {
    if (!err) console.log(job.id);
  });
  

Processing Jobs

Define how jobs of a certain type should be processed:

  
  queue.process('email', (job, done) => {
    sendEmail(job.data, done);
  });

  function sendEmail(data, done) {
    // Simulate email sending
    console.log('Email sent to:', data.to);
    done();
  }
  

Job Events

Subscribe to job-level events:

  
  job.on('complete', (result) => {
    console.log('Job complete with result:', result);
  }).on('failed', (err) => {
    console.log('Job failed with error:', err);
  });
  

Advanced Kue APIs

Delayed Jobs

Create jobs to run in the future:

  
  const delayedJob = queue.create('email', {
    title: 'Delayed welcome email',
    to: 'delayed-user@example.com',
    template: 'welcome-email'
  }).delay(3000).save();
  

Job Priority

Set job priority:

  
  const highPriorityJob = queue.create('email', {
    title: 'High priority email',
    to: 'priority-user@example.com',
    template: 'priority-email'
  }).priority('high').save();
  

Job Retry

Automatic job retries on failure:

  
  const retryJob = queue.create('email', {
    title: 'Retry email',
    to: 'retry-user@example.com',
    template: 'retry-email'
  })
  .attempts(3)
  .backoff({type: 'exponential'})
  .save();
  

Example Application

Here’s a full example of a Node.js application using Kue to manage an email queue:

  
  const kue = require('kue');
  const queue = kue.createQueue();

  queue.process('email', (job, done) => {
    sendEmail(job.data, done);
  });

  function sendEmail(data, done) {
    console.log('Sending email to:', data.to);
    setTimeout(() => {
      console.log('Email sent successfully to:', data.to);
      done();
    }, 1000);
  }

  // Create a new job
  const job = queue.create('email', {
    title: 'User registration email',
    to: 'newuser@example.com',
    template: 'registration-email'
  }).save((err) => {
    if (!err) console.log('Job created with ID:', job.id);
  });

  // Setup job event listeners
  job.on('complete', (result) => {
    console.log('Job completed with result:', result);
  }).on('failed', (err) => {
    console.log('Job failed with error:', err);
  }).on('progress', (progress) => {
    console.log('Job progress:', progress);
  });
  

And there you have it! A fully functional example of handling job queues using Kue in Node.js.

Hash: 4555a0b49453e7e3eeecc511fac9c38f5d24a31c97d21d77a4fd9da0754965bc

Leave a Reply

Your email address will not be published. Required fields are marked *