0

I have a function that create a backup of the app and download it in a zip file, with the datas in the databases, images, files, etc. For this i create a big temp file (using the Tempfile class) that send to browser with send_data, but when i delete it after send_data the download failed and its memory is not released.

send_file(zip_data.path, type: 'application/zip', filename: "#{model_name}.zip")

zip_data.unlink

Service class: http://pastebin.com/MskjP8d7

Controller method: http://pastebin.com/CV9Wr27h

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160

1 Answers1

0

It happens because by the time the unlink method is executed, the request hasn't been completely served yet and the server hasn't actually sent the file. send_file is actually handled by the web server.

You could either drop the unlink call. Ruby garbage collector will clean up Tempfiles once they are out of scope. Or, replace send_file with send_data and manually send the binary contents of your Zip file from within the controller.

danirod
  • 340
  • 4
  • 14