尝试使用delayed_job对作业进行排队,如下所示:
Delayed::Job.enqueue(BackgroundProcess.new(current_user, object))
当我打印出来时,current_user和object不是nil。奇怪的是,有时刷新页面或再次运行命令有效!
以下是异常跟踪:
Delayed::Backend::ActiveRecord::Job Columns (44.8ms) SHOW FIELDS FROM `delayed_jobs`
TypeError (wrong argument type nil (expected Data)):
/Users/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/yaml.rb:391:in `emit'
/Users/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/yaml.rb:391:in `quick_emit'
/Users/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/yaml/rubytypes.rb:86:in `to_yaml'
vendor/plugins/delayed_job/lib/delayed/backend/base.rb:65:in `payload_object='
activerecord (2.3.9) lib/active_record/base.rb:2918:in `block in assign_attributes'
activerecord (2.3.9) lib/active_record/base.rb:2914:in `each'
activerecord (2.3.9) lib/active_record/base.rb:2914:in `assign_attributes'
activerecord (2.3.9) lib/active_record/base.rb:2787:in `attributes='
activerecord (2.3.9) lib/active_record/base.rb:2477:in `initialize'
activerecord (2.3.9) lib/active_record/base.rb:725:in `new'
activerecord (2.3.9) lib/active_record/base.rb:725:in `create'
vendor/plugins/delayed_job/lib/delayed/backend/base.rb:21:in `enqueue'
答案 0 :(得分:7)
我猜这是因为您将对象作为参数发送到您的作业(至少我认为current_user和对象实际上是对象而不是id)。改为发送id,然后在开始执行时加载对象。
例如:
Delayed::Job.enqueue(BackgroundProcess.new(current_user.id, object.id))
class BackgroundProcess < Struct.new(:user_id, :object_id)
def perform
@current_user = User.find(user_id)
@object = Object.find(object_id)
...
end
end
这样,将ActiveRecord序列化到数据库中不会有任何问题,并且您将始终在作业运行时加载最新的更改。
答案 1 :(得分:0)
也遇到同样的问题。我仍然不知道是什么导致它,但由于某种原因克隆对象似乎解决了它
u = User.find 123
u.to_yaml
=> TypeError: wrong argument type nil (expected Data)
u.clone.to_yaml
=> works like normal
非常令人沮丧。最好知道根本原因,但如果你绝望,这可能会有所帮助。