Skip to content

Commit 7c59a1b

Browse files
Add health check
1 parent 162eb29 commit 7c59a1b

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The following parameters can be set in config files or in env variables:
4747
- EMAIL_VERIFY_DISAGREE_URL: email verify disagree URL
4848
- SCOPES: the configurable M2M token scopes, refer `config/default.js` for more details
4949
- SEARCH_MEMBERS_ADMIN_ONLY_FIELDS: only admin and M2M can view these fields for search members API
50+
- HEALTH_CHECK_TIMEOUT: health check timeout in milliseconds
5051

5152
Set the following environment variables used by bus API to get TC M2M token (use 'set' insted of 'export' for Windows OS):
5253
```

config/default.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module.exports = {
4343
MEMBER_TRAIT_ES_TYPE: process.env.MEMBER_TRAIT_ES_TYPE || '_doc'
4444
},
4545

46+
// health check timeout in milliseconds
47+
HEALTH_CHECK_TIMEOUT: process.env.HEALTH_CHECK_TIMEOUT || 3000,
48+
4649
// file upload max size in bytes
4750
FILE_UPLOAD_SIZE_LIMIT: process.env.FILE_UPLOAD_SIZE_LIMIT
4851
? Number(process.env.FILE_UPLOAD_SIZE_LIMIT) : 10 * 1024 * 1024, // 10M

src/common/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ module.exports = {
3434
BadRequestError: createError('BadRequestError', 400),
3535
UnauthorizedError: createError('UnauthorizedError', 401),
3636
ForbiddenError: createError('ForbiddenError', 403),
37-
NotFoundError: createError('NotFoundError', 404)
37+
NotFoundError: createError('NotFoundError', 404),
38+
ServiceUnavailableError: createError('ServiceUnavailableError', 503)
3839
}

src/controllers/HealthController.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Controller for health check endpoint
3+
*/
4+
const config = require('config')
5+
const service = require('../services/SearchService')
6+
const errors = require('../common/errors')
7+
8+
// the topcoder-healthcheck-dropin library returns checksRun count,
9+
// here it follows that to return such count
10+
let checksRun = 0
11+
12+
/**
13+
* Check health of the app
14+
* @param {Object} req the request
15+
* @param {Object} res the response
16+
*/
17+
async function checkHealth (req, res) {
18+
// perform a quick database access operation, if there is no error and is quick, then consider it healthy
19+
checksRun += 1
20+
const timestampMS = new Date().getTime()
21+
try {
22+
await service.searchMembers(null, { page: 1, perPage: 1 })
23+
} catch (e) {
24+
throw new errors.ServiceUnavailableError(`There is database operation error, ${e.message}`)
25+
}
26+
if (new Date().getTime() - timestampMS > Number(config.HEALTH_CHECK_TIMEOUT)) {
27+
throw new errors.ServiceUnavailableError('Database operation is slow.')
28+
}
29+
// there is no error, and it is quick, then return checks run count
30+
res.send({ checksRun })
31+
}
32+
33+
module.exports = {
34+
checkHealth
35+
}

src/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,11 @@ module.exports = {
108108
method: 'searchMembers',
109109
scopes: [MEMBERS.READ, MEMBERS.ALL]
110110
}
111+
},
112+
'/members/health': {
113+
get: {
114+
controller: 'HealthController',
115+
method: 'checkHealth'
116+
}
111117
}
112118
}

0 commit comments

Comments
 (0)