Last active
August 29, 2015 14:27
-
-
Save johnbeech/82ab3d034de359ec5512 to your computer and use it in GitHub Desktop.
Test if a status endpoint is working by polling it every 10 seconds for 2 minutes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var assert = require('assert'); | |
var testUrl = process.argv[2] || 'No url set'; | |
var expectedStatusBody = 'Status 200 OK'; | |
// Try for 2 minutes, every 10 seconds, to see if server has booted up | |
runTest(12); | |
function runTest(attempts) { | |
var retryDelayMs = 10000; | |
if(attempts <= 0) { | |
console.log('To many failed attempts, gave up.'); | |
process.exit(1); | |
} | |
request.get(testUrl, function(error, response, body) { | |
if(body) { | |
console.log('Body', body); | |
try { | |
assert.equal(expectedStatusBody, body); | |
console.log('Status Endpoint OK'); | |
process.exit(0); | |
} | |
catch(exception) { | |
console.log('Failed attempt: ', error, attempts, 'remaining'); | |
setTimeout(runTest, retryDelayMs, attempts - 1); | |
} | |
} | |
else { | |
console.log('Error', error); | |
console.log('Failed attempt: ', error, attempts, 'remaining'); | |
setTimeout(runTest, retryDelayMs, attempts - 1); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment