为什么 RackMultipart* 文件保留在我的 Rails /tmp 目录中?

发布于 2024-10-10 08:30:54 字数 588 浏览 3 评论 0原文

我正在使用 Paperclip (2.3) 来处理在 Ubuntu 上运行的 Rails 3.0.3 应用程序上的图像上传。 Paperclip 正在按广告处理上传,但在应用程序的 /tmp 文件夹中创建的 RackMultipart* 文件仍然存在 - 也就是说,它们只是累积而不是删除自身。我意识到我可以使用 tmpreaper 删除旧的 tmpfiles,但我真的很想找到一个更优雅(且可扩展)的解决方案。

我之前遇到过临时文件(即 RackMultipart* 文件)累积在 Rails 应用程序根目录(而不是 /tmp)中的问题。我通过在environment.rb文件中显式设置临时路径解决了这个问题,如下所示:

ENV['TMPDIR'] = Rails.root.join('tmp')

是否需要设置另一个环境变量以确保临时文件得到正确处理——即在模型中保存后将其删除?我不确定这是 Paperclip 还是我的 Rails 设置的问题。

我四处搜寻,但在这方面进展甚微。我将不胜感激任何线索。

真诚的感谢。

PS - 我目前正在使用 S3 进行存储。但这似乎与问题无关——当我在本地存储文件时遇到了同样的问题。

I'm using Paperclip (2.3) to handle image uploads on a Rails 3.0.3 app running on Ubuntu. Paperclip is handling the uploads as advertised BUT the RackMultipart* files that are created in the application's /tmp folder persist -- that is, they simply accumulate rather than deleting themselves. I realize that I could use tmpreaper to delete old tmpfiles but I'd really like to find a more elegant (and scalable) solution.

I had a previous issue with temp files (i.e. RackMultipart* files) accumulating in the Rails app's root directory (instead of in /tmp). I resolved this by explicitly setting the temp path in my environment.rb file like so:

ENV['TMPDIR'] = Rails.root.join('tmp')

Is there another environment variable that needs to be set to make sure that the tempfiles are handled properly -- i.e. deleted once they've been saved in the model? I'm not sure if this is a problem with Paperclip or my Rails setup.

I've searched high and low but have made little progress on this. I'd be grateful for any leads.

Sincere thanks.

PS - I'm using currently using S3 for storage. This doesn't seem to be tied to the problem though -- I had the same problem when I was storing the files locally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森罗 2024-10-17 08:30:54

TempFileReaper 是用于处理此问题的 Rack 中间件。

http://www.rubydoc.info/github/rack/rack/Rack/TempfileReaper

在 application.rb 中包含这一行可以解决问题:

config.middleware.use Rack::TempfileReaper

The TempFileReaper is the Rack middleware thought to handle this issue.

http://www.rubydoc.info/github/rack/rack/Rack/TempfileReaper

Including this line in the application.rb solves the problem:

config.middleware.use Rack::TempfileReaper
胡大本事 2024-10-17 08:30:54

我不知道这是否更加优雅,但这就是我在保存文件后所做的”

tempfile = params[:file].tempfile.path
if File::exists?(tempfile)
  File::delete(tempfile)
end

I don't know if this is anymore elegant but this is what I am doing after the file is saved"

tempfile = params[:file].tempfile.path
if File::exists?(tempfile)
  File::delete(tempfile)
end
网白 2024-10-17 08:30:54

更新:问题应该在rack-1.6.0.beta2 中得到解决。我看到它已经在 Rails 4.2.0.rc2 中使用。

下面的解决方法为我服务了近一年:

我在接受文件上传的控制器操作末尾添加了这个:

Thread.new { GC.start }

这会触发未使用的 Rack::Request 对象的垃圾收集,同时也会删除关联的临时文件。请注意,它不会清除当前请求的临时文件,但会删除以前的文件,并防止它们累积。

UPDATE: Problem should be resolved in rack-1.6.0.beta2. I see it's already being used in Rails 4.2.0.rc2.

Below workaround served me well for almost a year:

I've added this at the end of controller action that accepts file uploads:

Thread.new { GC.start }

This triggers Garbage Collection of unused Rack::Request objects which also deletes associated temp files. Note it doesn't sweep temp file of current request, but it does remove previous files, and prevents them from accumulating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文