我尝试在我的应用中使用sidekiq,但是当我写这个简单的工作者时
class ParseWorker
include Sidekiq::Worker
def perform(instance)
instance.spideys << Spidey.create
end
end
我在这里使用这个工作人员
def create
@user_link = UserLink.new(user_link_params)
if @user_link.save
binding.pry
ParseWorker.perform_async(@user_link)
redirect_to results_user_links_path
end
end
返回错误
2015-04-09T13:14:56.757Z 11644 TID-ay1nc ERROR: Actor crashed!
NoMethodError: undefined method `spideys' for "#<UserLink:0x007f65f00d99b0>":String
/home/weare138/simple-parser/app/workers/parse_worker.rb:7:in `perform'
但为什么呢? @user_link不是字符串
怎么修复?
UPD
def perform(id)
user_link = UserLink.find(id)
user_link.spideys << Spidey.create
end
错误
2015-04-09T14:15:21.889Z 11644 TID-ay1nc ERROR: Actor crashed!
NoMethodError: undefined method `spideys' for 39:Fixnum
UPD2
class ParseWorker
include Sidekiq::Worker
require 'open-uri'
def perform(id)
user_link = UserLink.find(id)
user_link.spideys << Spidey.create
end
end
答案 0 :(得分:0)
Sidekiq使用Redis,因此当您向worker传递一个对象时,它会将其序列化为JSON。
来自Sidekiq文档:
这意味着您的worker的参数必须是简单的JSON数据类型 (数字,字符串,布尔值,数组,哈希)。复杂的Ruby对象(例如 日期,时间,ActiveRecord实例)将无法正确序列化。
相反,你应该做
def create
...
ParseWorker.perform_async(@user_link.id)
end
def perform(id)
UserLink.find(id).spideys << Spidey.create
end