Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 416a16e

Browse files
authoredOct 1, 2019
Merge pull request #193 from topcoder-platform/add-healthcheck-endpoint
create health check endpoint for mongodb
2 parents 0f00215 + 13f2761 commit 416a16e

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed
 

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ npm run lint
4848
- GET /users/settings - gets the current user's setup
4949
- GET /users/accessToken - gets the user's access token
5050

51+
- GET /health - gets the app health
52+
5153
## Configuration
5254

5355
please see [configuration.md](configuration.md).

‎configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following config parameters are supported, they are defined in `src/config.j
3131
|TOPCODER_AUTH_SECRET| The auth secret used to sign the JWT| No default - needs to be set up|
3232
|TOPCODER_VALID_ISSUERS| Stringified array of valid JWT issuers| `'["topcoder-dev.com"]'`|
3333
|TOPCODER_JWT_KEY_CACHE_TIME| They JWT cache time | 90 |
34+
|MONGODB_TIMEOUT | The timeout used to check if the app is healthy. |10000 |
3435

3536
## GitHub OAuth App Setup
3637

‎src/common/errors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ class ForbiddenError extends ApiError {
5353
this.details = details;
5454
}
5555
}
56+
// The forbidden error
57+
class ServiceUnavailable extends ApiError {
58+
constructor(message, details) {
59+
super(503, 'SERVICE_UNAVAILABLE', message);
60+
this.details = details;
61+
}
62+
}
5663

5764
module.exports = {
5865
ApiError,
5966
ValidationError,
6067
NotFoundError,
6168
UnauthorizedError,
6269
ForbiddenError,
70+
ServiceUnavailable,
6371
};

‎src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
API_VERSION: process.env.API_VERSION || 'v1',
1414
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
1515
MONGODB_URI: process.env.MONGODB_URI || 'mongodb://localhost:27017/topcoderx',
16+
MONGODB_TIMEOUT: process.env.MONGODB_TIMEOUT || 10000, // eslint-disable-line no-magic-numbers
1617
SESSION_SECRET: process.env.SESSION_SECRET || 'kjsdfkj34857',
1718
// Github and gitlab client id and secret
1819
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID || 'ae39bea2a2a23f1dd032',
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2019 TopCoder, Inc. All rights reserved.
3+
*/
4+
5+
/**
6+
* This controller exposes application health related endpoints.
7+
*
8+
* @author Thomas Kranitsas
9+
* @version 1.0
10+
*/
11+
const helper = require('../common/helper');
12+
const AppHealthService = require('../services/AppHealthService');
13+
14+
/**
15+
* gets the application health
16+
* @returns {Object} the health details
17+
*/
18+
async function getAppHealth() {
19+
return await AppHealthService.getAppHealth();
20+
}
21+
22+
module.exports = {
23+
getAppHealth,
24+
};
25+
26+
helper.buildController(module.exports);

‎src/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,10 @@ module.exports = {
208208
method: 'getAppConfig',
209209
},
210210
},
211+
'/health': {
212+
get: {
213+
controller: 'AppHealthController',
214+
method: 'getAppHealth',
215+
},
216+
},
211217
};

‎src/services/AppHealthService.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2019 TopCoder, Inc. All rights reserved.
3+
*/
4+
5+
/**
6+
* This service will provide app health related operations.
7+
*
8+
* @author Thomas Kranitsas
9+
* @version 1.0
10+
*/
11+
const config = require('../config');
12+
const helper = require('../common/helper');
13+
const errors = require('../common/errors');
14+
const User = require('../models').User;
15+
16+
/**
17+
* gets the application health
18+
* @returns {Object} the health details
19+
*/
20+
async function getAppHealth() {
21+
const checkMongoDB = new Promise((resolve, reject) => {
22+
User.findOne({}, (err, data) => {
23+
if (err) {
24+
return reject(new errors.ServiceUnavailable('MongoDB instance cannot be reached'));
25+
}
26+
return resolve();
27+
});
28+
});
29+
30+
const timeOutBreak = new Promise((resolve, reject) => {
31+
setTimeout(reject, config.MONGODB_TIMEOUT, new errors.ServiceUnavailable('MongoDB instance cannot be reached'));
32+
});
33+
34+
await Promise.race([checkMongoDB, timeOutBreak]);
35+
36+
return {
37+
checksRun: 1,
38+
};
39+
}
40+
41+
module.exports = {
42+
getAppHealth,
43+
};
44+
45+
helper.buildService(module.exports);

0 commit comments

Comments
 (0)
This repository has been archived.