Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ARH3 opened this issue Jul 18, 2013 · 26 comments
Open

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

ARH3 opened this issue Jul 18, 2013 · 26 comments

Comments

@ARH3
Copy link

ARH3 commented Jul 18, 2013

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!

@gizmotronic
Copy link
Collaborator

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.

@MikeWills
Copy link
Contributor

Could the bot ping itself? Hit a "web page" every 50 minutes?

Mike Wills
mike@mikewills.me
http://mikewills.me
Ph: 507-933-0880

On Thu, Jul 18, 2013 at 12:34 PM, gizmo_tronic notifications@github.com
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:
#243 (comment)

@gizmotronic
Copy link
Collaborator

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.

@ARH3
Copy link
Author

ARH3 commented Jul 18, 2013

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
Copy link
Author

ARH3 commented Jul 18, 2013

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?

@Izzmo
Copy link
Collaborator

Izzmo commented Jul 18, 2013

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.

@MikeWills
Copy link
Contributor

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

Mike Wills
http://mikewills.me

@ARH3
Copy link
Author

ARH3 commented Jul 18, 2013

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?

@MikeWills
Copy link
Contributor

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

Mike Wills
http://mikewills.me

@Izzmo
Copy link
Collaborator

Izzmo commented Jul 18, 2013

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.

@ARH3
Copy link
Author

ARH3 commented Jul 18, 2013

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!

@Izzmo
Copy link
Collaborator

Izzmo commented Jul 18, 2013

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

@ghost
Copy link

ghost commented Jul 19, 2013

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

@ARH3
Copy link
Author

ARH3 commented Jul 19, 2013

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);

@MikeWills
Copy link
Contributor

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.

@samuri51
Copy link

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

@MikeWills
Copy link
Contributor

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

@gizmotronic
Copy link
Collaborator

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?

@romainbutteaud
Copy link

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

@j-jiafei
Copy link

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.

@raviteja548
Copy link

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

@chesterl
Copy link

chesterl commented Jan 4, 2015

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

@romainbutteaud
Copy link

@raviteja548 @chesterl, for now I can remove them manually.
Also, see romainbutteaud/Kaffeine#3 and romainbutteaud/Kaffeine#4

@varatep
Copy link

varatep commented Jan 10, 2015

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

@matijaabicic
Copy link

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

@ldco2016
Copy link

ldco2016 commented Oct 5, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests