Why are RackMultipart* files persisting in my Rail

2019-03-15 09:58发布

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.

3条回答
Anthone
2楼-- · 2019-03-15 10:22

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
查看更多
闹够了就滚
3楼-- · 2019-03-15 10:28

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
查看更多
爷、活的狠高调
4楼-- · 2019-03-15 10:30

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.

查看更多
登录 后发表回答