2

I'm running several free, single-dyno apps on Heroku that go to "sleep" when idle for some time. I have a master site that is always awake and links/utilises these other apps that sometimes go to sleep. When I link to the sleeping apps from the main site there is that long loading wait whilst the other app wakes up.

What I want to do is wake all of the potentially sleeping apps the moment a user lands on the main page, sort of like pre-loading them, so it's quick to respond when the user needs it.

I am currently using a simple $.get(...) in the background but of course the console throws the 'Access-Control-Allow-Origin' error. I don't need any data or anything, just some sort of response to indicate the app is up and running. Anyone know how I can do this with an AJAX call without errors?

(Or if there's any other way, like through the API or something)

igneosaur
  • 3,268
  • 3
  • 29
  • 44

3 Answers3

8

Here is possible solution: https://coderwall.com/p/u0x3nw

As answers containing only a link are not welcome here, I'll just duplicate link contents.


A common way to work around Heroku's idling policy is to set up a script to send a ping once an hour to keep the dyno alive.

You can use the following to add New Relic's free plan to your account.

$ heroku addons:add newrelic:standard

Open the New Relic interface:

$ heroku addons:open newrelic

Under Menu, inside the Reports section, find Availability.

Add your URL, set a ping time of < 1 hour, and you're all set to go.

Using Scheduler

Alternatively, if you don't like or want to use New Relic, you can actually set up a keep-alive dyno ping through Heroku itself, using the Heroku Scheduler.

For instance, if you're using Ruby, you could use a Rake task like:

desc "Pings PING_URL to keep a dyno alive"
task :dyno_ping do
  require "net/http"

  if ENV['PING_URL']
    uri = URI(ENV['PING_URL'])
    Net::HTTP.get_response(uri)
  end
end

Add PING_URL to your Heroku environment:

$ heroku config:add PING_URL=http://my-app.herokuapp.com

Set up Scheduler:

$ heroku addons:add scheduler:standard
$ heroku addons:open scheduler

That last command should open the Scheduler interface in your browser. You can now set up your dyno_ping task to run once an hour:

$ rake dyno_ping

(c) Original blog post by Aupajo

Community
  • 1
  • 1
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
1

The error is because heroku is on a different domain, right? If so, one option is to send a request from your server to Heroku. Even better would be to setup an automated task via cron to regularly poll the server. I recommend curl, because it's already installed in most linux hosts. This would do the same as what Edward recommended, but it wouldn't require using outside systems.

Nikhil
  • 1,121
  • 2
  • 11
  • 27
0

just do curl -I <name of project>.herokuapp.com/<some file like index.html or sometext.txt>

then add this as a cronjob

Pixon
  • 1
  • Hello and welcome to SO. Your answer does not take into account the initial intent of keeping the dyno alive only when there is traffic. Please review our guidelines for [good answers](https://stackoverflow.com/help/how-to-answer). – ferrix Jul 07 '20 at 17:12