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

Commit d410551

Browse files
Add health check endpoint
1 parent d8ae673 commit d410551

File tree

7 files changed

+7946
-5341
lines changed

7 files changed

+7946
-5341
lines changed

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.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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { DynamoDB } = require('../models');
14+
15+
/**
16+
* gets the application health
17+
* @returns {Object} the health details
18+
*/
19+
async function getAppHealth() {
20+
const checkDynamoDB = new Promise((resolve, reject) => {
21+
DynamoDB.listTables({}, (err, data) => {
22+
if (err) {
23+
return reject(new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
24+
}
25+
return resolve();
26+
});
27+
});
28+
29+
const timeOutBreak = new Promise((resolve, reject) => {
30+
setTimeout(reject, config.DYNAMODB.TIMEOUT, new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
31+
});
32+
33+
await Promise.race([checkDynamoDB, timeOutBreak]);
34+
35+
return {
36+
checksRun: 1,
37+
};
38+
}
39+
40+
module.exports = {
41+
getAppHealth,
42+
};
43+
44+
helper.buildService(module.exports);

0 commit comments

Comments
 (0)