@godaddy/terminus
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -18,3 +18,5 @@ 'use strict' | ||
function sendSuccess (res, info) { | ||
async function sendSuccess (res, options) { | ||
const { info } = options | ||
res.statusCode = 200 | ||
@@ -31,3 +33,8 @@ res.setHeader('Content-Type', 'application/json') | ||
function sendFailure (res, error) { | ||
async function sendFailure (res, options) { | ||
const { error, onSendFailureDuringShutdown } = options | ||
if (onSendFailureDuringShutdown) { | ||
await onSendFailureDuringShutdown() | ||
} | ||
res.statusCode = 503 | ||
@@ -51,3 +58,3 @@ res.setHeader('Content-Type', 'application/json') | ||
function decorateWithHealthCheck (server, state, options) { | ||
const { healthChecks, logger } = options | ||
const { healthChecks, logger, onSendFailureDuringShutdown } = options | ||
@@ -59,3 +66,3 @@ server.listeners('request').forEach((listener) => { | ||
if (state.isShuttingDown) { | ||
return sendFailure(res) | ||
return sendFailure(res, { onSendFailureDuringShutdown }) | ||
} | ||
@@ -67,5 +74,5 @@ let info | ||
logger('healthcheck failed', error) | ||
return sendFailure(res, error.causes) | ||
return sendFailure(res, { error: error.causes }) | ||
} | ||
sendSuccess(res, info) | ||
return sendSuccess(res, { info }) | ||
} else { | ||
@@ -111,2 +118,3 @@ listener(req, res) | ||
healthChecks = {}, | ||
onSendFailureDuringShutdown, | ||
onShutdown = noopResolves, | ||
@@ -121,3 +129,4 @@ beforeShutdown = noopResolves, | ||
healthChecks, | ||
logger | ||
logger, | ||
onSendFailureDuringShutdown | ||
}) | ||
@@ -124,0 +133,0 @@ } |
@@ -139,4 +139,12 @@ 'use strict' | ||
it('returns 503 once signal received', (done) => { | ||
execFile('node', ['lib/standalone-tests/terminus.onsignal.fail.js']) | ||
let responseAssertionsComplete = false | ||
// We're only truly finished when the response has been analyzed and the forked http process has exited, | ||
// freeing up port 8000 for future tests | ||
execFile('node', ['lib/standalone-tests/terminus.onsignal.fail.js'], (error) => { | ||
expect(error.signal).to.eql('SIGINT') | ||
expect(responseAssertionsComplete).to.eql(true) | ||
done() | ||
}) | ||
// let the process start up | ||
@@ -147,7 +155,53 @@ setTimeout(() => { | ||
expect(res.status).to.eql(503) | ||
done() | ||
responseAssertionsComplete = true | ||
}) | ||
.catch(done) | ||
}, 300) | ||
}) | ||
it('calls onSendFailureDuringShutdown when sending 503 during shutdown', (done) => { | ||
let responseAssertionsComplete = false | ||
// We're only truly finished when the response has been analyzed and the forked http process has exited, | ||
// freeing up port 8000 for future tests | ||
execFile('node', ['lib/standalone-tests/terminus.onsendfailureduringshutdown.js'], | ||
(error, stdout) => { | ||
expect(error.signal).to.eql('SIGTERM') | ||
expect(stdout).to.eql('onSendFailureDuringShutdown\n') | ||
expect(responseAssertionsComplete).to.eql(true) | ||
done() | ||
}) | ||
// let the process start up | ||
setTimeout(() => { | ||
fetch('http://localhost:8000/health') | ||
.then(res => { | ||
expect(res.status).to.eql(503) | ||
responseAssertionsComplete = true | ||
}) | ||
}, 300) | ||
}) | ||
it('does NOT call onSendFailureDuringShutdown when sending 503 during failed healthcheck', (done) => { | ||
let responseAssertionsComplete = false | ||
// We're only truly finished when the response has been analyzed and the forked http process has exited, | ||
// freeing up port 8000 for future tests | ||
execFile('node', ['lib/standalone-tests/terminus.onsendfailureduringshutdown.failed-health.js'], | ||
(error, stdout) => { | ||
expect(error.signal).to.eql('SIGTERM') | ||
// Here, we expect NOT to see "onSendFailureDuringShutdown" | ||
expect(stdout).to.eql('') | ||
expect(responseAssertionsComplete).to.eql(true) | ||
done() | ||
}) | ||
// let the process start up | ||
setTimeout(() => { | ||
fetch('http://localhost:8000/health') | ||
.then(res => { | ||
expect(res.status).to.eql(503) | ||
responseAssertionsComplete = true | ||
}) | ||
}, 300) | ||
}) | ||
}) | ||
@@ -154,0 +208,0 @@ |
{ | ||
"name": "@godaddy/terminus", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -63,6 +63,7 @@ | ||
signal, // [optional = 'SIGTERM'] what signal to listen for relative to shutdown | ||
signals, // [optional = []] array of signals to listen for relative to shutdown | ||
signals, // [optional = []] array of signals to listen for relative to shutdown | ||
beforeShutdown, // [optional] called before the HTTP server starts its shutdown | ||
onSignal, // [optional] cleanup function, returning a promise (used to be onSigterm) | ||
onShutdown, // [optional] called right before exiting | ||
onSendFailureDuringShutdown, // [optional] called before sending each 503 during shutdowns | ||
@@ -69,0 +70,0 @@ // both |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38262
44
904
179
28