-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheroku.js
33 lines (27 loc) · 899 Bytes
/
heroku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require('dotenv').config()
const Heroku = require('heroku-client')
const heroku = new Heroku({ token: process.env.HEROKU_TARGET_APP_API_TOKEN })
const getDynos = () => heroku.get(`/apps/${process.env.APP_NAME}/dynos`)
const restartDyno = ({ id }) => heroku.delete(`/apps/${process.env.APP_NAME}/dynos/${id}`)
const restartAllDynos = async (delay) => {
console.log(`Restarting dynos for ${process.env.APP_NAME}`)
const dynos = getDynos()
await asyncForEach(dynos, async (dyno) => {
await restartDyno(dyno)
await sleep(delay)
})
console.log('Dynos restarted')
}
async function asyncForEach (array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
module.exports = {
getDynos,
restartDyno,
restartAllDynos,
sleep,
asyncForEach
}