Home > Blockchain >  Rails test triggers Sidekiq warning
Rails test triggers Sidekiq warning

Time:03-03

I have the following method in a Sidekiq worker:

  def self.schedule_edits(course:, editing_user:, enrollment_results:)
    puts editing_user.id
    perform_async(course.id, editing_user.id, enrollment_results)
  end

I have a controller test that, when it calls this code throws the following warning:

WARN: Job arguments to MassEnrollmentWorker do not serialize to JSON safely. This will raise an error...

I have read up on the warning HERE and I'm guessing that enrollment_results is the offending argument. However, when I run the test and output enrollment_results, here is what I see:

{"FirstUser"=>{:success=>"User added to course."}, "SecondUser"=>{:success=>"User added to course."}, "NotARealUserOnWikipedia"=>{:failure=>"Not an existing user."}

This appears to be a valid hash, so what is the issue?

CodePudding user response:

Note the part about Symbols.

https://github.com/mperham/sidekiq/wiki/Best-Practices#1-make-your-job-parameters-small-and-simple

The arguments you pass to perform_async must be composed of simple JSON datatypes: string, integer, float, boolean, null(nil), array and hash. This means you must not use ruby symbols as arguments.

CodePudding user response:

For posterity; I fixed this by changing way the hash was build from:

{"FirstUser"=>{:success=>"User added to course."}, "SecondUser"=>{:success=>"User added to course."}, "NotARealUserOnWikipedia"=>{:failure=>"Not an existing user."}

to...

{"FirstUser"=>{"success"=>"User added to course."}, "SecondUser"=>{"success"=>"User added to course."}, "NotARealUserOnWikipedia"=>{"failure"=>"Not an existing user."}
  • Related