0

In Rails, is it possible to send a response back to the browser in two parts? The first part doesn't need to be anything specific (reason explained below). The second part of the response would be the full normal response such as a view in .html.erb format.

The reason that I'm looking to do this is that Heroku requires a response to be sent back to the browser within 30 seconds, however it's taking longer than 30 seconds for my app to perform several dozen calculations on 1.2 million records.

The Heroku documentation indicates that this may be possible with some platforms (but is it possible with Rails?): https://devcenter.heroku.com/articles/limits

HTTP requests have an initial 30 second window in which the web process must return response data (either the completed response or some amount of response data to indicate that the process is active). Processes that do not send response data within the initial 30-second window will see an H12 error in their logs.

P.S.- I recognize that there may be better platforms than Rails to build this kind of data-intensive app, but I'm building it in Rails because that's what I know.

Thanks!

user1515295
  • 1,181
  • 11
  • 28
  • Seems like a limitation with Heroku. Have you looked into streaming? http://guides.rubyonrails.org/action_controller_overview.html#live-streaming-of-arbitrary-data – lsaffie Jun 22 '14 at 19:34

1 Answers1

1

The right way to do this, especially on Heroku, is with background jobs. If you don't want to pay for the extra dyno, there are some hacks out there that might work for you.

If you just need to hack something together, the streaming API should work. You should be able to write a blank space every 30 seconds until your work is complete. Keep in mind you'll need a way to interrupt your long running task to send the response. That probably means a Ruby version that supports threading.

If all you're doing is reading a lot of records from the DB, you can use find_each with streaming.

James Mason
  • 4,246
  • 1
  • 21
  • 26
  • Thanks for the pointer to the streaming API. That seems to be the way to go. – user1515295 Jun 23 '14 at 00:28
  • I've used background jobs on Heroku before, but never with any background jobs that render a view for the browser when the job is done. Is that possible? – user1515295 Jun 23 '14 at 00:29
  • Typically you'd use a repeating ajax request to check the status, then redirect when it's complete. This might help: http://stackoverflow.com/questions/5042300/indicate-to-an-ajax-process-that-the-delayed-job-has-completed – James Mason Jun 23 '14 at 01:31