Ruby on rails 超时独角兽,rails 4.2.1
我有一个rails 4.2.1应用程序,以Unicorn作为应用程序服务器运行。 我需要为用户提供下载csv数据的能力。 我正在尝试流式传输数据,但当文件花费的时间超过Unicorn超时时间时,Unicorn将终止此进程 有什么办法解决这个问题吗 我的流代码:Ruby on rails 超时独角兽,rails 4.2.1,ruby-on-rails,Ruby On Rails,我有一个rails 4.2.1应用程序,以Unicorn作为应用程序服务器运行。 我需要为用户提供下载csv数据的能力。 我正在尝试流式传输数据,但当文件花费的时间超过Unicorn超时时间时,Unicorn将终止此进程 有什么办法解决这个问题吗 我的流代码: private def render_csv(data) set_file_headers() set_streaming_headers() response.status = 200 self.response_bo
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
private
def render_csv(数据)
设置文件头()
设置\u流\u头()
响应状态=200
self.response\u body=csv\u行(数据)
Rails.logger.debug(“结束”)
结束
def set_文件头
文件\u name=“transactions.csv”
标题[“内容类型”]=“文本/csv”
标题[“内容处置”]=“附件;文件名=\”{file\u name}”
结束
def set_数据流_标题
#nginx doc:将此设置为“否”将允许适用于Comet和HTTP流应用程序的无缓冲响应
标题['X-Accel-Buffering']=“否”
标题[“缓存控制”]| |=“无缓存”
标题。删除(“内容长度”)
结束
def csv_行(数据)
Enumerator.new do|y|
#理想情况下,您应该验证参数,为了简洁起见,跳过这里
数据。查找每个(批次大小:2000)do行|
y如果使用配置文件。在那里更改超时。我是这样做的
在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.
如果您使用配置文件。在那里更改超时。我是这样做的
在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.