Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit b6234f0

Browse files
authored
Merge pull request #192 from topcoder-platform/add-healthcheck
Add healthcheck
2 parents d8ae673 + bad2f65 commit b6234f0

File tree

9 files changed

+7950
-5341
lines changed

9 files changed

+7950
-5341
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ npm run lint
5151
- POST /issues - create an issue to Gitlab/Github
5252
- POST /issues/recreate - recreate an issue DB and its challenge
5353

54+
- GET /health - gets the app health
55+
5456
## Configuration
5557

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

configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The following config parameters are supported, they are defined in `src/config.j
3232
|AWS_SECRET_ACCESS_KEY | The Amazon certificate access key to use when connecting. Use local dynamodb you can set fake value|FAKE_SECRET_ACCESS_KEY |
3333
|AWS_REGION | The Amazon certificate region to use when connecting. Use local dynamodb you can set fake value|FAKE_REGION |
3434
|IS_LOCAL | Use Amazon DynamoDB Local or server. |true |
35+
|AWS_CONNECTION_TIMEOUT | The timeout used to check if the app is healthy. |10000 |
3536
|TC_LOGIN_URL | TC login url | |
3637
|TC_USER_PROFILE_URL | TC user profile url | |
3738

package-lock.json

Lines changed: 7857 additions & 5340 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/errors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,19 @@ class ForbiddenError extends ApiError {
5454
}
5555
}
5656

57+
// The forbidden error
58+
class ServiceUnavailable extends ApiError {
59+
constructor(message, details) {
60+
super(503, 'SERVICE_UNAVAILABLE', message);
61+
this.details = details;
62+
}
63+
}
64+
5765
module.exports = {
5866
ApiError,
5967
ValidationError,
6068
NotFoundError,
6169
UnauthorizedError,
6270
ForbiddenError,
71+
ServiceUnavailable,
6372
};

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ module.exports = {
5454
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
5555
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
5656
AWS_REGION: process.env.AWS_REGION,
57-
IS_LOCAL: process.env.IS_LOCAL
57+
IS_LOCAL: process.env.IS_LOCAL,
58+
TIMEOUT: process.env.AWS_CONNECTION_TIMEOUT || 10000, // eslint-disable-line no-magic-numbers
5859
},
5960
TOPCODER_VALUES: {
6061
dev: {
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/models/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ fs.readdirSync(__dirname).forEach((file) => { // eslint-disable-line no-sync
3333
}
3434
});
3535

36+
models.DynamoDB = dynamoose.ddb();
37+
3638
module.exports = models;

src/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,10 @@ module.exports = {
226226
method: 'getAppConfig',
227227
},
228228
},
229+
'/health': {
230+
get: {
231+
controller: 'AppHealthController',
232+
method: 'getAppHealth',
233+
},
234+
},
229235
};

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 { DynamoDB } = require('../models');
15+
16+
/**
17+
* gets the application health
18+
* @returns {Object} the health details
19+
*/
20+
async function getAppHealth() {
21+
const checkDynamoDB = new Promise((resolve, reject) => {
22+
DynamoDB.listTables({}, (err, data) => {
23+
if (err) {
24+
return reject(new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
25+
}
26+
return resolve();
27+
});
28+
});
29+
30+
const timeOutBreak = new Promise((resolve, reject) => {
31+
setTimeout(reject, config.DYNAMODB.TIMEOUT, new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
32+
});
33+
34+
await Promise.race([checkDynamoDB, 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)