Search code examples
ruby-on-railsrubyfile-uploadhashsidekiq

Sidekiq UploadedFile becomes Hash


What I am trying to do is use Sideqik to read some data from a file in the background. My problem is, when I pass the UploadedFile from my controller to the worker, it goes from being an UploadedFile to a Hash.

In my controller's create method:

file = params[file_key]                                                   
if not (file.nil? or file.class == ActionDispatch::Http::UploadedFile)    
  raise "File not a file in Controller!"                                  
elsif not file.nil?                                                       
  ImporterWorker.perform_async(name, file)                                
end

The exception is not raised, and we merrily go on to ImporterWorker, which has:

def perform(name, file)                                                                                                    
  if not file.class == ActionDispatch::Http::UploadedFile                     
    raise "File is: #{ file.class }:#{ file.inspect }"             
  end
  ...

Where the exception IS raised, with "File is: Hash:" followed by a huge string of arbitrary things I imagine is the representation of the file contents, followed by "'original_filename' => 'Foo.xls'", and other such fields.

Why is it becoming a hash? What can I do to fix this?


Solution

  • Per the sidekiq documentation :

    There's three ways to create a job in your application code ... All three ways create a Hash which represents the job, serializes that Hash to a JSON string and pushes that String into a queue in Redis. This means the arguments to your worker must be simple JSON datatypes (numbers, strings, boolean, array, hash). Complex Ruby objects (e.g. Date, Time, ActiveRecord instances) will not serialize properly.

    So you need to store the file more permanently, and then pass a file path to the job for further processing