【问题标题】:Timeout unicorn, rails 4.2.1超时独角兽,rails 4.2.1
【发布时间】:2017-01-15 04:08:53
【问题描述】:

我有一个使用 Unicorn 作为应用服务器的 rails 4.2.1 应用。 我需要为用户提供下载 csv 数据的能力。 我正在尝试流式传输数据,但是当文件花费的时间超过 Unicorn 超时并且 Unicorn 将终止此进程时

有什么办法可以解决这个问题 我的流代码:

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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    如果你使用配置文件。在那里更改超时。这是我的做法。

    在 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.
    

    然后你会开始独角兽

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

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 2012-05-27
      • 2014-08-04
      • 2012-09-11
      • 2020-10-31
      • 2013-04-12
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      相关资源
      最近更新 更多