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 1 commit
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
Next Next commit
Add health check endpoint
  • Loading branch information
ThomasKranitsas committed Oct 1, 2019
commit e0612f754028b9a3e3348d5cccdd7191e3c53b57
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.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',
},
},
};
44 changes: 44 additions & 0 deletions src/services/AppHealthService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { 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);