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

create health check endpoint for mongodb #193

Merged
merged 1 commit into from
Oct 1, 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 @@ -48,6 +48,8 @@ npm run lint
- GET /users/settings - gets the current user's setup
- GET /users/accessToken - gets the user's access token

- 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 @@ -31,6 +31,7 @@ The following config parameters are supported, they are defined in `src/config.j
|TOPCODER_AUTH_SECRET| The auth secret used to sign the JWT| No default - needs to be set up|
|TOPCODER_VALID_ISSUERS| Stringified array of valid JWT issuers| `'["topcoder-dev.com"]'`|
|TOPCODER_JWT_KEY_CACHE_TIME| They JWT cache time | 90 |
|MONGODB_TIMEOUT | The timeout used to check if the app is healthy. |10000 |

## GitHub OAuth App Setup

Expand Down
8 changes: 8 additions & 0 deletions src/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,19 @@ class ForbiddenError extends ApiError {
this.details = details;
}
}
// 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,
};
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
API_VERSION: process.env.API_VERSION || 'v1',
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
MONGODB_URI: process.env.MONGODB_URI || 'mongodb://localhost:27017/topcoderx',
MONGODB_TIMEOUT: process.env.MONGODB_TIMEOUT || 10000, // eslint-disable-line no-magic-numbers
SESSION_SECRET: process.env.SESSION_SECRET || 'kjsdfkj34857',
// Github and gitlab client id and secret
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID || 'ae39bea2a2a23f1dd032',
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);
6 changes: 6 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,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 User = require('../models').User;

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

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

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

return {
checksRun: 1,
};
}

module.exports = {
getAppHealth,
};

helper.buildService(module.exports);