-
Notifications
You must be signed in to change notification settings - Fork 18
/
healthCheck.js
133 lines (115 loc) · 3.28 KB
/
healthCheck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
var Step = require('step');
var Pg = require('pg');
var Redis = require('redis');
/**
* Attempts to connect to a PostgreSQL database, providing the
* callback with the error object.
*
* The error object is the second callback argument because
* error objects in the first argument cause Step to short
* circuit. In the case of a health check, we want all
* errors.
*
* @param {string} host
* @param {string} port
* @param {string} user
* @param {string} password
* @param {string} database
* @param {function} callback
*/
function checkPostgres(host, port, user, password, database, callback) {
var conString = ['postgres://', user, ':', password, '@', host, '/', database].join('');
var client = new Pg.Client(conString);
client.connect(function(err) {
client.end();
callback(null, err);
});
}
/**
* Attempts to connect to a Redis data store, providing the
* callback with the error object.
*
* The error object is the second callback argument because
* error objects in the first argument cause Step to short
* circuit. In the case of a health check, we want all
* errors.
*
* @param {string} host
* @param {string} port
* @param {function} callback
*/
function checkRedis(host, port, callback) {
var client = Redis.createClient(port, host);
client.on('connect', function(err) {
client.quit();
callback(null, err);
});
client.on('error', function(err) {
client.quit();
callback(null, err);
});
}
function buildPayload(err) {
var payload = {};
if (err) {
payload = {'ok': !err, 'msg': err.toString()};
} else {
payload = {'ok': !err};
}
return payload;
}
/**
* Makes use of the PostgreSQL and Redis checks above to return
* a JSON object representing the health of both data stores:
*
* {
* "databases": [
* {
* "default": {
* "ok": true
* }
* }
* ],
* "caches": [
* {
* "default": {
* "ok": true
* }
* }
* ]
* }
*
* @param {object} config - The configuration object passed to Windshaft.Server()
*/
module.exports = function createHealthCheckHandler(config) {
var pgConf = config.grainstore.datasource,
pgHost = pgConf.host,
pgPort = pgConf.port,
pgUser = pgConf.user,
pgPassword = pgConf.password,
pgDatabase = pgConf.dbname,
redisHost = config.redis.host,
redisPort = config.redis.port;
return function(req, res) {
var status = {};
/* jshint ignore:start */
Step(
function executeChecks() {
checkPostgres(pgHost, pgPort, pgUser, pgPassword, pgDatabase, this.parallel());
checkRedis(redisHost, redisPort, this.parallel());
},
function sendResponse(err, databaseError, cacheError) {
var response = {},
statusCode = 503;
response.databases = [{'default': buildPayload(databaseError)}];
response.caches = [{'default': buildPayload(cacheError)}];
if (!databaseError && !cacheError) {
statusCode = 200;
}
res.send(response, statusCode);
}
);
/* jshint ignore:end */
};
};