From 0f30b7d7ef64368d8172a07e6d0f87978a8f75b9 Mon Sep 17 00:00:00 2001 From: Luis Bosque Date: Wed, 25 Mar 2015 18:19:40 +0100 Subject: [PATCH 1/3] Return failed health checks with disabled file --- config/environments/development.js.example | 1 + config/environments/production.js.example | 1 + config/environments/staging.js.example | 1 + config/environments/test.js.example | 1 + lib/cartodb/monitoring/health_check.js | 20 +++++++++- test/acceptance/health_check.js | 45 ++++++++++++++++++++++ 6 files changed, 68 insertions(+), 1 deletion(-) diff --git a/config/environments/development.js.example b/config/environments/development.js.example index d6bfba7f..4af948ae 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -174,6 +174,7 @@ var config = { x: 0, y: 0 } + ,disabled_file: 'pids/disabled' // Use this as a feature flags enabling/disabling mechanism ,enabledFeatures: { diff --git a/config/environments/production.js.example b/config/environments/production.js.example index e58aea66..505d454f 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -183,6 +183,7 @@ var config = { x: 0, y: 0 } + ,disabled_file: 'pids/disabled' // Use this as a feature flags enabling/disabling mechanism ,enabledFeatures: { diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 8f4b0941..1f4a0409 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -183,6 +183,7 @@ var config = { x: 0, y: 0 } + ,disabled_file: 'pids/disabled' // Use this as a feature flags enabling/disabling mechanism ,enabledFeatures: { diff --git a/config/environments/test.js.example b/config/environments/test.js.example index 64c40ffd..f3af958b 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -172,6 +172,7 @@ var config = { x: 0, y: 0 } + ,disabled_file: 'pids/disabled' // Use this as a feature flags enabling/disabling mechanism ,enabledFeatures: { diff --git a/lib/cartodb/monitoring/health_check.js b/lib/cartodb/monitoring/health_check.js index 930f20a4..235f9dae 100644 --- a/lib/cartodb/monitoring/health_check.js +++ b/lib/cartodb/monitoring/health_check.js @@ -42,7 +42,25 @@ HealthCheck.prototype.check = function(config, callback) { var mapnikXmlParams = config; step( - function getDBParams() { + function getManualDisable() { + disabled_file = global.environment.disabled_file; + fs.readFile(disabled_file, this); + }, + function handleDisabledFile(err, data) { + var next = this; + if (err) { + return next(); + } + if (!!data) { + err = new Error(data); + err.http_status = 503; + throw err; + } + }, + function getDBParams(err) { + if (err) { + throw err; + } startTime = Date.now(); self.metadataBackend.getAllUserDBParams(config.username, this); }, diff --git a/test/acceptance/health_check.js b/test/acceptance/health_check.js index b6409574..657a2d6d 100644 --- a/test/acceptance/health_check.js +++ b/test/acceptance/health_check.js @@ -5,6 +5,11 @@ var CartodbWindshaft = require(__dirname + '/../../lib/cartodb/cartodb_windshaft var serverOptions = require(__dirname + '/../../lib/cartodb/server_options')(); var server = new CartodbWindshaft(serverOptions); +var metadataBackend = {}; +var tilelive = {}; +var HealthCheck = require('../../lib/cartodb/monitoring/health_check'); +var healthCheck = new HealthCheck(metadataBackend, tilelive); + suite('health checks', function () { function resetHealthConfig() { @@ -71,4 +76,44 @@ suite('health checks', function () { ); }); + test('error if disabled file exists', function(done) { + var fs = require('fs'); + + readFileFn = fs.readFile + fs.readFile = function(filename, callback) { + callback(null, "Maintenance"); + } + + healthCheck.check(null, function(err, result) { + assert.equal(err.message, "Maintenance"); + assert.equal(err.http_status, 503); + done(); + }); + + fs.readFile = readFileFn; + }); + + test('not err if disabled file does not exists', function(done) { + resetHealthConfig(); + + global.environment.disabled_file = '/tmp/ftreftrgtrccre'; + + assert.response(server, + healthCheckRequest, + { + status: 200 + }, + function (res, err) { + assert.ok(!err); + + var parsed = JSON.parse(res.body); + + assert.equal(parsed.enabled, true); + assert.equal(parsed.ok, true); + + done(); + } + ); + }); + }); From e3b7027b243bac417dd8f8cd442016739d085b2b Mon Sep 17 00:00:00 2001 From: Luis Bosque Date: Wed, 25 Mar 2015 18:23:03 +0100 Subject: [PATCH 2/3] Remove unnecesary variable in health check --- lib/cartodb/monitoring/health_check.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/cartodb/monitoring/health_check.js b/lib/cartodb/monitoring/health_check.js index 235f9dae..f59fbfb1 100644 --- a/lib/cartodb/monitoring/health_check.js +++ b/lib/cartodb/monitoring/health_check.js @@ -43,8 +43,7 @@ HealthCheck.prototype.check = function(config, callback) { step( function getManualDisable() { - disabled_file = global.environment.disabled_file; - fs.readFile(disabled_file, this); + fs.readFile(global.environment.disabled_file, this); }, function handleDisabledFile(err, data) { var next = this; From 09e0f869367336839b121389fba9446ed9554de8 Mon Sep 17 00:00:00 2001 From: Luis Bosque Date: Wed, 25 Mar 2015 18:41:27 +0100 Subject: [PATCH 3/3] jshint fixes --- test/acceptance/health_check.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/acceptance/health_check.js b/test/acceptance/health_check.js index 657a2d6d..890b559e 100644 --- a/test/acceptance/health_check.js +++ b/test/acceptance/health_check.js @@ -79,7 +79,7 @@ suite('health checks', function () { test('error if disabled file exists', function(done) { var fs = require('fs'); - readFileFn = fs.readFile + var readFileFn = fs.readFile fs.readFile = function(filename, callback) { callback(null, "Maintenance"); } @@ -88,9 +88,9 @@ suite('health checks', function () { assert.equal(err.message, "Maintenance"); assert.equal(err.http_status, 503); done(); + fs.readFile = readFileFn; }); - fs.readFile = readFileFn; }); test('not err if disabled file does not exists', function(done) {