Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 819
Basim Hennawi Avatar asked Nov 17 '16 04:11

Basim Hennawi


People also ask

How do I keep my Heroku app from sleeping?

Kaffeine pings your Heroku app every 30 minutes so it will never go to sleep* (read more)

Why does Heroku app sleep?

If an app has a free web dyno, and that dyno receives no web traffic in a 30-minute period, it will sleep. In addition to the web dyno sleeping, the worker dyno (if present) will also sleep. Free web dynos do not consume free dyno hours while sleeping.

Does Heroku stay online?

You have two options to keep your app awake: You can leave your Heroku account unverified (i.e. without a credit card linked) and have enough free hours for your app to run 18 hours per day.

Is Heroku online 24 7?

Starting today, Heroku accounts have an account-based pool of free dyno hours for use on free apps. This replaces the 18 hours a day limit on free apps, allowing a free app to run 24/7 if needed. New accounts receive 550 free dyno hours and you can verify your identity with a credit card for an additional 450 hours.


2 Answers

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.

like image 167
Yoni Rabinovitch Avatar answered Oct 06 '22 23:10

Yoni Rabinovitch


Try Kaffeine it will prevent your app from sleeping. Heroku obliges free apps to sleep 6 hours. Kaffeine gives you the possibility to choose when your app sleeps

like image 26
Malek Kamoua Avatar answered Oct 06 '22 22:10

Malek Kamoua