-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwokeDyno.js
More file actions
37 lines (30 loc) · 1.06 KB
/
wokeDyno.js
File metadata and controls
37 lines (30 loc) · 1.06 KB
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
34
35
36
37
//source code:
//https://hackernoon.com/how-to-prevent-your-free-heroku-dyno-from-sleeping-dggxo3bi2
const fetch = require("node-fetch");
const wakeUpDyno = (url, interval = 25, callback) => {
const milliseconds = interval * 60000;
setTimeout(() => {
try {
console.log(`setTimeout called.`);
// HTTP GET request to the dyno's url
fetch(url).then(() => console.log(`Fetching ${url}.`));
}
catch (err) { // catch fetch errors
console.log(`Error fetching ${url}: ${err.message}
Will try again in ${interval} minutes...`);
}
finally {
try {
callback(); // execute callback, if passed
}
catch (e) { // catch callback error
callback ? console.log("Callback failed: ", e.message) : null;
}
finally {
// do it all again
return wakeUpDyno(url, interval, callback);
}
}
}, milliseconds);
};
module.exports = wakeUpDyno;