Last active
April 2, 2022 17:03
-
-
Save Janoyan/3ed2195d24477e1922e1be33094cf6ad to your computer and use it in GitHub Desktop.
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
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