feat(ch07s02): Add readiness probe switch (#152)
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -69,3 +69,8 @@ nb-configuration.xml
|
|||||||
## OS X
|
## OS X
|
||||||
##########################
|
##########################
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
##########################
|
||||||
|
## JavaScript
|
||||||
|
##########################
|
||||||
|
node_modules/
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ var express = require('express'),
|
|||||||
app = express();
|
app = express();
|
||||||
|
|
||||||
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
|
||||||
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
|
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
|
||||||
|
healthy = true,
|
||||||
|
ready = true;
|
||||||
|
|
||||||
var route = express.Router();
|
var route = express.Router();
|
||||||
|
|
||||||
// global var to track app health
|
|
||||||
var healthy = true;
|
|
||||||
|
|
||||||
app.use('/', route);
|
app.use('/', route);
|
||||||
|
|
||||||
// A route that says hello
|
// A route that says hello
|
||||||
@@ -17,11 +16,10 @@ route.get('/', function(req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// A route that returns readiness status
|
// A route that returns readiness status
|
||||||
// simulates readiness 30 seconds after start up
|
|
||||||
route.get('/ready', function (req, res) {
|
route.get('/ready', function (req, res) {
|
||||||
var now = Math.floor(Date.now() / 1000);
|
var now = Math.floor(Date.now() / 1000);
|
||||||
var lapsed = now - started;
|
var lapsed = now - started;
|
||||||
if (lapsed > 30) {
|
if (ready && (lapsed > 30)) {
|
||||||
console.log('ping /ready => pong [ready]');
|
console.log('ping /ready => pong [ready]');
|
||||||
res.send('Ready for service requests...\n');
|
res.send('Ready for service requests...\n');
|
||||||
}
|
}
|
||||||
@@ -41,25 +39,30 @@ route.get('/healthz', function(req, res) {
|
|||||||
else {
|
else {
|
||||||
console.log('ping /healthz => pong [unhealthy]');
|
console.log('ping /healthz => pong [unhealthy]');
|
||||||
res.status(503);
|
res.status(503);
|
||||||
res.send('Error!. App not healthy!\n');
|
res.send('Error! App not healthy!\n');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// This route handles switching the state of the app
|
// This route handles switching the state of the app
|
||||||
route.route('/flip').get(function (req, res) {
|
route.route('/flip').get(function (req, res) {
|
||||||
|
|
||||||
var flag = req.query.op;
|
var flag = req.query.op;
|
||||||
if (flag == "kill") {
|
if (flag === 'kill-health') {
|
||||||
console.log('Received kill request. Changing app state to unhealthy...');
|
console.log('Received kill request for health probe.');
|
||||||
healthy = false;
|
healthy = false;
|
||||||
res.send('Switched app state to unhealthy...\n');
|
res.send('Switched app state to unhealthy...\n');
|
||||||
}
|
} else if (flag === "awaken-health") {
|
||||||
else if (flag == "awaken") {
|
console.log('Received awaken request for health probe.');
|
||||||
console.log('Received awaken request. Changing app state to healthy...');
|
|
||||||
healthy = true;
|
healthy = true;
|
||||||
res.send('Switched app state to healthy...\n');
|
res.send('Switched app state to healthy...\n');
|
||||||
}
|
} else if (flag === 'kill-ready') {
|
||||||
else {
|
console.log('Received kill request for readiness probe.');
|
||||||
|
ready = false;
|
||||||
|
res.send('Switched app state to not ready...\n');
|
||||||
|
} else if (flag === 'awaken-ready') {
|
||||||
|
console.log('Received awaken request for readiness probe.');
|
||||||
|
ready = true;
|
||||||
|
res.send('Switched app state to ready...\n');
|
||||||
|
} else {
|
||||||
res.send('Error! unknown flag...\n');
|
res.send('Error! unknown flag...\n');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,6 +9,6 @@
|
|||||||
"author": "Red Hat Training",
|
"author": "Red Hat Training",
|
||||||
"license": "ASL",
|
"license": "ASL",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "4.14.x"
|
"express": "4.17.x"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user