Skip to content

Instantly share code, notes, and snippets.

@Janoyan
Last active April 2, 2022 17:03
Show Gist options
  • Save Janoyan/3ed2195d24477e1922e1be33094cf6ad to your computer and use it in GitHub Desktop.
Save Janoyan/3ed2195d24477e1922e1be33094cf6ad to your computer and use it in GitHub Desktop.
const axios = require('axios');
const Heroku = require('heroku-client');
const heroku = new Heroku({ token: process.env.API_KEY })
const URLS = [
'https://api.myip.com/?id=1',
'https://api.myip.com/?id=2',
'https://api.myip.com/?id=3',
'https://api.myip.com/?id=4',
'https://api.myip.com/?id=5',
'https://api.myip.com/?id=6',
'https://api.myip.com/?id=7',
];
function throwRateLimitErrorIfNeeded(url) {
if (url.includes('id=5')) {
const err = new Error('Rate limit exceeded');
err.status = 429;
throw err;
}
}
async function main() {
try {
for await (const url of URLS) {
const response = await axios.get(url);
console.log(`URL: ${url} (${response.data.ip})`);
throwRateLimitErrorIfNeeded(url);
}
} catch (err) {
console.log(err.message);
if (err.status === 429) {
await restartMe();
}
}
}
async function restartMe() {
console.log('Restarting myself..')
await heroku.delete(`/apps/${process.env.APP_NAME}/dynos/${process.env.DYNO_NAME}.1`);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment