Rails - ActionDispatch :: Http :: UploadedFile in background job

I use a similar idea, as in importing csv and excel Railscast , but since the standard code in this episode takes some time to process (uses ActiveRecord to create a new record for each line in the file). I get timeouts on Heroku and would like to move the import process to a background job.

I was unable to send the file variable (which is of type ActionDispatch :: Http :: UploadedFile), so instead I sent the separate variables file.original_filename and file.path

The job fails with an error file /var/folders/q3/xn0bp7yd2m56_4lbq0069jj80000gn/T/RackMultipart20150319-72431-1a4pnja.xlsx does not exist, which I believe is happening because the file was already deleted before the job started:

Downloaded files are temporary files whose lifespan is one request. When the object is complete, Ruby peels off the file, so there is no need to clean them with a separate maintenance task.

ActionDispatch :: Http :: UploadedFile

Can a file loaded with ActionDispatch :: Http :: UploadedFile not be used in background jobs?

I am using Rails 4.2, ActiveJob and Resque

+4
source share
1 answer

No, the downloaded file cannot be used in the background job. What you need to do is save the downloaded file to a more permanent place to handle the background job.

Your controller needs something like:

file_path_to_save_to = '/path/to/file'
File.write(file_path_to_save_to, params[:uploaded_file].read)
BackgroundJob.perform_later file_path_to_save_to
+5
source

All Articles