Created
June 16, 2014 06:46
-
-
Save lauripiispanen/086313b49b4cd6fcd269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cluster = require 'cluster' | |
settings = require './settings' | |
log4js = require 'log4js' | |
port = process.env.PORT || 3000 | |
forks = settings.forks || 1 | |
newrelic = | |
noticeError: (error) -> | |
if (settings.newrelic_license) | |
newrelic = require 'newrelic' | |
log4js.replaceConsole() | |
if cluster.isMaster | |
startWorker = -> | |
console.log 'starting worker' | |
cluster.fork() | |
[1..forks].forEach startWorker | |
cluster.on 'disconnect', (worker) -> | |
console.error 'Worker disconnected!' | |
startWorker() | |
else | |
http = require('http').createServer (req, res) -> | |
d = require('domain').create() | |
d.add req | |
d.add res | |
d.on 'error', (err) -> | |
console.error 'Application encountered an uncaught exception and will be shut down.\n', err.stack | |
newrelic.noticeError err | |
try | |
# make sure the process exits in 10 secs | |
setTimeout( -> | |
process.exit(1); | |
, 10000).unref() | |
# try to end current request (somewhat more) gracefully | |
res.statusCode = 500 | |
res.end() | |
http.close() | |
cluster.worker.disconnect() | |
catch e | |
d.run -> | |
require('./index')(req, res) | |
http.listen(port) | |
console.log "Server listening on port #{port}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment