views:

826

answers:

3

Hi,

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?

+1  A: 

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

Maximiliano Guzman
The cron might not be the good solution. When the user is shown with the tempfile created PDF file and if he doesn't submit the PDF form for sometime and if that cron gets fired and delete the temp file, and later when the user submits the PDF form, the system gets loose/invalid.
Millisami
+1  A: 

Tempfile will delete files when the object is finalized.

http://www.ruby-doc.org/core/classes/Tempfile.html

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.

Scott
Hi Scott!I think that the Tempfile might be the solution. But I'm still confused on how to use this to do task that I've mentioned above.An example will be much better.Thanks
Millisami
A: 

Can someone provide a better example please. I don't want to create millions of temporary files that don't get destroyed.

Thanks

Thomas