2017-01-14 2 views
1

Ich habe eine Rails 4.2.1 App mit Unicorn als App-Server laufen. Ich muss den Benutzer mit der Möglichkeit zum Download von CSV-Daten. zu lange Zeit in Anspruch nehmen Ich versuche, die Daten zu streamen, aber wenn die Datei als Einhorn-Timeout und Unicorn wird dieser ProzessTimeout Einhorn, Schienen 4.2.1

Ist töten es eine Möglichkeit, dieses Problem Mein Strom Code zu lösen:

private 
def render_csv(data) 
    set_file_headers() 
    set_streaming_headers() 

    response.status = 200 
    self.response_body = csv_lines(data) 
    Rails.logger.debug("end") 
end 

def set_file_headers 
    file_name = "transactions.csv" 
    headers["Content-Type"] = "text/csv" 
    headers["Content-disposition"] = "attachment; filename=\"#{file_name}\"" 
end 

def set_streaming_headers 
    #nginx doc: Setting this to "no" will allow unbuffered responses suitable for Comet and HTTP streaming applications 
    headers['X-Accel-Buffering'] = 'no' 

    headers["Cache-Control"] ||= "no-cache" 
    headers.delete("Content-Length") 
end 

def csv_lines(data) 
    Enumerator.new do |y| 
    #ideally you'd validate the params, skipping here for brevity 
    data.find_each(batch_size: 2000) do |row| 
     y << "jhjj"+ "\n" 
    end 
    end 
end 

Antwort

0

Wenn Sie die Konfigurationsdatei verwenden. Ändern Sie die Zeitüberschreitung dort. Hier ist, wie ich es mache.

In config/unicorn.rb

root = "/home/deployer/apps/appname/current" 
working_directory root 
pid "#{root}/tmp/pids/unicorn.pid" 
stderr_path "#{root}/log/unicorn.log" 
stdout_path "#{root}/log/unicorn.log" 


listen "/tmp/unicorn.appname.sock" 
worker_processes 2 
timeout 60 #<<< if you need you can increase it. 

Dann würden Sie von

Einhorn starten
bundle exec unicorn -D -E production -c config/unicorn.rb