Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

40 lines (27 sloc) 1.136 kb
# Module dependencies.
cluster = require("cluster")
numCPUs = require('os').cpus().length
# Counts the number of restarts
restarts = 0
workers = 0
delay = 5
exports.createWorkers = (startupFunction, cpuCount = numCPUs) ->
console.log "Starting only one worker because not in production mode." if process.env.NODE_ENV != "production"
if cluster.isMaster and process.env.NODE_ENV == "production"
console.log "Starting node cluster with #{cpuCount} workers."
workersId = "#{new Date().getTime()}-#{}"
# Fork workers.
for i in [0...cpuCount]
cluster.on "exit", (worker, code, signal) ->
console.error "Worker #{} died (Code: #{code})."
unless signal?
console.log "Restarting in #{delay} seconds (restarted #{restarts} already)..."
setTimeout (-> cluster.fork()), delay * 1000
startupFunction (info) ->
log = "Worker #{} created."
if info?.port and info.mode
log += " Express server listening on port #{info.port} in #{info.mode} mode"
console.log log
Jump to Line
Something went wrong with that request. Please try again.