Home

Awesome

simple-redis-safe-work-queue

Build Status

Gitter chat

A work queue for Node.js producers and consumers that uses Redis.

Client

Example of a client producing work:

var Queue = require('simple-redis-safe-work-queue');

client = Queue.client('send-email');
client.push({to: 'someone@somewhere.com', subject: 'hey there', body: 'yo'});

or, with a callback for when the work is pushed:

client.push({to: 'someone@somewhere.com', subject: 'hey there', body: 'yo'}, function(err) {
  if (err) console.error('Error when pushing work into the queue: ', err.stack);
});

You can also provide some options for the work item you're pushing:

var options = {
  timeout: 120e3 // 120 seconds
};

client.push({to: 'someone@somewhere.com', subject: 'hey there', body: 'yo'}, options, function(err) {
  if (err) console.error('Error when pushing work into the queue: ', err.stack);
});

Client options:

Client also accepts options as second argument in constructor:

Client events:

Client emmits the following events:

Worker

Example of a worker consuming work:

var Queue = require('simple-redis-safe-work-queue');

worker = Queue.worker('send-email', workFunction);

function workFunction(email, cb) {
  sendEmail(email, function(err) {
    if (err) cb(err);
    else {
      console.log('email send successfully, calling back with no errors');
      cb();
    }
  });
}

Worker options:

You can pass some options on the third argument of the worker constructor:

The .fetch() method is a non-long-polling version of .listen(), useful when you want to get a null response if there are no messages, rather than waiting for one to be pushed. If you're using autoListen: false then you probably want to use .fetch().

Worker Events:

A worker emits the following events:

Requirements

Redis 2.6 or greater, with Lua scripting enabled.

Testing

Clone this repo, enter the repo directory, start a redis server and run:

$ npm test

License

MIT