【发布时间】:2017-02-05 20:24:09
【问题描述】:
我有一个 Rails 4 应用程序,它允许使用 jQuery Dropzone 插件和回形针 gem 上传视频。每个上传的视频都被编码为多种格式,并在后台使用 delay_paperclip、av-transcoder 和 sidekiq gems 上传到 Amazon S3。
大多数视频都可以正常工作,但是在上传到 dropzone 插件的进度条末尾时,它会返回 Nginx 504 网关超时。
就服务器而言,rails 应用程序在负载均衡器后面的几台服务器上的 Nginx + Passenger 上运行(这里也使用 Nginx)。我没有在负载平衡器的上游部分设置超时,client_max_body_size 设置为 2000M(在负载平衡器和服务器上),我尝试将passenger_pool_idle_time 设置为一个较大的值(600),即没有帮助,我也试过设置send_timeout(600s),没有任何区别。
注意:进行这些更改时,我在两台服务器以及负载均衡器的主机文件上进行了更改,之后总是重新启动 nginx。
我还阅读了几个关于this one 和this one 等类似问题的答案,但仍然无法弄清楚,谷歌也没有多大帮助。
对于不熟悉整个paperclip/delayed_paperclip过程的人来说一些额外的说明,文件上传到服务器然后操作就用户而言完成,在后台对视频进行后期处理(编码/上传到 S3)作为作业推送到 Redis,Sidekiq 会在有时间/资源时对其进行处理。
什么可能导致此问题?我该如何调试并解决它?
更新
感谢 Sergey 的回答,我能够解决这个问题。由于我被限制在特定版本的 Paperclip,我无法将其更新到具有修复程序的最新版本,因此我将把我最终做的事情留在这里。
在我用来处理上传的引擎中,我在 engine_name.rb 文件中添加了以下代码,以覆盖 Paperclip 中需要修复的方法:
Paperclip::AbstractAdapter.class_eval do
def copy_to_tempfile(src)
link_or_copy_file(src.path, destination.path)
destination
end
def link_or_copy_file(src, dest)
Paperclip.log("Trying to link #{src} to #{dest}")
FileUtils.ln(src, dest, force: true) # overwrite existing
@destination.close
@destination.open.binmode
rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT => e
Paperclip.log("Link failed with #{e.message}; copying link #{src} to #{dest}")
FileUtils.cp(src, dest)
end
end
Paperclip::AttachmentAdapter.class_eval do
def copy_to_tempfile(source)
if source.staged?
link_or_copy_file(source.staged_path(@style), destination.path)
else
source.copy_to_local_file(@style, destination.path)
end
destination
end
end
Paperclip::Storage::Filesystem.class_eval do
def flush_writes #:nodoc:
@queued_for_write.each do |style_name, file|
FileUtils.mkdir_p(File.dirname(path(style_name)))
begin
move_file(file.path, path(style_name))
rescue SystemCallError
File.open(path(style_name), "wb") do |new_file|
while chunk = file.read(16 * 1024)
new_file.write(chunk)
end
end
end
unless @options[:override_file_permissions] == false
resolved_chmod = (@options[:override_file_permissions] &~ 0111) || (0666 &~ File.umask)
FileUtils.chmod( resolved_chmod, path(style_name) )
end
file.rewind
end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {}
end
private
def move_file(src, dest)
# Support hardlinked files
if File.identical?(src, dest)
File.unlink(src)
else
FileUtils.mv(src, dest)
end
end
end
【问题讨论】:
-
只是想摆脱这个等式。服务器规格是什么?您是否尝试过增加服务器资源?我从经验中知道,即使是图像,回形针也是一个非常苛刻的过程。我最近在我的一个应用程序中添加了视频功能(games.directory GIF 到 MP4),由于在解码 GIF 时产生的负载回形针,我不得不进行缩放。我也在使用 nginx,但使用的是 Rails 5 和 Puma。
-
CPU:8 内存:8GB 硬盘:50GB
-
您设法解决了这个问题吗?我遇到了同样的问题,无法找出解决方案。
标签: ruby-on-rails nginx paperclip passenger delayed-paperclip