diff --git a/multicore.coffee b/multicore.coffee index 194b5940e..d9a560513 100644 --- a/multicore.coffee +++ b/multicore.coffee @@ -5,7 +5,7 @@ if cluster.isMaster for i in [0...numCPUs] cluster.fork() cluster.on 'exit', (worker, code, signal) -> - console.log 'worker' + worker.process.id + 'died' + console.log 'worker ' + worker.id + ' died' cluster.fork() else require('coffee-script') diff --git a/server/hipchat.coffee b/server/hipchat.coffee index e85b21c1f..dd6addf2b 100644 --- a/server/hipchat.coffee +++ b/server/hipchat.coffee @@ -14,3 +14,15 @@ module.exports.sendHipChatMessage = sendHipChatMessage = (message) -> request.post {uri: url, json: form}, (err, res, body) -> return log.error 'Error sending HipChat patch request:', err or body if err or /error/i.test body #log.info "Got HipChat patch response:", body + +module.exports.sendTowerHipChatMessage = sendTowerHipChatMessage = (message) -> + return unless key = config.hipchatTowerAPIKey + roomID = 318356 + form = + color: 'red' + notify: true + message: message + messageFormat: 'html' + url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}" + request.post {uri: url, json: form}, (err, res, body) -> + return log.error 'Error sending HipChat Tower message:', err or body if err or /error/i.test body diff --git a/server_config.coffee b/server_config.coffee index c4e6ecde9..20cb28341 100644 --- a/server_config.coffee +++ b/server_config.coffee @@ -48,7 +48,7 @@ config.mail = cronHandlerPrivateIP: process.env.COCO_CRON_PRIVATE_IP or '' config.hipchatAPIKey = process.env.COCO_HIPCHAT_API_KEY or '' - +config.hipchatTowerAPIKey = process.env.COCO_HIPCHAT_TOWER_API_KEY or '' config.queue = accessKeyId: process.env.COCO_AWS_ACCESS_KEY_ID or '' secretAccessKey: process.env.COCO_AWS_SECRET_ACCESS_KEY or '' diff --git a/server_setup.coffee b/server_setup.coffee index 69475c022..7120c22db 100644 --- a/server_setup.coffee +++ b/server_setup.coffee @@ -13,6 +13,7 @@ logging = require './server/commons/logging' config = require './server_config' auth = require './server/routes/auth' UserHandler = require './server/users/user_handler' +hipchat = require './server/hipchat' global.tv4 = require 'tv4' # required for TreemaUtils to work global.jsondiffpatch = require 'jsondiffpatch' @@ -38,6 +39,14 @@ developmentLogging = (tokens, req, res) -> elapsedColor = if elapsed < 500 then 90 else 31 "\x1b[90m#{req.method} #{req.originalUrl} \x1b[#{color}m#{res.statusCode} \x1b[#{elapsedColor}m#{elapsed}ms\x1b[0m" +setupErrorMiddleware = (app) -> + app.use (err, req, res, next) -> + if err + res.status(500).send(error: "Something went wrong!") + hipchat.sendTowerHipChatMessage("The server crashed. Stack trace:
#{err.stack}") + console.log "Got a server error: #{err.stack}" + else + next(err) setupExpressMiddleware = (app) -> if config.isProduction express.logger.format('prod', productionLogging) @@ -101,6 +110,7 @@ exports.setupMiddleware = (app) -> setupOneSecondDelayMiddleware app setupTrailingSlashRemovingMiddleware app setupRedirectMiddleware app + setupErrorMiddleware app ###Routing function implementations###