延迟工作奇怪的例外 [英] Weird exception with delayed_job

查看:58
本文介绍了延迟工作奇怪的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试按以下步骤将具有延迟工作的作业排入队列:

Trying to queue a job with delayed_job as follows:

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

current_user和对象在打印时不是nil。奇怪的是,有时刷新页面或再次运行命令仍然有效!

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!

这是异常跟踪:

  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和object实际上是对象,而不是id)。而是发送ID,并在执行开始时开始加载对象。

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.

例如:

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序列化到数据库时出现问题,并且在作业运行时您将始终加载最新的更改。

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.

这篇关于延迟工作奇怪的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆