Turntable-API icon indicating copy to clipboard operation
Turntable-API copied to clipboard

How do I avoid Heroku Free Tier's 1hr "Sleep Mode"?

Open ARH3 opened this issue 11 years ago • 26 comments

Hey-

Wondering if anyone has any experience hosting on heroku using just 1 dyno (free tier). It is working great for me except when using only 1 dyno it goes to sleep about an hour of "inactivity". I am curious how one goes about avoiding this. Is it as simple as setting the bot to run a command every 59 minutes, or does it require user input (ie: someone giving him a command) to avoid this sleeping issue.

Thanks!

ARH3 avatar Jul 18 '13 16:07 ARH3

The only way to avoid the problem is to access your app from the web at least once an hour. API activity isn't enough to keep Heroku (or for that matter, OpenShift) from idling your application.

There's a listen action you can use to start listening for requests but you'd have to check the Heroku docs to see how to find the port/address to listen on.

gizmotronic avatar Jul 18 '13 17:07 gizmotronic

Could the bot ping itself? Hit a "web page" every 50 minutes? — Mike Wills [email protected] http://mikewills.me Ph: 507-933-0880

On Thu, Jul 18, 2013 at 12:34 PM, gizmo_tronic [email protected] wrote:

The only way to avoid the problem is to access your app from the web at least once an hour. API activity isn't enough to keep Heroku (or for that matter, OpenShift) from idling your application.

There's a listen action you can use to start listening for requests but you'd have to check the Heroku docs to see how to find the port/address to listen on.

Reply to this email directly or view it on GitHub: https://github.com/alaingilbert/Turntable-API/issues/243#issuecomment-21200345

MikeWills avatar Jul 18 '13 17:07 MikeWills

In theory, yes, but I'm sure it depends on the provider. It'd be interesting to know whether they will keep the dyno up if the access comes from the same machine.

gizmotronic avatar Jul 18 '13 18:07 gizmotronic

gizmotronic, are you saying I have to view the actual page, as opposed to interacting via a TT room?

I've designed the bot so it can talk to people in the public chat. For example, if you say "Hi Bruce", he will respond with a witty comment (for context, the bot's name is Bruce). So perhaps if ever 50 minutes I could make him say a certain command that will trigger one of his pre-defined responses.

I will probably try this approach when I get a chance to make some adjustments.

ARH3 avatar Jul 18 '13 18:07 ARH3

Per this Stack Overflow page it appears that you can install this Heroku performance monitoring add-on to circumvent the issue.

Now, this might be a result of my limited node.js experience, so please excuse my potential ignorance, but every time I hit my app's page, my bot leaves the room and re-enters it. I think this is because it restarts the http server instance created by the node file. Is this just how node works or did I just make my bot in a really n00b way?

ARH3 avatar Jul 18 '13 18:07 ARH3

OpenShift's is 48 hours with inactivity. If you want, I can send a http request to your bot's webservice every 30 minutes @ARH3 from my web server if you'd like. This should keep it from going idle.

EDIT: @MikeWills brings up a good point. You could simply send a http request to yourself and possibly circumvent the idle. Not sure if they have detection in place for this though.

Izzmo avatar Jul 18 '13 19:07 Izzmo

That would work too... a simple wget call would work.

Mike Wills http://mikewills.me

MikeWills avatar Jul 18 '13 19:07 MikeWills

Thanks @MikeWills and @Izzmo I will try some of these ideas.

I still wonder though: every time I ping or wget my app, it is going to remove my bot from the room and then add him back in there. I think this is because every time the page is pinged, this code is run: var bot = new Bot(AUTH, USERID, ROOMID);

Any ideas for how to circumvent that so people in the TT room don't notice the disturbance?

ARH3 avatar Jul 18 '13 19:07 ARH3

Maybe it isn't calling the same script and calling a new version?

Mike Wills http://mikewills.me

MikeWills avatar Jul 18 '13 20:07 MikeWills

You need to add a HTTP web service to your file then @ARH3, so when someone requests the page, it will return the http response instead of reloading ur app.

Izzmo avatar Jul 18 '13 20:07 Izzmo

Thanks @Izzmo that is probably it. I hacked this bot together a while back and am only now revisiting it with a renewed interest in learning more about node. I will play around with some of these suggestions and see what I can do.

Here is the main file of my bot if anyone cares to look. It is one, big, ugly file :). I am also noticing that ALL my functions and code are within the "http.createServer" function, which I'm sure is awful practice. After looking at @MikeWills's bot I think I will try to mimic what I am seeing there and see what happens.

Thanks for all the help everyone!

ARH3 avatar Jul 18 '13 20:07 ARH3

Yeah, definitely don't put it all with the createServer function =P

Izzmo avatar Jul 18 '13 21:07 Izzmo

wow, that is the uglest code, i have ever seen, besides the code that I wrote for my turntable.fm bot.

ghost avatar Jul 19 '13 01:07 ghost

lol @Turntablelover no offense taken, that was my first foray into node EVER.

Below is the final solution that is working great for me and keeping my app from going idle. All you have to do is replace "myapp.herokuapp.com" with the subdomain for your own heroku-hosted app. Thanks for all the help everyone, and I hope others find this useful in the future! CODE:

var minutes = 20, the_interval = minutes * 60 * 1000;

setInterval(function() { var options = { host: 'myapp.herokuapp.com' }; http.get(options, function (http_res) { console.log("Sent http request to myapp.herokuapp.com to stay awake."); }); }, the_interval);

ARH3 avatar Jul 19 '13 13:07 ARH3

My first bot's code was ugly too... Then I rewrote from scratch trying to make it more organized.

This is what I was thinking. Glad to see it works.

MikeWills avatar Jul 19 '13 13:07 MikeWills

the thing i've found to solve this problem is to declare your process as type worker in your Procfile. the reason for this is that type web does not listen on any ports. then use bot.listen() and make sure your bot does something every once in a while. it never idles me and i didn't have to specifically send it messages or whatever, just use the api as normal

samuri51 avatar Aug 13 '13 21:08 samuri51

This information would be great to publish in the wiki so others can easily find it in the future.

MikeWills avatar Aug 14 '13 01:08 MikeWills

I agree! Nice find.

One thing I've noticed with the web type specified is that even if I attempt to keep it running with a periodic request, Heroku restarts the app every 24 hours. I'm curious, have you noticed the same thing with the worker type?

gizmotronic avatar Aug 24 '13 15:08 gizmotronic

Hi guys, I made a very simple tool that pings your Heroku app every 30 minutes so it will never fall asleep again.

romainbutteaud avatar Apr 17 '14 19:04 romainbutteaud

Please try CloudUp. It visits your apps periodically to keep them awake. It is free, and you can add as many apps as you want. It also activates apps on Google App Engine and Azure.

j-jiafei avatar May 22 '14 03:05 j-jiafei

@romainbutteaud , After adding heroku app to your tool is there any way remove or stop pings?

raviteja548 avatar Nov 20 '14 07:11 raviteja548

what @raviteja548 said! There's no option to stop it right now. And no contact info.

chesterl avatar Jan 04 '15 03:01 chesterl

@raviteja548 @chesterl, for now I can remove them manually. Also, see https://github.com/romainbutteaud/Kaffeine/issues/3 and https://github.com/romainbutteaud/Kaffeine/issues/4

romainbutteaud avatar Jan 04 '15 06:01 romainbutteaud

@romainbutteaud kaffeine is very impressive and does a nice job of keeping the dynos awake. Much appreciation.

varatep avatar Jan 10 '15 03:01 varatep

@romainbutteaud - Awesome little tool, kudos to you, my friend !

matijaabicic avatar Sep 10 '15 14:09 matijaabicic

Is there any use case out there where pinging our Heroku app every hour for a site that gets little traffic resulting in dyno hours being drastically diminished to the point where the developer had to stop scheduling an hourly ping?

ldco2016 avatar Oct 05 '17 02:10 ldco2016