master = ->
workers = []
cluster.on 'fork', (worker) ->
debug 'Adding worker', worker.id
workers.push worker
null
cluster.on 'exit', (worker) ->
index = workers.indexOf worker
debug 'Removing worker', worker.id, index
if index >= 0
workers.splice index, 1
null
server_options = pauseOnConnect: true
if options.https?
for own k,v of options.https
server_options[k] ?= v
server = net.createServer server_options, (connection) ->
worker = workers[ connection_hash connection, workers.length ]
worker.send 'sticky-session:connection', connection
zapp = {server}
listen zapp
start = (id) ->
debug "Starting worker #{id}"
http_module = switch
when options.http_module?
options.http_module
when options.https?
require 'https'
else
require 'http'
server = options.server = http_module.createServer()
{app} = zappa.app root_function, options
server.on 'request', app
process.on 'message', (message, connection) ->
return unless message is 'sticky-session:connection'
server.emit 'connection', connection
connection.resume()
throng {master,start}
zapp = null