Rails 4 - Delayed_Job for CSV Import
I'm building a Marketplace app in Rails 4 where sellers can list items for sale. I have a csv import function so sellers can bulk upload products. The import code worked fine on small files, but I ran into timeout issues with large files. So I want to use delayed_job to process these files in the background.
I have set delayed_job to the point where the job is in the queue (I can see the job in the delayed_job table). But when I run the job, I get a message that the file to import was not found. It looks for a file in a temporary folder that does not exist when the job is running.
How do I save (or not save) the file in a location where delayed_job can access it? And how do I tell delayed_job where the file is?
my list controller:
def import
Listing.import(params[:file], params[:user_id])
redirect_to seller_url, notice: "Products are being imported."
end
my ad model:
class Listing < ActiveRecord::Base
require 'csv'
require 'open-uri'
class << self
def importcsv(file_path)
CSV.foreach(file_path, headers: true, skip_blanks: true) do |row|
#some model processing
end
end
handle_asynchronously :importcsv
end
# My importer as a class method
def self.import(file, user_id)
Listing.importcsv file.path
end
end
Here's a view of the form:
<%= form_tag import_listings_path, multipart: true do %>
<%= file_field_tag :file %>
<%= hidden_field_tag :user_id, current_user.id %>
<%= submit_tag "Import CSV" %>
<% end %>
source to share
Presumably the file is a form upload. I think these files are only saved when the web request is run. My recommendation is to use FileUtils.copy to copy the file to some location that will exist when your work starts. So, you might not want to handle_synchronously importcsv, but copy the files instead, then call a private method on your model (which will be processed asynchronously) with the new file paths.
source to share