ruby-on-rails – 使用delayed_job时出现奇怪的异常
发布时间:2020-12-17 03:26:53 所属栏目:百科 来源:网络整理
导读:尝试使用delayed_job对作业进行排队,如下所示: Delayed::Job.enqueue(BackgroundProcess.new(current_user,object)) 打印出来时,current_user和object不是nil.奇怪的是,有时刷新页面或再次运行命令有效! 这是异常跟踪: Delayed::Backend::ActiveRecord::J
尝试使用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' 解决方法
我猜这是因为您将对象作为参数发送到您的作业(至少我假设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序列化到数据库中就没有任何问题,并且在作业运行时总是会加载最新的更改. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |