Use Node.js domains to gracefully handle exceptions.

This commit is contained in:
Zaid Abdulla committed 2014-08-02 21:40:49 +03:00
1 parent d5dc7b36df
commit 3d201981e2
1 file changed
+37 -1
+37 -1
View File
@@ -15,6 +15,9 @@
# along with GenieACS. If not, see <http://www.gnu.org/licenses/>. # along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
### ###
domain = require 'domain'
util = require 'util'
cluster = require 'cluster'
config = require './config' config = require './config'
db = require './db' db = require './db'
@@ -26,7 +29,40 @@ networkInterface = config["#{service.toUpperCase()}_INTERFACE"]
port = config["#{service.toUpperCase()}_PORT"] port = config["#{service.toUpperCase()}_PORT"]
useHttps = config["#{service.toUpperCase()}_SSL"] useHttps = config["#{service.toUpperCase()}_SSL"]
listener = require("./#{service}").listener serviceListener = require("./#{service}").listener
server = null
listener = (httpRequest, httpResponse) ->
d = domain.create()
d.on('error', (err) ->
util.error("#{new Date().toISOString()} - #{err.stack}\n")
try
killTimer = setTimeout(() ->
process.exit(1)
, 30000)
killTimer.unref()
cluster.worker?.disconnect()
server.close(() ->
db.disconnect()
)
httpResponse.writeHead(500, {'Connection' : 'close'})
httpResponse.end()
catch err2
util.error("#{new Date().toISOString()} - #{err2.stack}\n")
)
d.add(httpRequest)
d.add(httpResponse)
d.run(() ->
serviceListener(httpRequest, httpResponse)
)
if useHttps if useHttps
path = require 'path' path = require 'path'