gpt4 book ai didi

ruby-on-rails - 将选项哈希传递给 sidekiq 工作器

转载 作者:行者123 更新时间:2023-12-04 00:34:30 24 4
gpt4 key购买 nike

我记得当我尝试将 params (JSON) 选项传递给 sidekiq 工作方法时,效果不佳,因为我引用了如下选项:

options[:email_address]

但我认为如果我这样做的话它会奏效:
options["email_address"]

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

这是一种安全的做法吗?

我有一个交易电子邮件 worker ,看起来像:
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"}

关于ruby-on-rails - 将选项哈希传递给 sidekiq 工作器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39682223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com