Weird exception with delayed_job

 ̄綄美尐妖づ 提交于 2019-12-10 14:22:30

问题


Trying to queue a job with delayed_job as follows:

Delayed::Job.enqueue(BackgroundProcess.new(current_user, object))

current_user and object are not nil when I print them out. The weird thing is that sometimes refreshing the page or running the command again works!

Here is the exception trace:

  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'

回答1:


I would guess that it is caused by the fact that you send the objects as arguments to your jobs (at least I assume that current_user and object are in fact objects and not id's). Send the id's instead and start with loading the objects when perform starts.

For example:

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

This way, it does not risk any problem with serializing an ActiveRecord into the database and you will always load the latest changes when the job is run.




回答2:


Ran into this same problem as well. I still don't know what is causing it, but for some reason cloning the object seems to solve it

u = User.find 123
u.to_yaml
=> TypeError: wrong argument type nil (expected Data)
u.clone.to_yaml
=> works like normal

Very frustrating. Would be better to know the root cause, but this might help if you are desperate.



来源:https://stackoverflow.com/questions/4688073/weird-exception-with-delayed-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!