开发者

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

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 li开发者_运维技巧ttle 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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜