Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: configure readiness probe to fail after SIGTERM received #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let metrics;
let readinessChecks = [];
let healthChecks = [];
let isShuttingDown = false;

const prometheusContentType = 'text/plain; version=0.0.4';

Expand Down Expand Up @@ -38,6 +39,10 @@ const getMiddleware = () => (req, res, next) => {
return Promise.resolve().then(() => res.sendStatus(200));
}

if (isShuttingDown) {
return Promise.resolve().then(() => res.sendStatus(500));
}

const promisedChecks = readinessChecks.map((check) =>
new Promise((resolve, reject) => doReadinessCheck(check)
.then(resolve)
Expand Down Expand Up @@ -85,10 +90,16 @@ const getMiddleware = () => (req, res, next) => {
return next();
};

// Listen for requests to kill the process and initiate the safe shutdown.
process.on('SIGTERM', () => {
isShuttingDown = true;
});

const reset = () => {
metrics = undefined;
readinessChecks = [];
healthChecks = [];
isShuttingDown = false;
};

module.exports = {
Expand Down
23 changes: 21 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,34 @@ describe('bw-monitoring', () => {
it('should return 500 if any check fails', (done) => {
mon.addReadinessCheck({ name: 'bob', check: (ok) => ok() });
mon.addReadinessCheck({ name: 'bob2', check: (ok, warning) => warning() });
mon.addReadinessCheck({ name: 'bob2', check: (ok, warning, critical) => critical() });
mon.addReadinessCheck({ name: 'bob2', check: (ok, warning, critical, unknown) => unknown() });
mon.addReadinessCheck({ name: 'bob3', check: (ok, warning, critical) => critical() });
mon.addReadinessCheck({ name: 'bob4', check: (ok, warning, critical, unknown) => unknown() });
const mid = mon.getMiddleware();
mid(req, res, next)
.then(() => {
assert(res.sendStatus.calledWith(500));
done();
});
});

it('should return 500 after receiving SIGTERM', (done) => {
mon.addReadinessCheck({ name: 'bob', check: (ok) => ok() });
const mid = mon.getMiddleware();
mid(req, res, next)
.then(() => {
assert(res.sendStatus.calledWith(200));
})
.then(() => {
process.once('SIGTERM', () => {
mid(req, res, next)
.then(() => {
assert(res.sendStatus.calledWith(500));
done();
});
});
});
process.kill(process.pid, 'SIGTERM');
});
});

describe('/checkz endpoint', () => {
Expand Down