0

Here's my Controller

class CsvController < ApplicationController
  def read_csv
    CsvWorker.perform_async(params[:file])
  end

end

Here's my Worker

class CsvWorker
  include Sidekiq::Worker

  def perform(file)
    csv_file = SmarterCSV.process(file)
  end
end

I didn't get complete file object in CsvWorker, I only get this file object as string

"#<ActionDispatch::Http::UploadedFile:0x00007fa6b87ea0c0>"

I want to access this file in CsvWorker so i can read and manage the CSV file in the background.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • `ActionDispatch::Http::UploadedFile` is a simple wrapper around a tempfile which is automatically unlinked when the object is garbage collected, to make it permanent so that you can pass it to Sidekiq you need to copy the contents to a new file (recommended) or monkey with the finalizer. https://stackoverflow.com/questions/21285419/is-there-any-way-to-make-a-ruby-temporary-file-permanent – max Jan 11 '23 at 00:27

1 Answers1

1

The ActionDispatch::Http::UploadedFile is a stream so it only can be read during the request time.

Another thing is that sidekiq recommends you to post simple types and small data into a message job. So I recommend you to save the file into a storage, such as ActiveStorage, and then post the ID of that resource or the path of the saved file to a sidekiq worker like:

class CsvFile < ApplicationModel
    has_one_attached :file
end

class CsvController < ApplicationController
  def read_csv
    csv = CsvFile.create(params)
    CsvWorker.perform_async(csv.id)
  end
end

class CsvWorker
  include Sidekiq::Worker

  def perform(csv_file_id)
    csv = CsvFile.find(csv_file_id)
    csv_file = SmarterCSV.process(csv.file.read)
  end
end

With this approach you can give a good feedback to the final user, by adding a status column telling the final user, that is received, then processing, processed and error

manuwell
  • 402
  • 2
  • 8
  • 1
    ActionDispatch::Http::UploadedFile is not a stream. Its a thin wrapper around a tempfile instance which is automatically unlinked when Ruby garbage collects the object. https://api.rubyonrails.org/v6.0.2.2/classes/ActionDispatch/Http/UploadedFile.html – max Jan 11 '23 at 00:21
  • yeah, you are right. I always thought it was a stream due to the `read`, `rewind` and `eof?` instance methods. That's right – manuwell Jan 11 '23 at 19:57