简体   繁体   English

超时独角兽,铁轨4.2.1

[英]Timeout unicorn, rails 4.2.1

I have a rails 4.2.1 app running with Unicorn as app server. 我有一个使用Unicorn作为app服务器运行的rails 4.2.1 app。 I need to provide the user with the ability to download csv data. 我需要为用户提供下载csv数据的能力。 I'm trying to stream the data, but when the file take too long time than Unicorn timeout and Unicorn will kill this process 我正在尝试流式传输数据,但是当文件花费的时间比Unicorn超时太长时,Unicorn会杀死这个进程

Is there any way to solve this problem My stream code : 有没有办法解决这个问题我的流代码:

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

If you use configuration file. 如果使用配置文件。 Change timeout there. 在那里更改超时。 Here is how I do it. 我就是这样做的。

In config/unicorn.rb 在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.

Then you would start unicorn by 然后你会开始独角兽

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM