Kue is a priority job queue backed by redis, built for node.js.
$ npm install kue
- Delayed jobs
- Job event and progress pubsub
- Rich integrated UI
- Infinite scrolling
- UI progress indication
- Job specific logging
- Powered by Redis
- Optional retries
- Full-text search capabilities
- RESTful JSON API
- Graceful workers shutdown
- Creating Jobs
- Jobs Priority
- Failure Attempts
- Job Logs
- Job Progress
- Job Events
- Queue Events
- Delayed Jobs
- Processing Jobs
- Processing Concurrency
- 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.
jobs.create('email', {
title: 'welcome email for tj'
, to: '[email protected]'
, template: 'welcome-email'
}).save();
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: '[email protected]'
, 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: '[email protected]'
, template: 'welcome-email'
}).priority('high').attempts(5).save();
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:
- `failed` the job has failed and has no remaining attempts
- 'failed attempt' the job has failed, but has remaining attempts yet
- `complete` the job has completed
- `promotion` the job (when delayed) is now queued
- `progress` the job's progress ranging from 0-100
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(){
console.log("Job complete");
}).on('failed', function(){
console.log("Job failed");
}).on('progress', function(progress){
process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
});
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 complete', function(id){
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: '[email protected]'
, 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.
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. Support for named queues are also requested
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.
var kue = require('kue')
, jobs = kue.createQueue();
jobs.process('email', function(job, done){
email(job.data.to, done);
});
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){
// ...
});
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
). Queue#createQueue(options)
accepts redis connection options in options.redis
key.
var kue = require('kue');
q = kue.createQueue({
redis: {
port: 1234,
host: '10.0.50.20',
auth: 'password',
options: {
// look for more redis options in [node_redis](https://github.com/mranney/node_redis)
}
}
});
For backward compatibility to Kue < 0.7.0
, monkey-patch-styled Redis client connection settings can be set by overriding the kue.redis.createClient
function.
For example, to create a Redis client that connects to 192.168.1.2
on port 1234
that requires authentication, use the following:
var kue = require('kue')
, redis = require('redis');
kue.redis.createClient = function() {
var client = redis.createClient(1234, '192.168.1.2');
client.auth('password');
return client;
};
Redis connection settings must be set before calling kue.createQueue()
or accessing kue.app
.
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.app.listen(3000);
The title defaults to "Kue", to alter this invoke:
kue.app.set('title', 'My Application');
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"]
You may disable search indexes in Kue >= 0.7.0 for memory optimization or to avoid memory leaks that some have reported:
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":"[email protected]","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":"[email protected]","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":"[email protected]","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": "[email protected]",
"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 <[email protected]>
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.