Created
November 8, 2019 21:14
-
-
Save tyler-boyd/5cdc486cb1edf1df680ed2ec55368678 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
const process = require("process"); | |
const fetch = require("node-fetch"); | |
if (process.argv.length < 3 || process.argv.length > 4) { | |
console.error(`Usage: ${process.argv[0]} endpoint timeout`); | |
process.exit(2); | |
} | |
const endpoint = process.argv[2]; | |
const timeout = parseInt(process.argv[3]) || 10; | |
const wait = (delay) => { | |
return new Promise((r) => setTimeout(r, delay)); | |
}; | |
console.log(`Going to try hitting ${endpoint} for ${timeout} seconds`); | |
(async () => { | |
for(let tries = 0; tries < timeout; tries += 1) { | |
try { | |
await fetch(endpoint); | |
console.log(`${endpoint} is up after ${tries} seconds`); | |
process.exit(0); | |
} catch (err) { | |
if (tries % 15 === 0) { | |
console.log(`${endpoint} is not up after ${tries} seconds`); | |
} | |
await wait(1000); | |
} | |
} | |
console.error(`${endpoint} never came up 😭`); | |
process.exit(1); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment