Kue is a priority job queue backed by redis, built for node.js.
PROTIP This is the latest Kue documentation, make sure to read the changelist for compatibility.
$ npm install kue
- Delayed jobs
- Distribution of parallel work load
- Job event and progress pubsub
- Rich integrated UI
- Infinite scrolling
- UI progress indication
- Job specific logging
- Powered by Redis
- Optional retries with backoff
- Full-text search capabilities
- RESTful JSON API
- Graceful workers shutdown
- Creating Jobs
- Jobs Priority
- Failure Attempts
- Failure Backoff
- Job Logs
- Job Progress
- Job Events
- Queue Events
- Delayed Jobs
- Processing Jobs
- Processing Concurrency
- Pause Processing
- Updating Progress
- Graceful Shutdown
- Redis Connection Settings
- User-Interface
- JSON API
- Parallel Processing With Cluster
- Securing Kue
- Screencasts
- License
First create a job Queue
with kue.createQueue()
:
var kue = require('kue')
, jobs = kue.createQueue();
Calling jobs.create()
with the type of job ("email"), and arbitrary job data will return a Job
, which can then be save()
ed, adding it to redis, with a default priority level of "normal". The save()
method optionally accepts a callback, responding with an error
if something goes wrong. The title
key is special-cased, and will display in the job listings within the UI, making it easier to find a specific job.
var job = jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).save( function(err){
if( !err ) console.log( job.id );
});
To specify the priority of a job, simply invoke the priority()
method with a number, or priority name, which is mapped to a number.
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).priority('high').save();
The default priority map is as follows:
{
low: 10
, normal: 0
, medium: -5
, high: -10
, critical: -15
};
By default jobs only have one attempt, that is when they fail, they are marked as a failure, and remain that way until you intervene. However, Kue allows you to specify this, which is important for jobs such as transferring an email, which upon failure, may usually retry without issue. To do this invoke the .attempts()
method with a number.
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).priority('high').attempts(5).save();
Job retry attempts are done as soon as they fail, with no delay, even if your job had a delay set via Job#delay
. If you want to delay job re-attempts upon failures (known as backoff) you can use Job#backoff
method in different ways:
// Honor job's original delay (if set) at each attempt, defaults to fixed backoff
job.attempts(3).backoff( true )
// Override delay value, fixed backoff
job.attempts(3).backoff( {delay: 60*1000, type:'fixed'} )
// Enable exponential backoff using original delay (if set)
job.attempts(3).backoff( {type:'exponential'} )
// Use a function to get a customized next attempt delay value
job.attempts(3).backoff( function( attempts, delay ){
return my_customized_calculated_delay;
})
In the last scenario, provided function will be executed (via eval) on each re-attempt to get next attempt delay value, meaning that you can't reference external/context variables within it.
Note that backoff feature depends on .delay
under the covers and therefore .promote()
needs to be called if used.
Job-specific logs enable you to expose information to the UI at any point in the job's life-time. To do so simply invoke job.log()
, which accepts a message string as well as variable-arguments for sprintf-like support:
job.log('$%d sent to %s', amount, user.name);
Job progress is extremely useful for long-running jobs such as video conversion. To update the job's progress simply invoke job.progress(completed, total)
:
job.progress(frames, totalFrames);
Job-specific events are fired on the Job
instances via Redis pubsub. The following events are currently supported:
- `enqueue` the job is now queued
- `promotion` the job is promoted from delayed state to queued
- `progress` the job's progress ranging from 0-100
- 'failed attempt' the job has failed, but has remaining attempts yet
- `failed` the job has failed and has no remaining attempts
- `complete` the job has completed
For example this may look something like the following:
var job = jobs.create('video conversion', {
title: 'converting loki\'s to avi'
, user: 1
, frames: 200
});
job.on('complete', function(result){
console.log("Job completed with data ", result);
}).on('failed', function(){
console.log("Job failed");
}).on('progress', function(progress){
process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
});
Note that Job level events are not guaranteed to be received upon worker process restarts, since the process will lose the reference to the specific Job object. If you want a more reliable event handler look for Queue Events.
Queue-level events provide access to the job-level events previously mentioned, however scoped to the Queue
instance to apply logic at a "global" level. An example of this is removing completed jobs:
jobs.on('job enqueue', function(id,type){
console.log( 'job %s got queued', id );
});
jobs.on('job complete', function(id,result){
kue.Job.get(id, function(err, job){
if (err) return;
job.remove(function(err){
if (err) throw err;
console.log('removed completed job #%d', job.id);
});
});
});
The events available are the same as mentioned in "Job Events", however prefixed with "job ".
Delayed jobs may be scheduled to be queued for an arbitrary distance in time by invoking the .delay(ms)
method, passing the number of milliseconds relative to now. This automatically flags the Job
as "delayed".
var email = jobs.create('email', {
title: 'Account renewal required'
, to: 'tj@learnboost.com'
, template: 'renewal-email'
}).delay(milliseconds)
.priority('high')
.save();
When using delayed jobs, we must also check the delayed jobs with a timer, promoting them if the scheduled delay has been exceeded. This setInterval
is defined within Queue#promote(ms,limit)
, defaulting to a check of top 200 jobs every 5 seconds. If you have a cluster of kue processes, you must call .promote
in just one (preferably master) process or promotion race can happen.
jobs.promote();
Processing jobs is simple with Kue. First create a Queue
instance much like we do for creating jobs, providing us access to redis etc, then invoke jobs.process()
with the associated type.
Note that unlike what the name createQueue
suggests, it currently returns a singleton Queue
instance. So you can configure and use only a single Queue
object within your node.js process.
In the following example we pass the callback done
to email
, When an error occurs we invoke done(err)
to tell Kue something happened, otherwise we invoke done()
only when the job is complete. If this function responds with an error it will be displayed in the UI and the job will be marked as a failure. The error object passed to done, should be of standard type Error
.
var kue = require('kue')
, jobs = kue.createQueue();
jobs.process('email', function(job, done){
email(job.data.to, done);
});
function email(address, done) {
if(!isValidEmail(address)) {
//done('invalid to address') is possible but discouraged
return done(new Error('invalid to address'));
}
// email send stuff...
done();
}
Workers can also pass job result as the second parameter to done done(null,result)
to store that in Job.result
key. result
is also passed through complete
event handlers so that job producers can receive it if they like to.
By default a call to jobs.process()
will only accept one job at a time for processing. For small tasks like sending emails this is not ideal, so we may specify the maximum active jobs for this type by passing a number:
jobs.process('email', 20, function(job, done){
// ...
});
Workers can temporary pause and resume their activity. It is, after calling pause
they will receive no jobs in their process callback until resume
is called. pause
function gracefully shutdowns this worker, and uses the same internal functionality as shutdown
method in Graceful Shutdown.
jobs.process('email', function(job, done, ctx){
ctx.pause( function(err){
console.log("Worker is paused... ");
setTimeout( function(){ ctx.resume(); }, 10000 );
}, 5000);
});
For a "real" example, let's say we need to compile a PDF from numerous slides with node-canvas. Our job may consist of the following data, note that in general you should not store large data in the job it-self, it's better to store references like ids, pulling them in while processing.
jobs.create('slideshow pdf', {
title: user.name + "'s slideshow"
, slides: [...] // keys to data stored in redis, mongodb, or some other store
});
We can access this same arbitrary data within a separate process while processing, via the job.data
property. In the example we render each slide one-by-one, updating the job's log and process.
jobs.process('slideshow pdf', 5, function(job, done){
var slides = job.data.slides
, len = slides.length;
function next(i) {
var slide = slides[i]; // pretend we did a query on this slide id ;)
job.log('rendering %dx%d slide', slide.width, slide.height);
renderSlide(slide, function(err){
if (err) return done(err);
job.progress(i, len);
if (i == len) done()
else next(i + 1);
});
}
next(0);
});
As of Kue 0.7.0, a Queue#shutdown(fn, timeout)
is added which signals all workers to stop processing after their current active job is done. Workers will wait timeout
milliseconds for their active job's done to be called or mark the active job failed
with shutdown error reason. When all workers tell Kue they are stopped fn
is called.
var queue = require('kue').createQueue();
process.once( 'SIGTERM', function ( sig ) {
queue.shutdown(function(err) {
console.log( 'Kue is shut down.', err||'' );
process.exit( 0 );
}, 5000 );
});
By default, Kue will connect to Redis using the client default settings (port defaults to 6379
, host defaults to 127.0.0.1
, prefix defaults to q
). Queue#createQueue(options)
accepts redis connection options in options.redis
key.
var kue = require('kue');
var q = kue.createQueue({
prefix: 'q',
redis: {
port: 1234,
host: '10.0.50.20',
auth: 'password',
db: 3, // if provided select a non-default redis db
options: {
// see https://github.com/mranney/node_redis#rediscreateclientport-host-options
}
}
});
prefix
controls the key names used in Redis. By default, this is simply q
. Prefix generally shouldn't be changed unless you need to use one Redis instance for multiple apps. It can also be useful for providing an isolated testbed across your main application.
Since node_redis supports Unix Domain Sockets, you can also tell Kue to do so. See unix-domain-socket for your redis server configuration.
var kue = require('kue');
var q = kue.createQueue({
prefix: 'q',
redis: {
socket: '/data/sockets/redis.sock',
auth: 'password',
options: {
// see https://github.com/mranney/node_redis#rediscreateclientport-host-options
}
}
});
Any node.js redis client library that conforms (or when adapted) to node_redis API can be injected into Kue. You should only provide a createClientFactory
function as a redis connection factory instead of providing node_redis connection options.
Below is a sample code to enable redis-sentinel to connect to Redis Sentinel for automatic master/slave failover.
var kue = require('kue');
var Sentinel = require('redis-sentinel');
var endpoints = [
{host: '192.168.1.10', port: 6379},
{host: '192.168.1.11', port: 6379}
];
var opts = options || {}; // Standard node_redis client options
var masterName = 'mymaster';
var sentinel = Sentinel.Sentinel(endpoints);
var q = kue.createQueue({
redis: {
createClientFactory: function(){
return sentinel.createClient(masterName, opts);
}
}
});
Note that all <0.8.x
client codes should be refactored to pass redis options to Queue#createQueue
instead of monkey patched style overriding of redis#createClient
or they will be broken from Kue 0.8.x
.
The UI is a small Express application, to fire it up simply run the following, altering the port etc as desired.
var kue = require('kue');
kue.createQueue(...);
kue.app.listen(3000);
The title defaults to "Kue", to alter this invoke:
kue.app.set('title', 'My Application');
Note that if you are using non-default Kue options, kue.createQueue(...)
must be called before accessing kue.app
.
Along with the UI Kue also exposes a JSON API, which is utilized by the UI.
Query jobs, for example "GET /job/search?q=avi video":
["5", "7", "10"]
By default kue indexes the whole Job data object for searching, but this can be customized via calling Job#searchKeys
to tell kue which keys on Job data to create index for:
var kue = require('kue');
jobs = kue.createQueue();
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj@learnboost.com'
, template: 'welcome-email'
}).searchKeys( ['to', 'title'] ).save();
You may also fully disable search indexes for redis memory optimization:
var kue = require('kue');
q = kue.createQueue({
disableSearch: true
});
Currently responds with state counts, and worker activity time in milliseconds:
{"inactiveCount":4,"completeCount":69,"activeCount":2,"failedCount":0,"workTime":20892}
Get a job by :id
:
{"id":"3","type":"email","data":{"title":"welcome email for tj","to":"tj@learnboost.com","template":"welcome-email"},"priority":-10,"progress":"100","state":"complete","attempts":null,"created_at":"1309973155248","updated_at":"1309973155248","duration":"15002"}
Get job :id
's log:
['foo', 'bar', 'baz']
Get jobs with the specified range :from
to :to
, for example "/jobs/0..2", where :order
may be "asc" or "desc":
[{"id":"12","type":"email","data":{"title":"welcome email for tj","to":"tj@learnboost.com","template":"welcome-email"},"priority":-10,"progress":0,"state":"active","attempts":null,"created_at":"1309973299293","updated_at":"1309973299293"},{"id":"130","type":"email","data":{"title":"welcome email for tj","to":"tj@learnboost.com","template":"welcome-email"},"priority":-10,"progress":0,"state":"active","attempts":null,"created_at":"1309975157291","updated_at":"1309975157291"}]
Same as above, restricting by :state
which is one of:
- active
- inactive
- failed
- complete
Same as above, however restricted to :type
and :state
.
Delete job :id
:
$ curl -X DELETE http://local:3000/job/2
{"message":"job 2 removed"}
Create a job:
$ curl -H "Content-Type: application/json" -X POST -d \
'{
"type": "email",
"data": {
"title": "welcome email for tj",
"to": "tj@learnboost.com",
"template": "welcome-email"
},
"options" : {
"attempts": 5,
"priority": "high"
}
}' http://localhost:3000/job
{"message":"job 3 created"}
The example below shows how you may use Cluster to spread the job processing load across CPUs. Please see Cluster module's documentation for more detailed examples on using it.
When cluster .isMaster
the file is being executed in context of the master process, in which case you may perform tasks that you only want once, such as starting the web app bundled with Kue. The logic in the else
block is executed per worker.
var kue = require('kue')
, cluster = require('cluster')
, jobs = kue.createQueue();
var clusterWorkerSize = require('os').cpus().length;
if (cluster.isMaster) {
kue.app.listen(3000);
for (var i = 0; i < clusterWorkerSize; i++) {
cluster.fork();
}
} else {
jobs.process('email', 10, function(job, done){
var pending = 5
, total = pending;
var interval = setInterval(function(){
job.log('sending!');
job.progress(total - pending, total);
--pending || done();
pending || clearInterval(interval);
}, 1000);
});
}
This will create an email
job processor (worker) per each of your machine CPU cores, with each you can handle 10 concurrent email jobs, leading to total 10 * N
concurrent email jobs processed in your N
core machine.
Now when you visit Kue's UI in the browser you'll see that jobs are being processed roughly N
times faster! (if you have N
cores).
Through the use of app mounting you may customize the web application, enabling TLS, or adding additional middleware like Connect's basicAuth()
.
var app = express.createServer({ ... tls options ... });
app.use(express.basicAuth('foo', 'bar'));
app.use(kue.app);
app.listen(3000);
- Introduction to Kue
- API walkthrough to Kue
(The MIT License)
Copyright (c) 2011 LearnBoost <tj@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.