File tree 5 files changed +47
-1
lines changed 5 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ The following parameters can be set in config files or in env variables:
47
47
- EMAIL_VERIFY_DISAGREE_URL: email verify disagree URL
48
48
- SCOPES: the configurable M2M token scopes, refer ` config/default.js ` for more details
49
49
- 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
50
51
51
52
Set the following environment variables used by bus API to get TC M2M token (use 'set' insted of 'export' for Windows OS):
52
53
```
Original file line number Diff line number Diff line change @@ -43,6 +43,9 @@ module.exports = {
43
43
MEMBER_TRAIT_ES_TYPE : process . env . MEMBER_TRAIT_ES_TYPE || '_doc'
44
44
} ,
45
45
46
+ // health check timeout in milliseconds
47
+ HEALTH_CHECK_TIMEOUT : process . env . HEALTH_CHECK_TIMEOUT || 3000 ,
48
+
46
49
// file upload max size in bytes
47
50
FILE_UPLOAD_SIZE_LIMIT : process . env . FILE_UPLOAD_SIZE_LIMIT
48
51
? Number ( process . env . FILE_UPLOAD_SIZE_LIMIT ) : 10 * 1024 * 1024 , // 10M
Original file line number Diff line number Diff line change @@ -34,5 +34,6 @@ module.exports = {
34
34
BadRequestError : createError ( 'BadRequestError' , 400 ) ,
35
35
UnauthorizedError : createError ( 'UnauthorizedError' , 401 ) ,
36
36
ForbiddenError : createError ( 'ForbiddenError' , 403 ) ,
37
- NotFoundError : createError ( 'NotFoundError' , 404 )
37
+ NotFoundError : createError ( 'NotFoundError' , 404 ) ,
38
+ ServiceUnavailableError : createError ( 'ServiceUnavailableError' , 503 )
38
39
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -108,5 +108,11 @@ module.exports = {
108
108
method : 'searchMembers' ,
109
109
scopes : [ MEMBERS . READ , MEMBERS . ALL ]
110
110
}
111
+ } ,
112
+ '/members/health' : {
113
+ get : {
114
+ controller : 'HealthController' ,
115
+ method : 'checkHealth'
116
+ }
111
117
}
112
118
}
You can’t perform that action at this time.
0 commit comments