From ec4b9ce536f4f28325c44cf3f1eb28e458ceb368 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Mon, 3 Jun 2013 18:56:00 -0400 Subject: [PATCH] No more killing the server at startup if healthcheck fails. However, we do print a big red message telling anyone watching that things are seriously messed up. --- app.js | 17 +++++++++++------ controllers/health-check.js | 26 ++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 1e74962..20f51f2 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,7 @@ if ( process.env.NEW_RELIC_HOME ) { require( 'newrelic' ); } +const colors = require('colors'); const path = require('path'); const http = require('http'); const express = require('express'); @@ -62,16 +63,20 @@ if (!module.parent) app.listen(port, function(err) { if (err) throw err; console.log("Listening on port " + port + "."); - console.log("Performing health check."); + console.log("Performing health check.\n"); healthChecker.runChecks(function(results) { + var consoleStr = healthCheck.resultsToConsoleString(results); + console.log("Health check results:\n"); if (results.status != "OK") { - console.error("Health check failed:", - JSON.stringify(results, null, 2)); - console.error("Please resolve the problem and restart the server."); - process.exit(1); + console.error(consoleStr + "\n"); + console.error(("One or more critical services are down or " + + "misconfigured. Please fix them!").red.bold); + } else { + console.log(consoleStr); + console.log(("\nHealth check indicates all systems are " + + "functional.").green); } - console.log("Health check indicates all systems are functional."); }); }); else diff --git a/controllers/health-check.js b/controllers/health-check.js index ef084b4..f82d03f 100644 --- a/controllers/health-check.js +++ b/controllers/health-check.js @@ -1,15 +1,16 @@ // TODO: Separate this module out into its own npm module/git repo. var async = require('async'); +var colors = require('colors'); -const HEALTH_CHECK_TIMEOUT = 15000; +const CHECKMARK = "\u2713"; function checker(fn) { return function check(cb) { var timeout = setTimeout(function() { timeout = null; cb(null, {status: "FAILED", reason: "TIMEOUT"}); - }, HEALTH_CHECK_TIMEOUT); + }, module.exports.TIMEOUT); try { fn(function(err) { @@ -71,6 +72,25 @@ function runChecks(checks, cb) { }); } +function resultsToConsoleString(results) { + var lines = []; + + Object.keys(results).forEach(function(name) { + var info = results[name]; + + if (info && typeof(info) == "object" && info.status) { + if (info.status == "OK") { + lines.push(CHECKMARK.green + " " + name.grey); + } else { + lines.push("x".red + " " + name.grey + " " + + (info.reason ? info.reason : "")); + } + } + }); + + return lines.join('\n'); +} + module.exports = function healthCheck(options) { var authenticate = options.auth || function(req, res, next) { next(); }; var checks = options.checks; @@ -92,6 +112,8 @@ module.exports = function healthCheck(options) { return healthChecker; }; +module.exports.TIMEOUT = 15000; +module.exports.resultsToConsoleString = resultsToConsoleString; module.exports.runChecks = runChecks; module.exports.sessionStorageChecker = sessionStorageChecker; module.exports.checker = checker; diff --git a/package.json b/package.json index b2eeeaf..6203912 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,12 @@ "underscore": "~1.4.4", "winston": "~0.7.1", "newrelic": "~0.9.19", + "colors": "~0.6.0", "jwt-simple": "~0.1.0" }, "devDependencies": { "sinon": "~1.7.2", "injectr": "~0.4.0", - "colors": "~0.6.0", "optimist": "~0.5.2", "up": "~0.2.2" },