node-resque
                                
                                
                                
                                    node-resque copied to clipboard
                            
                            
                            
                        Resque for Node.js (pure JS)
h1. This respository is not under active development
p. Check out Rick Olson's "coffee-resque":https://github.com/technoweenie/coffee-resque which is.
h2. Resque for Node.js
p. "Resque":http://github.com/defunkt/resque is a "redis":http://code.google.com/p/redis/ backed task queue inspired by "delayed job":http://github.com/tobi/delayed_job. "Node.js":http://nodejs.org is a javascript runtime built on the "v8 engine":http://code.google.com/p/v8/.
h2. 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.
h2. Install
@npm install resque@ If you don't already have "node_redis":http://github.com/mranney/node_redis (@redis@ on npm), npm will install it for you.
h2. Use
h3. 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.
h3. 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.
h3. 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.
h3. 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" })
...
h3. 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 })
...
h3. 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 })
...
h3. 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@.