Search code examples
javascriptnode.jsherokujobs

Avoid heroku server from sleeping


I got a website hosted on a Heroku server (i'm a new to Heroku btw), and as it's under the free package, it sleeps after 30m of inactivity, and to put it in action again when a user hits it, it takes around 7 secs to npm run start successfully.

I'm thinking of run a nodejs job or something that open the website every 29m so that the server never sleeps, initially, I got something like this:

(function wakeup() {
  require('open')('https://mywebsite.herokuapp.com', (err) => {
    if (err) throw err;
    console.log('Woke up!');
    setTimeout(wakeup, 1740000); //29m
  });
})()

N.B.: That just opens it in a browser, but not handling closing it.

  • First, is it legal to do this workaround?
  • Second, if yes, what's the best approach to implement this?

Solution

  • It is perfectly legal to keep your free dyno awake for as long as you want, by any means at your disposal. Just note that your monthly allocation of free dyno hours is limited. If you verified your Heroku account with a credit card, you have 1000 free dyno hours per month, which is more than enough to keep a single free web dyno up "forever" without sleeping at all. A very easy way to do that is simply to configure a New Relic add-on for your app (using the free New Relic plan), which you can easily configure to ping your Heroku app periodically. For additional methods to prevent your Heroku app from idling, see here.