send_data 无法发送大文件 - 30 秒后,终止请求

send_data can't send large file - after 30 seconds, kills the request

我在 nginx/unicorn 上有一个 rails 应用程序,其中包含以下代码行:

    format.csv { send_data Test_Model.to_csv, stream: 'true', filename: "assets-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.csv" }

问题是 - 我的 to_csv 方法生成的文件偏大,有 2.4 MB - 我的应用程序通过 sql queries...那个长度不是我能改变的。

那么,90% 的时间我调用我的功能我得到:

也许 10 次中有 1 次,我确实得到了文件。但关键是......生成它并通过 send_data.

发送它会非常有用

我知道有人在谈论 send_file。但是,send_file 需要一个已生成文件的文件路径。在这种情况下,我想用 send_data 动态创建此数据。如果可能的话,我真的很想将这个文件从 send_data 中推出 - 即使我的应用程序需要超过 30 秒才能生成文件。

更新

所以我的应用程序有点 "times out" 并在 30 秒时咬住灰尘。听起来很像某处的超时设置。我想知道 - send_data 超时会在 unicorn conf 中设置,还是在通用 rails conf 中设置? 如何控制与 send_data 关联的超时?

Ngnix 将终止耗时过长的连接。长连接意味着您的应用程序可能无法处理其他请求。除了重写使用 ActiveJob 或其他后台生成的请求外,您还可以检查 nginx 确认参数 keepalive_timeout

独角兽有一个 timeout parameter

sets the timeout of worker processes to +seconds+. Workers handling the request/app.call/response cycle taking longer than this time period will be forcibly killed (via SIGKILL). This timeout is enforced by the master process itself and not subject to the scheduling limitations by the worker process. Due the low-complexity, low-overhead implementation, timeouts of l than 3.0 seconds can be considered inaccurate and unsafe.