Skip to content
This repository has been archived by the owner on May 10, 2019. It is now read-only.

Commit

Permalink
add a ping wsapi that's registered on all daemons and does a light we…
Browse files Browse the repository at this point in the history
…ight test of database health - closes #1324
  • Loading branch information
lloyd committed Mar 23, 2012
1 parent 1943fac commit 1971ecc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/db/mysql_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ exports.createClient = function(options) {
});
},
ping: function(cb) {
this.realClient.ping(cb);
if (stalled) {
process.nextTick(function() {
cb("database is intentionally stalled");
});
} else {
this.realClient.ping(cb);
}
},
_runNextQuery: function() {
var self = this;
Expand Down
6 changes: 4 additions & 2 deletions lib/wsapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ exports.setup = function(options, app) {
try {
var api = require(path.join(__dirname, 'wsapi', f));

// don't register read apis if we are configured as a writer
if (options.only_write_apis && !api.writes_db) return;
// don't register read apis if we are configured as a writer,
// with the exception of ping which tests database connection health.
if (options.only_write_apis && !api.writes_db &&
operation != 'ping') return;

wsapis[operation] = api;

Expand Down
16 changes: 16 additions & 0 deletions lib/wsapi/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const db = require('../db.js');

exports.method = 'get';
exports.writes_db = false;
exports.i18n = false;

exports.process = function(req, res) {
db.ping(function(err) {
if (err) res.send("fail", 500);
else res.send("ok",200);
});
};
24 changes: 24 additions & 0 deletions tests/stalled-mysql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ suite.addBatch({

// now try all apis that can be excercised without further setup
suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', {}),
"fails with 500 when db is stalled": function(err, r) {
// address info with a primary address doesn't need db access.
assert.strictEqual(r.code, 500);
}
},
"address_info": {
topic: wsapi.get('/wsapi/address_info', {
email: 'test@example.domain'
Expand Down Expand Up @@ -154,6 +161,16 @@ addStallDriverBatch(false);

var token = undefined;

suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', {}),
"works when database is unstalled": function(err, r) {
// address info with a primary address doesn't need db access.
assert.strictEqual(r.code, 200);
}
}
});

suite.addBatch({
"account staging": {
topic: wsapi.post('/wsapi/stage_user', {
Expand Down Expand Up @@ -194,6 +211,13 @@ addStallDriverBatch(true);
// test remaining wsapis

suite.addBatch({
"ping": {
topic: wsapi.get('/wsapi/ping', { }),
"fails": function(err, r) {
assert.strictEqual(r.code, 500);
}
},

"account_cancel": {
topic: wsapi.post('/wsapi/account_cancel', { }),
"fails with 503": function(err, r) {
Expand Down

0 comments on commit 1971ecc

Please sign in to comment.