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.
This commit is contained in:
Atul Varma 2013-06-03 18:56:00 -04:00
Родитель d25d41eb8c
Коммит ec4b9ce536
3 изменённых файлов: 36 добавлений и 9 удалений

17
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

Просмотреть файл

@ -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;

Просмотреть файл

@ -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"
},