CoffeeScript JavaScript
Switch branches/tags
upstream/0.2.2 hudson-nodejs-chain-gang-57 hudson-nodejs-chain-gang-56 hudson-nodejs-chain-gang-55 hudson-nodejs-chain-gang-55-SUCCESS hudson-nodejs-chain-gang-54 hudson-nodejs-chain-gang-54-SUCCESS hudson-nodejs-chain-gang-53 hudson-nodejs-chain-gang-53-SUCCESS hudson-nodejs-chain-gang-52 hudson-nodejs-chain-gang-52-SUCCESS hudson-nodejs-chain-gang-51 hudson-nodejs-chain-gang-51-SUCCESS hudson-nodejs-chain-gang-50 hudson-nodejs-chain-gang-49 hudson-nodejs-chain-gang-48 hudson-nodejs-chain-gang-47 hudson-nodejs-chain-gang-47-SUCCESS hudson-nodejs-chain-gang-46 hudson-nodejs-chain-gang-45 hudson-nodejs-chain-gang-45-SUCCESS hudson-nodejs-chain-gang-16 hudson-nodejs-chain-gang-15 hudson-nodejs-chain-gang-15-SUCCESS hudson-nodejs-chain-gang-14 hudson-nodejs-chain-gang-14-SUCCESS hudson-nodejs-chain-gang-13 hudson-nodejs-chain-gang-13-SUCCESS hudson-nodejs-chain-gang-12 hudson-nodejs-chain-gang-11 hudson-nodejs-chain-gang-11-SUCCESS hudson-nodejs-chain-gang-10 hudson-nodejs-chain-gang-10-SUCCESS hudson-nodejs-chain-gang-9 hudson-nodejs-chain-gang-9-SUCCESS hudson-nodejs-chain-gang-8 hudson-nodejs-chain-gang-8-SUCCESS hudson-nodejs-chain-gang-7 hudson-nodejs-chain-gang-7-SUCCESS hudson-nodejs-chain-gang-6 hudson-nodejs-chain-gang-6-SUCCESS hudson-nodejs-chain-gang-5 debian/0.2.2-0sg57 debian/0.2.2-0sg56 debian/0.2.2-0sg55 debian/0.2.2-0sg54 debian/0.2.2-0sg53 debian/0.2.2-0sg52 debian/0.2.2-0sg51 debian/0.2.2-0sg50 debian/0.2.2-0sg49 debian/0.2.2-0sg48 debian/0.2.2-0sg47 debian/0.2.2-0sg46 debian/0.2.2-0sg45 debian/0.2.2-0sg16 debian/0.2.2-0sg15 debian/0.2.2-0sg14 debian/0.2.2-0sg13 debian/0.2.2-0sg11 debian/0.2.2-0sg10 debian/0.2.2-0sg9 debian/0.2.2-0sg8 debian/0.2.2-0sg7 debian/0.2.2-0sg6
Nothing to show
Pull request Compare This branch is 27 commits ahead, 40 commits behind technoweenie:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Chain Gang

Chain Gang is a small in-process Node.js queue. It ensures a limit to the number of simultaneous tasks in action at any time.

var chainGang = require('chaingang')
var chain     = chainGang.create({workers: 3})

var workCallback = function(worker) {
  // do some work
  worker.finish() // and call this when finished

chain.add(workCallback, 'foo')
chain.add(workCallback, 'bar')
chain.add(workCallback, 'baz')
chain.add(workCallback, 'qux') // waits until one finishes

chain.addListener('error', function(name, error) {

chain.addListener('finished', function(name, value) {
  sys.puts(name + " has finished, leaving us with " + sys.inspect(value))

Use Case

Let's say you have an expensive child process to run when requests come in. If traffic is really busy, you don't want to spin up 50 of these at once. Also, you don't want identical child processes running. If multiple requests want the same thing, assume they'll all get notified when it's finished.


  • Persistence
  • Single Process Durability

If you need anything more, use a real queue (like Resque) backed by a persistent data store.


Run this in the main directory to compile coffeescript to javascript as you go:

coffee -wc -o lib --no-wrap src/**/*.coffee

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with version or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


Copyright (c) 2010 rick. See LICENSE for details.