Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the temporary files automatically in ruby-rails?

My Rails app has to process and generate PDF XFA files and send to the user/browser. Its working fine. But the issue is that before sending the file to the user, it creates 2 files in the rails tmp directory.

If 10 requests come to the pdf_controller, the number of the temp files in the tmp directory will double and it will eat up the space.

After searching around I thought that Sweeper will come to the rescue. But not much knowledge about Sweeper.

So, can anyone plz suggest which way to go?

like image 738
Autodidact Avatar asked Mar 30 '09 10:03

Autodidact


2 Answers

Tempfile will delete files when the object is finalized.

Tempfile on Rdoc

Example:

def get_pdf
  model = Model.find(params[:id])
  file = Tempfile.new
  model.to_pdf(file)
  send_file file.path, ...
end

I can provide a better example if you paste your code into your question.

like image 131
Scott Avatar answered Sep 17 '22 12:09

Scott


You could use a cron task, that deletes the files every n minutes, or, you could order the deletion from the controller itself.

like image 26
Maximiliano Guzman Avatar answered Sep 17 '22 12:09

Maximiliano Guzman