Reputation: 4003
I am new to Heroku I have an application which sends request to an external resource and get response this response can take more than 30 seconds may be 1 to 3 minutes. And I got error H-12 I can not use delay_job because my next step is totally dependent on the data of external resource. Please any one can help me what should I do to solve this problem. Thanks in advance
Upvotes: 0
Views: 262
Reputation: 11668
building upon @johns answer, heroku's cedar stack provides more options other than it's default web
.
You'll want to use delayed_job or my personal favourite sidekiq to run your jobs.
Start by creating a file called Procfile
in the root directory of your project
web: bundle exec rails server -p $PORT
worker: bundle exec sidekiq -c 15
Documentation for the Procfile is available on heroku.
Add sidekiq to your gemfile and run bundle install
You'll need a redis server running like redistogo and you should install redis on your development environment too.
Now create a workers
directory in your app
directory and create a worker class like so:
class MyWorker
include Sidekiq::Worker
def perform
# insert slow task here
end
end
When you want to perform the slow task call, say in your controller call
MyWorker.perform_async()
It can take arguments too like so
def perform(name)
MyModel.doSomethingSlow(name)
end
and then
MyWorker.perform_async("foobar")
Keep in mind this is asynchronous, the method will run in the background and unless you have something in your code to check the result is finished you won't know.
So see this all in action on development, make sure you have foreman installed (installed by default with the heroku toolbelt) and call foreman start
This will start both your web and worker on your local machine.
To get this running on heroku you just need to scale up the worker. The heroku console will give you an option to do this or you can use the command line heroku ps:scale worker=1
Upvotes: 1
Reputation: 37507
Your only option is to speed up the retrieval of your external data. The maximum time a web request has to respond is 30 seconds before it is killed by heroku and an h12 returned.
1-2 minute requests are simply not possible on Heroku. You only option would be to cache the data if possible after retrieving it periodically. Alternatively, a delayed_job style approach but you then poll the job until it completes via Ajax and when the data is available at the server prompt the user to continue.
Upvotes: 2