Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Node.js v0.8 Cluster Server
cluster = require "cluster"
log = require "winston"
config = require "config" # Your configuration goes here.
app = require "./server" # Your real server goes here.
numCpus = require("os").cpus().length
workers = {}
# Helpers.
shutdown = ->
log.warning "Hard shutdown."
for own pid, worker of workers
log.warning "Killing worker: #{pid}"
process.exit 0
# Start up the cluster.
if cluster.isMaster
# Fork workers.
cluster.fork() for cpu in [0...numCpus]
cluster.on "online", (worker) ->
workers[] = worker "[#{}] Worker #{} came up."
cluster.on "exit", (worker, code, signal) ->
log.warning "[#{}] Worker #{} died. Restarting."
delete workers[]
# Manually shutdown workers on signals.
process.on signal, shutdown for signal in ["SIGINT", "SIGQUIT", "SIGTERM"] "Master #{} started."
# Worker: Start server and message "up".
app.listen, "Worker #{} started."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.