Why are RackMultipart* files persisting in my Rails /tmp directory?

大兔子大兔子 提交于 2019-12-03 03:24:18

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

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
Wojtek Kruszewski

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!