Skip to content

Commit

Permalink
feat(ch07s02): Add readiness probe switch (RedHatTraining#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Czernek authored Jul 6, 2021
1 parent 237c9e1 commit 9ac3d08
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ nb-configuration.xml
## OS X
##########################
.DS_Store

##########################
## JavaScript
##########################
node_modules/
97 changes: 50 additions & 47 deletions probes/app.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
var express = require('express'),
app = express();
app = express();

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();

// global var to track app health
var healthy = true;

app.use('/', route);

// A route that says hello
route.get('/', function(req, res) {
res.send('Hello! This is the index page for the app.\n');
route.get('/', function (req, res) {
res.send('Hello! This is the index page for the app.\n');
});

// A route that returns readiness status
// simulates readiness 30 seconds after start up
route.get('/ready', function(req, res) {
var now = Math.floor(Date.now() / 1000);
var lapsed = now - started;
if (lapsed > 30) {
console.log('ping /ready => pong [ready]');
res.send('Ready for service requests...\n');
}
else {
console.log('ping /ready => pong [notready]');
res.status(503);
res.send('Error! Service not ready for requests...\n');
}
route.get('/ready', function (req, res) {
var now = Math.floor(Date.now() / 1000);
var lapsed = now - started;
if (ready && (lapsed > 30)) {
console.log('ping /ready => pong [ready]');
res.send('Ready for service requests...\n');
}
else {
console.log('ping /ready => pong [notready]');
res.status(503);
res.send('Error! Service not ready for requests...\n');
}
});

// A route that returns health status
route.get('/healthz', function(req, res) {
if (healthy) {
console.log('ping /healthz => pong [healthy]');
res.send('OK\n');
}
else {
console.log('ping /healthz => pong [unhealthy]');
res.status(503);
res.send('Error!. App not healthy!\n');
}
route.get('/healthz', function (req, res) {
if (healthy) {
console.log('ping /healthz => pong [healthy]');
res.send('OK\n');
}
else {
console.log('ping /healthz => pong [unhealthy]');
res.status(503);
res.send('Error! App not healthy!\n');
}
});

// This route handles switching the state of the app
route.route('/flip').get(function(req, res) {

var flag = req.query.op;
if (flag == "kill") {
console.log('Received kill request. Changing app state to unhealthy...');
healthy = false;
res.send('Switched app state to unhealthy...\n');
}
else if (flag == "awaken") {
console.log('Received awaken request. Changing app state to healthy...');
healthy = true;
res.send('Switched app state to healthy...\n');
}
else {
res.send('Error! unknown flag...\n');
}
});
route.route('/flip').get(function (req, res) {
var flag = req.query.op;
if (flag === 'kill-health') {
console.log('Received kill request for health probe.');
healthy = false;
res.send('Switched app state to unhealthy...\n');
} else if (flag === "awaken-health") {
console.log('Received awaken request for health probe.');
healthy = true;
res.send('Switched app state to healthy...\n');
} else if (flag === 'kill-ready') {
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');
}
});

app.listen(port, ip);
console.log('nodejs server running on http://%s:%s', ip, port);
Expand Down
2 changes: 1 addition & 1 deletion probes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"author": "Red Hat Training",
"license": "ASL",
"dependencies": {
"express": "4.14.x"
"express": "4.17.x"
}
}

0 comments on commit 9ac3d08

Please sign in to comment.