Comprehensive Guide to Kue Task Queue Management

Introduction to Kue

Kue is a priority job queue backed by Redis that is built for node.js. Kue manages jobs, supports job scheduling, and offers a web interface for job monitoring. This makes it an excellent choice for handling background tasks, processing jobs, and managing workflows in node.js applications.

Getting Started with Kue

First, install Kue using npm:

 npm install kue

Next, create a Kue instance and start processing jobs:

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

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

   function sendEmail(data, done) {
     // email logic
     console.log('Sending email to:', data.to);
     done();
   }
 

Creating Jobs

You can create jobs and add them to the queue. Here’s how:

 
   const job = queue.create('email', {
     to: 'user@example.com',
     subject: 'Welcome!',
     body: 'Thank you for signing up!'
   }).save(err => {
     if (!err) console.log('Job saved:', job.id);
   });
 

Handling Job Events

Kue allows you to handle various job events such as job completion, failure, etc. Here are some examples:

 
   queue.on('job complete', (id, result) => {
     kue.Job.get(id, (err, job) => {
       if (err) return;
       job.remove(err => {
         if (err) throw err;
         console.log('Removed completed job:', job.id);
       });
     });
   });

   queue.on('job failed', (id, errorMessage) => {
     console.log('Job failed:', id, errorMessage);
   });

   queue.on('job retry', (id) => {
     console.log('Job retry:', id);
   });
 

Scheduling Jobs

You can also schedule jobs to be processed in the future:

 
   const job = queue.create('email', {
     to: 'user@example.com',
     subject: 'Reminder!',
     body: 'This is a reminder email.'
   }).delay(60000) // 1-minute delay
   .save();
 

Priority Jobs

Set priority for the jobs:

 
   const job = queue.create('email', {
     to: 'user@example.com',
     subject: 'Urgent!',
     body: 'This is an urgent email.'
   }).priority('high')
   .save();
 

Job Queue UI

Kue provides a web interface for monitoring jobs. Here’s how to set it up:

 
   kue.app.listen(3000);
   console.log('UI serving at: http://localhost:3000');
 

Complete App Example

Here’s a complete example of a node.js app using Kue:

 
   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);
     done();
   }

   queue.on('job complete', (id, result) => {
     kue.Job.get(id, (err, job) => {
       if (err) return;
       job.remove((err) => {
         if (err) throw err;
         console.log('Removed completed job:', job.id);
       });
     });
   });

   queue.create('email', {
     to: 'user@example.com',
     subject: 'Welcome!',
     body: 'Thank you for signing up!'
   }).save();

   kue.app.listen(3000);
   console.log('UI serving at: http://localhost:3000');
 

Hash: 4555a0b49453e7e3eeecc511fac9c38f5d24a31c97d21d77a4fd9da0754965bc

Leave a Reply

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