【问题标题】:Rails test triggers Sidekiq warningRails 测试触发 Sidekiq 警告
【发布时间】:2022-07-15 01:30:52
【问题描述】:

我在 Sidekiq 工作人员中有以下方法:

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

我有一个控制器测试,当它调用此代码时会引发以下警告:

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

我已经阅读了警告HERE,我猜注册结果是有问题的论点。但是,当我运行测试并输出enrollment_results 时,我看到的是:

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

这似乎是一个有效的哈希,那么问题是什么?

【问题讨论】:

    标签: ruby-on-rails sidekiq


    【解决方案1】:

    注意符号部分。

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

    您传递给 perform_async 的参数必须由简单的 JSON 数据类型组成:字符串、整数、浮点数、布尔值、null(nil)、数组和哈希。这意味着您不能使用 ruby​​ 符号作为参数。

    【讨论】:

    • 我正在阅读这篇文章,因为您不应该像 SomeWorker.perform_async(:quote_id) 那样将符号传递给 perform_async。你是说像:success 这样的嵌套符号是什么原因造成的?
    • JSON 不知道什么是符号。不管你嵌套多深。
    【解决方案2】:

    为了后代;我通过改变哈希的构建方式来解决这个问题:

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

    到...

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

    【讨论】:

      【解决方案3】:

      就我而言,我只需要更改我的 hashmap:

       Sidekiq::Client.push('class' => 'RecordOrderJob', 'args' => order_opts)
      

      到:

      Sidekiq::Client.push('class' => 'RecordOrderJob', 'args' => order_opts.to_json)
      

      【讨论】:

        猜你喜欢
        • 2017-03-06
        • 1970-01-01
        • 2020-12-25
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多