Skip to content

Commit

Permalink
Use HMCTS NodeJs Healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
bradley-hmcts committed Apr 23, 2020
1 parent 741eeb7 commit 34b7225
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ exports.init = function(isA11yTest = false, a11yTestSession = {}, ftValue) {
store: utils.getStore(config.redis, session)
}));

healthcheck.setup(app);

app.use((req, res, next) => {
if (!req.session) {
return next(new Error('Unable to reach redis'));
Expand Down Expand Up @@ -220,8 +222,6 @@ exports.init = function(isA11yTest = false, a11yTestSession = {}, ftValue) {

app.post('*', sanitizeRequestBody);

app.use(healthcheck);

app.use(`${config.livenessEndpoint}`, (req, res) => {
res.json({status: 'UP'});
});
Expand Down
2 changes: 1 addition & 1 deletion app/core/steps/Step.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Step {
const correlationId = req.session.correlationId;
const formData = ServiceMapper.map(
'FormData',
[config.services.orchestration.url, sessionID]
[config.services.pcqBackend.url, sessionID]
);
// Set the completed date
formdata.completedDate = moment().toISOString();
Expand Down
67 changes: 50 additions & 17 deletions app/healthcheck.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
'use strict';

const router = require('express').Router();
const healthcheck = require('@hmcts/nodejs-healthcheck');
const os = require('os');
const gitProperties = require('git.properties');
const commonContent = require('app/resources/en/translation/common');
const gitRevision = process.env.GIT_REVISION;
const config = require('config');
const outputs = require('@hmcts/nodejs-healthcheck/healthcheck/outputs');
const getStore = require('app/components/utils').getStore;
const session = require('express-session');
const logger = require('app/components/logger')('init');

const osHostname = os.hostname();
const gitProperties = require('git.properties');
const gitCommitId = gitProperties.git.commit.id;
const config = require('config');
const gitRevision = process.env.GIT_REVISION;

const healthOptions = message => {
return {
callback: (error, res) => {
if (error) {
logger.error(null, 'health_check_error', message, error);
}
return !error && res.status === 200 ? outputs.up() : outputs.down(error);
},
timeout: config.health.timeout,
deadline: config.health.deadline
};
};

const sessionStore = getStore(config.redis, session);
const checks = {
'pcq-backend': healthcheck.web(`${config.services.pcqBackend.url}/health`,
healthOptions('Health check failed on pcq-backend:')
)
};
if (sessionStore.constructor.name === 'RedisStore') {
checks.redis = healthcheck.raw(() => {
return sessionStore.client.status === 'ready' ? healthcheck.up() : healthcheck.down();
});
}

router.get(`${config.app.basePath}/health`, (req, res) => {
res.json({
name: commonContent.serviceName,
status: 'UP',
uptime: process.uptime(),
host: osHostname,
version: gitRevision,
gitCommitId
const setup = app => {
healthcheck.addTo(app, {
checks: checks,
buildInfo: {
name: config.service.name,
host: os.hostname(),
uptime: process.uptime(),
version: gitRevision,
gitCommitId
}
});
});
};

module.exports = router;
module.exports.osHostname = osHostname;
module.exports.gitCommitId = gitCommitId;
module.exports = {
setup: setup,
osHostname: osHostname,
gitCommitId: gitCommitId
};
2 changes: 1 addition & 1 deletion app/services/FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Service = require('./Service');

class FormData extends Service {
post(token, correlationId, data = {}) {
const url = this.endpoint + this.config.services.orchestration.paths.forms;
const url = this.endpoint + this.config.services.pcqBackend.paths.forms;
this.log('Post application form data');
const headers = {
'Content-Type': 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion charts/pcq-frontend/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nodejs:
REDIS_USE_TLS: true
REDIS_PORT: 6380
REDIS_HOST: pcq-frontend-redis-cache-{{ .Values.global.environment }}.redis.cache.windows.net
ORCHESTRATION_SERVICE_URL: http://pcq-backend-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal
PCQ_BACKEND_URL: http://pcq-backend-{{ .Values.global.environment }}.service.core-compute-{{ .Values.global.environment }}.internal
REFORM_ENVIRONMENT: '{{ .Values.global.environment }}'
image: hmctspublic.azurecr.io/pcq/frontend:latest
ingressHost: pcq.{{ .Values.global.environment }}.platform.hmcts.net
Expand Down
4 changes: 2 additions & 2 deletions config/custom-environment-variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ auth:
jwt:
secret: JWT_SECRET
services:
orchestration:
url: ORCHESTRATION_SERVICE_URL
pcqBackend:
url: PCQ_BACKEND_URL
redis:
host: REDIS_HOST
port: REDIS_PORT
Expand Down
9 changes: 7 additions & 2 deletions config/default.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

---
service:
name: 'pcq-frontend'
questionsVersion: 1
frontendPublicHttpProtocol: 'http'
environment: 'prod'
Expand All @@ -24,7 +26,7 @@ auth:
ttl: '8h'
secret: 'JwtSecretKey'
services:
orchestration:
pcqBackend:
url: 'http://localhost:4550'
port: 4550
paths:
Expand Down Expand Up @@ -79,3 +81,6 @@ livenessEndpoint: "/health/liveness"
healthEndpoint: "/health"
appInsights:
instrumentationKey: ''
health:
timeout: 5000
deadline: 15000
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"lint"
],
"dependencies": {
"@hmcts/nodejs-healthcheck": "^1.6.0",
"@hmcts/nodejs-logging": "^3.0.0",
"@hmcts/properties-volume": "^0.0.9",
"ajv": "^4.11.8",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/services/testFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('FormDataService', () => {
'Authorization': 'Bearer ' + token,
'X-Correlation-Id': correlationId
}
}).post(config.services.orchestration.paths.forms, expectedForm)
}).post(config.services.pcqBackend.paths.forms, expectedForm)
.reply(200, expectedForm);

co(function* () {
Expand Down
53 changes: 51 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
lodash.mergewith "^4.6.2"
webdriverio "^5.15.2"

"@hmcts/nodejs-healthcheck@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@hmcts/nodejs-healthcheck/-/nodejs-healthcheck-1.6.0.tgz#ecc2002f2617b207c0cc48c6c11198e53fc8bb68"
integrity sha512-i9PsSbbZoI7fghnVNi6sWnwvWnJdTtEz0ytBuCogpLOVQLpeMh1FTub79AxAxIdJIvpS/s6lyGg5U+gk2mdhxA==
dependencies:
"@hmcts/nodejs-logging" "^3.0.1"
js-yaml "^3.8.4"
superagent "5"

"@hmcts/nodejs-logging@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@hmcts/nodejs-logging/-/nodejs-logging-3.0.0.tgz#81d84cea5528fa57b4162641b427233fca771715"
Expand All @@ -55,6 +64,15 @@
on-finished "^2.3.0"
winston "^2.4.1"

"@hmcts/nodejs-logging@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@hmcts/nodejs-logging/-/nodejs-logging-3.0.1.tgz#370bfee6c1d502ea7fec13001632bb03f15d072d"
integrity sha512-BpOBBFvUfy3d/pGi/s0s2CxtNwnLEm1hJcsEH7av9NpXaGL1rO0zfAcEk4eKNKYZVBEoiyXlpajFtAutSAFWfg==
dependencies:
moment "^2.19.3"
on-finished "^2.3.0"
winston "^2.4.1"

"@hmcts/properties-volume@^0.0.9":
version "0.0.9"
resolved "https://registry.yarnpkg.com/@hmcts/properties-volume/-/properties-volume-0.0.9.tgz#1c2b29a563fcad9041231442476a4a3b7a8abf7d"
Expand Down Expand Up @@ -1429,7 +1447,7 @@ colors@^1.1.2:
resolved "https://registry.yarnpkg.com/colors/-/colors-1.3.3.tgz#39e005d546afe01e01f9c4ca8fa50f686a01205d"
integrity sha512-mmGt/1pZqYRjMxB1axhTo16/snVZ5krrKkcmMeVKxzECMMXoCgnvTPp10QgHfcbQZw8Dq2jMNG6je4JlWU0gWg==

combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -2734,6 +2752,11 @@ fast-safe-stringify@^2.0.6:
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz#04b26106cc56681f51a044cfc0d76cf0008ac2c2"
integrity sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg==

fast-safe-stringify@^2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz#124aa885899261f68aedb42a7c080de9da608743"
integrity sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==

fd-slicer@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.0.1.tgz#8b5bcbd9ec327c5041bf9ab023fd6750f1177e65"
Expand Down Expand Up @@ -2978,6 +3001,15 @@ form-data@^2.5.0:
combined-stream "^1.0.6"
mime-types "^2.1.12"

form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -4299,7 +4331,7 @@ js-tokens@^3.0.2:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
integrity sha1-mGbfOVECEw449/mWvOtlRDIJwls=

[email protected], [email protected], js-yaml@^3.13.1, js-yaml@^3.7.0, js-yaml@^3.9.1:
[email protected], [email protected], js-yaml@^3.13.1, js-yaml@^3.7.0, js-yaml@^3.8.4, js-yaml@^3.9.1:
version "3.13.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==
Expand Down Expand Up @@ -7447,6 +7479,23 @@ stryker@^0.23.0:
tslib "^1.5.0"
typed-rest-client "^1.0.7"

superagent@5:
version "5.2.2"
resolved "https://registry.yarnpkg.com/superagent/-/superagent-5.2.2.tgz#6ff726c5642795b2c27009e92687c8e69a6bb07d"
integrity sha512-pMWBUnIllK4ZTw7p/UaobiQPwAO5w/1NRRTDpV0FTVNmECztsxKspj3ZWEordVEaqpZtmOQJJna4yTLyC/q7PQ==
dependencies:
component-emitter "^1.3.0"
cookiejar "^2.1.2"
debug "^4.1.1"
fast-safe-stringify "^2.0.7"
form-data "^3.0.0"
formidable "^1.2.1"
methods "^1.1.2"
mime "^2.4.4"
qs "^6.9.1"
readable-stream "^3.4.0"
semver "^6.3.0"

superagent@^3.7.0, superagent@^3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/superagent/-/superagent-3.8.3.tgz#460ea0dbdb7d5b11bc4f78deba565f86a178e128"
Expand Down

0 comments on commit 34b7225

Please sign in to comment.