This respository is not under active development
Check out Rick Olsonโs coffee-resque which is.
Resque for Node.js
Resque is a redis backed task queue inspired by delayed job. Node.js is a javascript runtime built on the v8 engine.
Why?
Resque.js allows you to chunk together blocks of concurrent code and work through them serially across multiple worker processes & machines. The advantage of building this as drop-in compatible resque workers is that the monitoring interface (resque-web) is already written and it provides a nice queue interface between Rails and node. Youโre probably already running resque for Rails; use it for Node, too.
Install
npm install resque
If you donโt already have node_redis (redis
on npm), npm will install it for you.
Use
sandwich_worker.js
var sandwichWorker = exports
sandwichWorker.makeMeASandwich = function (sandwich) {
var job = this
makeSandwich (sandwich, function (err, result) {
if (err) job.fail ({ error: err })
else job.succeed ()
})
}
sandwichWorker.SomeRubyTask = function () {
require ('sys').puts ("This task could have been pulled off a shared queue")
this.succeed ()
}
success()
and failure(error)
callbacks are provided and can be passed in through async code to be called on completion, which frees up the worker to take another job off the queue.
Gentlemen, start your workers
To start the worker, WORKER=sandwich_worker.js QUEUE=sandwich_factory bin/node-resque-worker
.
You can optionally provide a url to redis, like WORKER=sandwich_worker.js QUEUE=* REDIS=redis.example.com:9876 bin/node-resque-worker
To stop the worker gracefully (finishing the last task), send a QUIT signal.
To stop the worker less gracefully, send a KILL signal.
Enqueueing tasks
#!/usr/bin/env node
var resque = require ("resque").connect ()
resque.enqueue ('sandwich_factory', 'makeMeASandwich',
{ bread: 'Acme Levain'
, cheese: 'Cowgirl Creamery St Pat'
, greens: ['arugula']
, mustard: true
})
This code adds a makeMeASandwich
request to the sandwich_factory
resque queue.
Namespaces
By default, resque uses the redis namespace of resque:
. If you require resque as follows, you can use a different namespace:
var resque = require ("resque").connect ({ namespace: "node-only" })
...
Providing a Redis connection
If you already are connected to redis in your app, you can use that client instead of creating a new one. To do so:
var redisClient = require ("redis").createClient ()
, resque = require ("resque").connect ({ redis: redisClient })
...
Specifying Redis port & host
If Redis is not running locally or is not bound to the default Redis port & host (localhost:6379), you can provide port and host as follows:
var resque = require ("resque").connect ({ host: 'redis.production.example.com', port: 1337 })
...
Cleaning stale workers
At any point, you can call require ("resque").connect({...}).cleanStaleWorkers ()
, which will ensure that any dead workers have been removed from resque. This is called at the start of node-resque-worker
.