加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 将选项哈希传递给sidekiq worker

发布时间:2020-12-17 04:26:37 所属栏目:百科 来源:网络整理
导读:我记得当我尝试将params( JSON)选项传递给sidekiq工作方法时它并没有很好用,因为我引用了以下选项: options[:email_address] 但我认为如果我这样做会有效: options["email_address"] 因此,出于某种原因,当它被序列化和反序列化时,哈希只能用字符串而不是符
我记得当我尝试将params( JSON)选项传递给sidekiq工作方法时它并没有很好用,因为我引用了以下选项:
options[:email_address]

但我认为如果我这样做会有效:

options["email_address"]

因此,出于某种原因,当它被序列化和反序列化时,哈希只能用字符串而不是符号来引用.

这是安全的做法吗?

我有一个交易电子邮件工作者,看起来像:

class TransactionEmailWorker
    include Sidekiq::Worker

  def perform(action,options)
    case action
    when 'welcome'
      welcome(options["email_address"],options["something_else"])
    when 'reset_password'
      reset_password(options["email_address"])
    end
  end

  def welcome(email,title)
    # ...
  end

  def reset_password(email)
   # ..
  end

解决方法

这就是Sidekiq wiki page所说的:

The arguments you pass to perform_async must be composed of simple JSON datatypes: string,integer,float,boolean,null,array and hash. The Sidekiq client API uses JSON.dump to send the data to Redis. The Sidekiq server pulls that JSON data from Redis and uses JSON.load to convert the data back into Ruby types to pass to your perform method. Don’t pass symbols,named parameters or complex Ruby objects (like Date or Time!) as those will not survive the dump/load round trip correctly.

传递Ruby哈希是有效的,因为它能够序列化为有效的JSON.所以回答你的问题,这是安全的做法.

serialized_args = JSON.dump({email_address: "someemailaddress@gmail.com",something_else: "thing"})
=> "{"email_address":"someemailaddress@gmail.com","something_else":"thing"}"

JSON.load(serialized_args)
=> {"email_address"=>"someemailaddress@gmail.com","something_else"=>"thing"}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读