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

余生长醉 提交于 2019-12-23 07:47:57

问题


I'm using a similar idea as in the importing csv and excel Railscast but as the standard code in that episode takes some time to process (uses ActiveRecord to create a new record for each row in the file) I'm getting timeouts on Heroku and would like to move the import process to a background job.

I have been unsuccessful at sending the file variable (which is of type ActionDispatch::Http::UploadedFile) to the job so instead I sent individual variables of the file.original_filename and file.path

The job fails with the error file /var/folders/q3/xn0bp7yd2m56_4lbq0069jj80000gn/T/RackMultipart20150319-72431-1a4pnja.xlsx does not exist which I assume is happening because the file has already been deleted before the job begins as:

Uploaded files are temporary files whose lifespan is one request. When the object is finalized Ruby unlinks the file, so there is no need to clean them with a separate maintenance task.

ActionDispatch::Http::UploadedFile

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

I am using Rails 4.2, ActiveJob and Resque


回答1:


No, the uploaded file cannot be used in the background job. What you need to do is save the uploaded file to a more permanent location for your background job to process.

Your controller will need to 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


来源:https://stackoverflow.com/questions/29136542/rails-actiondispatchhttpuploadedfile-in-background-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!