最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I wake a Heroku app with a ping from JS? - Stack Overflow

programmeradmin1浏览0评论

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)

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)

Share Improve this question edited Aug 29, 2013 at 18:35 igneosaur asked Aug 29, 2013 at 18:28 igneosaurigneosaur 3,3463 gold badges31 silver badges44 bronze badges 1
  • Have you seen this: stackoverflow./questions/5480337/…? – Edward Ruchevits Commented Aug 29, 2013 at 18:39
Add a ment  | 

3 Answers 3

Reset to default 8

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

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


A mon 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.

Set up Scheduler:

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

That last mand 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

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 remend curl, because it's already installed in most linux hosts. This would do the same as what Edward remended, but it wouldn't require using outside systems.

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

then add this as a cronjob

发布评论

评论列表(0)

  1. 暂无评论