Last active
November 5, 2021 02:28
-
-
Save njakobsen/6257887 to your computer and use it in GitHub Desktop.
Live stream a database dump (or any other STDOUT) using Rails 4. Why would you want this? If you have a large database dump and want to avoid storing it in memory as Rails streams it. This allows pipe the dump directly into the http response instead of storing it as a file, sending it, and then deleting it. Let me know what you think! I've teste…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DatabaseController < ApplicationController | |
def database_dump | |
database = Rails.configuration.database_configuration[Rails.env]["database"] | |
send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup") | |
pipe = IO.popen("pg_dump '#{database}' -F c") | |
stream = response.stream | |
while (line = pipe.gets) | |
stream.write line | |
sleep 0.0001 # HACK: Prevent server instance from sleeping forever if client disconnects during download | |
end | |
rescue IOError | |
# Client Disconnected | |
ensure | |
pipe.close | |
response.stream.close | |
end | |
# Code that allows us to only mix in the live methods if we're accessing the desired action | |
def dispatch(name, *args) | |
extend ActionController::Live if name.to_s == 'database_dump' | |
super | |
end | |
end |
@njakobsen btw, you probably don't want to use gets
here: I imagine read(n)
will be much more efficient. Most of the "lines" you see are going to be pretty tiny.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I booted it up again in Ruby 2.1, Rails 4.0.2, and using Puma 2.8.2 and Passenger 4.0.42.
I trigger the streaming download, and then after a few seconds, cancel it in the browser download window.
Puma
No sleep 0.0001
Downloads are streaming but cancelling one causes the server to stop responding.
Using sleep 0.0001
Downloads are streaming but queued, one will not start before the previous one is cancelled.
Random number of downloads (usually between 2 and 8) can be cancelled before the server stops responding.
ctrl + c the server consistently causes the next-response-in-line to return the following error.
Thread.pass
Downloads are streaming but queued, one will not start before the previous one is cancelled.
Doesn't seem to lock up the server at all.
All requests reach response.stream.close.
Puma worker threads
puma -w
No effect on above behaviour
Passenger
No sleep 0.0001
Downloads are streaming and download in parallel.
Random number of downloads (usually between 2 and 8) can be cancelled before the server stops responding.
Cancelled requests don't seem to reach response.stream.close.
Using sleep 0.0001
Downloads are streaming and download in parallel.
Doesn't seem to lock up the server at all.
Thread.pass
Downloads are streaming and download in parallel.
Doesn't seem to lock up the server at all.
All requests reach response.stream.close.