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

Add healthcheck #192

Merged
merged 3 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ npm run lint
- POST /issues - create an issue to Gitlab/Github
- POST /issues/recreate - recreate an issue DB and its challenge

- GET /health - gets the app health

## Configuration

please see [configuration.md](configuration.md).
Expand Down
1 change: 1 addition & 0 deletions configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following config parameters are supported, they are defined in `src/config.j
|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 |
|AWS_REGION | The Amazon certificate region to use when connecting. Use local dynamodb you can set fake value|FAKE_REGION |
|IS_LOCAL | Use Amazon DynamoDB Local or server. |true |
|AWS_CONNECTION_TIMEOUT | The timeout used to check if the app is healthy. |10000 |
|TC_LOGIN_URL | TC login url | |
|TC_USER_PROFILE_URL | TC user profile url | |

Expand Down
13,197 changes: 7,857 additions & 5,340 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ class ForbiddenError extends ApiError {
}
}

// The forbidden error
class ServiceUnavailable extends ApiError {
constructor(message, details) {
super(503, 'SERVICE_UNAVAILABLE', message);
this.details = details;
}
}

module.exports = {
ApiError,
ValidationError,
NotFoundError,
UnauthorizedError,
ForbiddenError,
ServiceUnavailable,
};
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ module.exports = {
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
AWS_REGION: process.env.AWS_REGION,
IS_LOCAL: process.env.IS_LOCAL
IS_LOCAL: process.env.IS_LOCAL,
TIMEOUT: process.env.AWS_CONNECTION_TIMEOUT || 10000, // eslint-disable-line no-magic-numbers
},
TOPCODER_VALUES: {
dev: {
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/AppHealthController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2019 TopCoder, Inc. All rights reserved.
*/

/**
* This controller exposes application health related endpoints.
*
* @author Thomas Kranitsas
* @version 1.0
*/
const helper = require('../common/helper');
const AppHealthService = require('../services/AppHealthService');

/**
* gets the application health
* @returns {Object} the health details
*/
async function getAppHealth() {
return await AppHealthService.getAppHealth();
}

module.exports = {
getAppHealth,
};

helper.buildController(module.exports);
2 changes: 2 additions & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ fs.readdirSync(__dirname).forEach((file) => { // eslint-disable-line no-sync
}
});

models.DynamoDB = dynamoose.ddb();

module.exports = models;
6 changes: 6 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,10 @@ module.exports = {
method: 'getAppConfig',
},
},
'/health': {
get: {
controller: 'AppHealthController',
method: 'getAppHealth',
},
},
};
45 changes: 45 additions & 0 deletions src/services/AppHealthService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019 TopCoder, Inc. All rights reserved.
*/

/**
* This service will provide app health related operations.
*
* @author Thomas Kranitsas
* @version 1.0
*/
const config = require('../config');
const helper = require('../common/helper');
const errors = require('../common/errors');
const { DynamoDB } = require('../models');

/**
* gets the application health
* @returns {Object} the health details
*/
async function getAppHealth() {
const checkDynamoDB = new Promise((resolve, reject) => {
DynamoDB.listTables({}, (err, data) => {
if (err) {
return reject(new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
}
return resolve();
});
});

const timeOutBreak = new Promise((resolve, reject) => {
setTimeout(reject, config.DYNAMODB.TIMEOUT, new errors.ServiceUnavailable('DynamoDB instance cannot be reached'));
});

await Promise.race([checkDynamoDB, timeOutBreak]);

return {
checksRun: 1,
};
}

module.exports = {
getAppHealth,
};

helper.buildService(module.exports);