Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 758
Marklar Avatar asked Mar 19 '15 03:03

Marklar


People also ask

How do you implement a callback in rails?

Like other callbacks in Rails, you can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks: The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line.

Should I pick a queue backend or frontend for my rails job?

Picking your queuing backend becomes more of an operational concern, then. And you'll be able to switch between them without having to rewrite your jobs. Rails by default comes with an asynchronous queuing implementation that runs jobs with an in-process thread pool.

How do I run a queue in a Rails application?

Since jobs run in parallel to your Rails application, most queuing libraries require that you start a library-specific queuing service (in addition to starting your Rails app) for the job processing to work. Refer to library documentation for instructions on starting your queue backend. Most of the adapters support multiple queues.


1 Answers

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
like image 167
Rob Di Marco Avatar answered Dec 02 '22 15:12

Rob Di Marco