Skip to content

HTTPS clone URL

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()}-#{process.pid}"
# Fork workers.
for i in [0...cpuCount]
cluster.fork()
cluster.on "exit", (worker, code, signal) ->
console.error "Worker #{worker.process.pid} died (Code: #{code})."
unless signal?
console.log "Restarting in #{delay} seconds (restarted #{restarts} already)..."
restarts++
setTimeout (-> cluster.fork()), delay * 1000
else
startupFunction (info) ->
log = "Worker #{process.pid} 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.