Skip to content

Commit b79949d

Browse files
tnguyen14gergelyke
authored andcommitted
feat(health-check): extend info response with the resolved health-check promise
1 parent 0f5a518 commit b79949d

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ function onShutdown () {
3434
console.log('cleanup finished, server is shutting down');
3535
}
3636

37+
function healthCheck () {
38+
return Promise.resolve(
39+
// optionally include a resolve value to be included as
40+
// info in the healthcheck response
41+
)
42+
}
43+
3744
const server = http.createServer((request, response) => {
3845
response.end(
3946
`<html>
@@ -47,7 +54,7 @@ const server = http.createServer((request, response) => {
4754
const options = {
4855
// healtcheck options
4956
healthChecks: {
50-
'/healthcheck': check // a promise returning function indicating service health
57+
'/healthcheck': healthCheck // a promise returning function indicating service health
5158
},
5259

5360
// cleanup options

lib/terminus.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ function noopResolves () {
1515
return Promise.resolve()
1616
}
1717

18-
function sendSuccess (res) {
18+
function sendSuccess (res, info) {
1919
res.statusCode = 200
20+
if (info) {
21+
return res.end(JSON.stringify({
22+
status: 'ok',
23+
info: info
24+
}))
25+
}
2026
res.end(SUCCESS_RESPONSE)
2127
}
2228

@@ -42,8 +48,8 @@ function decorateWithHealthCheck (server, options) {
4248
return sendFailure(res)
4349
}
4450
healthChecks[req.url]()
45-
.then(() => {
46-
sendSuccess(res)
51+
.then((info) => {
52+
sendSuccess(res, info)
4753
})
4854
.catch((error) => {
4955
logger('healthcheck failed', error)

lib/terminus.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ describe('Terminus', () => {
5454
.catch(done)
5555
})
5656

57+
it('includes info on resolve', (done) => {
58+
let onHealthCheckRan = false
59+
60+
terminus(server, {
61+
healthChecks: {
62+
'/health': () => {
63+
onHealthCheckRan = true
64+
return Promise.resolve({
65+
version: '1.0.0'
66+
})
67+
}
68+
}
69+
})
70+
server.listen(8000)
71+
72+
fetch('http://localhost:8000/health')
73+
.then(res => {
74+
expect(res.status).to.eql(200)
75+
expect(onHealthCheckRan).to.eql(true)
76+
return res.json()
77+
})
78+
.then(json => {
79+
expect(json).to.deep.eql({
80+
status: 'ok',
81+
info: {
82+
version: '1.0.0'
83+
}
84+
})
85+
done()
86+
})
87+
.catch(done)
88+
})
89+
5790
it('returns 503 on reject', (done) => {
5891
let onHealthCheckRan = false
5992
let loggerRan = false

0 commit comments

Comments
 (0)