我有这个Sidekiq工人:
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
Run Code Online (Sandbox Code Playgroud)
我将散列传递给perform_async方法,但我认为转移到perform方法的参数与传递给它的类型不同perform_async.我试图用户logger.info并p调试我的问题但没有输出...
问题是作业已添加到电子邮件队列但从未得到处理.我甚至试图在perform方法中引发异常(在方法的第一行)但是没有输出任何...
我知道以下工作人员的工作原理:
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
Run Code Online (Sandbox Code Playgroud)
所以问题在于hash参数(选项).我做错了什么?
小智 24
传递给perform_async的参数必须由简单的JSON数据类型组成:string,integer,float,boolean,null,array和hash.Sidekiq客户端API使用JSON.dump将数据发送到Redis.Sidekiq服务器从Redis中提取JSON数据并使用JSON.load将数据转换回Ruby类型以传递给您的perform方法.不要传递符号或复杂的Ruby对象(如Date或Time!),因为这些对象无法在转储/加载往返中正常运行.
你可以在控制台上看到这个:
> 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
Run Code Online (Sandbox Code Playgroud)
看起来您使用符号作为options哈希的键.如果你切换到字符串键,它应该工作.