为什么 RackMultipart* 文件保留在我的 Rails /tmp 目录中?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TempFileReaper 是用于处理此问题的 Rack 中间件。
http://www.rubydoc.info/github/rack/rack/Rack/TempfileReaper
在 application.rb 中包含这一行可以解决问题:
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:
我不知道这是否更加优雅,但这就是我在保存文件后所做的”
I don't know if this is anymore elegant but this is what I am doing after the file is saved"
更新:问题应该在rack-1.6.0.beta2 中得到解决。我看到它已经在 Rails 4.2.0.rc2 中使用。
下面的解决方法为我服务了近一年:
我在接受文件上传的控制器操作末尾添加了这个:
这会触发未使用的 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:
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.