gpt4 book ai didi

ruby - Sidekiq 如何将参数传递给 perform 方法?

转载 作者:数据小太阳 更新时间:2023-10-29 06:38:56 26 4
gpt4 key购买 nike

我有这个 Sidekiq worker:

class DealNoteWorker
include Sidekiq::Worker
sidekiq_options queue: :email

def perform(options = {})
if options[:type] == "deal_watch_mailer"
deal_watchers = DealWatcher.where("deal_id = ?", options[:deal_id])

deal_note = DealNote.find(options[:deal_note_id])

current_user = User.find(options[:current_user_id])

deal_watchers.each do |deal_watcher|
unless deal_watcher.user_id == options[:current_user_id]
# Tell the DealWatchMailer to send emails to ALL watchers advising of change to deal
if deal_watcher.user.active
DealWatchMailer.deal_watch_email(deal_watcher, nil, deal_note, current_user, options[:url]).deliver
end
end
end
elsif options[:type] == "deal_note_mailer"
options[:user_ids].each do |id|
if DealWatcher.where("deal_id = ? and user_id =?", options[:deal_id], id).count == 0
deal_note = Deal.find(options[:deal_note_id])
user = User.find_by_id(id)
DealNoteMailer.deal_note_email(deal_note, user, options[:url]).deliver
end
end
end
end
end

我将哈希传递给 perform_async 方法,但我认为传递给 perform 方法的参数与传递给 的参数类型不同>执行异步。我尝试使用 logger.infop 来调试我的问题,但没有任何输出...

问题是作业已添加到电子邮件队列但从未得到处理。我什至尝试在 perform 方法(在方法的第一行)中引发异常,但也没有输出任何内容......

我知道以下 worker 在工作:

class DealNoteWorker
include Sidekiq::Worker

def perform(deal_id, deal_note_id, current_user_id, url)
deal_watchers = DealWatcher.where("deal_id = ?", deal_id)

deal_note = DealNote.find(deal_note_id)

current_user = User.find(current_user_id)

deal_watchers.each do |deal_watcher|
unless deal_watcher.user_id == current_user_id
# Tell the DealWatchMailer to send emails to ALL watchers advising of change to deal
if deal_watcher.user.active
DealWatchMailer.deal_watch_email(deal_watcher, nil, deal_note, current_user, url).deliver
end
end
end
end
end

所以问题出在hash参数(options)上。请问我做错了什么?

最佳答案

来自Sidekiq documentation :

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 or complex Ruby objects (like Date or Time!) as those will not survive the dump/load round trip correctly.

你可以在控制台上看到这个:

> options = { :a => 'b' }
> how_sidekiq_stores_the_options = JSON.dump(options)
> how_sidekiq_loads_the_options = JSON.load(how_sidekiq_stores_the_options)
> how_sidekiq_stores_the_options == how_sidekiq_loads_the_options
false

看起来您正在使用符号作为您的 options 散列的键。如果您切换到字符串键,它应该可以工作。

关于ruby - Sidekiq 如何将参数传递给 perform 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792207/

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