Rails 3.2 streaming
The edited question turned out to contain exactly the answer I needed. Posting it here as an answer.
The answer to getting the Rack handler to stream properly is apparently to add a Last-Modified
header to the response:
self.response.headers['Last-Modified'] = Time.now.ctime.to_s
Streaming CSV Download from Rails 3.2 app
OK, after a bit more research I hacked together the following in my controller. It'll stream if response_body
is given something enumeratable (is that a word?). Also, the server needs to be able to stream (I am using Unicorn on Heroku). I'd like very much to not have all this stuff in the controller, so my next step is to extract it out somehow.
format.csv {
@entries = Entry.all
@columns = ["First Name", "Last Name"].to_csv
@filename = "entries-#{Date.today.to_s(:db)}"
self.response.headers["Content-Type"] ||= 'text/csv'
self.response.headers["Content-Disposition"] = "attachment; filename=#{@filename}"
self.response.headers["Content-Transfer-Encoding"] = "binary"
self.response_body = Enumerator.new do |y|
@entries.each_with_index do |entry, i|
if i == 0
y << @columns
end
y << [entry.first_name, entry.last_name].to_csv
end
end
}
Ruby on Rails 3: Streaming data through Rails to client
It looks like this isn't available in Rails 3
https://rails.lighthouseapp.com/projects/8994/tickets/2546-render-text-proc
This appeared to work for me in my controller:
self.response_body = proc{ |response, output|
output.write "Hello world"
}
unexpected filter behavior in rails streaming controlle
It seems like, rather than relying on around_filter
, you might be able to invoke your data clean-up routine in the class's each
method after the iterator has been exhausted:
class Asdf
def each
(1..5).each do |num|
sleep(1)
Rails.logger.info "STREAMING LINE #{num}"
yield "test#{num}\n"
end
Rails.logger.info "ALL DONE; READY TO CLEAN UP"
clean_up
end
def clean_up
Rails.logger.info "CLEANING UP"
end
end
Hititng the welcome#index
action in a Rails 3.2 app yielded the following in the log:
Started GET "/welcome" for 127.0.0.1 at 2016-01-25 18:55:49 -0800
Processing by WelcomeController#index as HTML
before_filter called
around_filter prior to action
around_filter after action
after_filter called
Completed 200 OK in 0.5ms (ActiveRecord: 0.0ms)
STREAMING LINE 1
STREAMING LINE 2
STREAMING LINE 3
STREAMING LINE 4
STREAMING LINE 5
ALL DONE; READY TO CLEAN UP
CLEANING UP
Related Topics
How to Use Activesupport::Configurable with Rails Engine
Exception_Notification for Delayed_Job
Using Activemodel::Serializer in Rails - JSON Data Differs Between JSON and Index Response
Where to Put a Before_Filter Shared Between Multiple Controllers
Converting String "2½" (Two and a Half) into 2.5
Paperclip File Not Found Error
In Ruby, How to Find Out If a String Is Not in an Array
Ruby -V Dyld: Library Not Loaded: /Usr/Local/Lib/Libgmp.10.Dylib
Remove Double Quotes from String
Error "Undefinded Method "Load_Defaults" " When Trying to Deploy App on Heroku
How to Reference a Method in Another Ruby Code File
Heroku: How to Push to Specific App If You Have Multiple Apps in Heroku
Gem Which Cannot Find Gem Despite It Being Installed