0

我有 mongoid 模型

class RequestResponse
  include Mongoid::Document
  field :body, type: String
  field :job_id, type: Fixnum
  field :completed, type: Boolean
end

根据rails cast,我的lib文件夹中有一个类

class MyJob < Struct.new(:session, :url, :r_id)
  def perform
    rr = RequestResponse.find(r_id)
    session = YAML.load session
    rr.body = session.get(url).body
    rr.completed = true
    rr.save
  end
end

我在控制器中的某个地方调用了

rr = RequestResponse.new
rr.save
Delayed::Job.enqueue(MyJob.new(session.to_yaml, url, rr.id),3)

我可以看到

rake jobs:work
1 jobs processed at 19.3392 j/s, 0 failed ...

如果我检查,结果不会存储在 rr 的表中

rr.body

仍然没有任何人可以帮助我提前谢谢

4

2 回答 2

0

Struct.new 为您创建实例变量,您可以使用self@

尝试这个

class MyJob < Struct.new(:session, :url, :r_id)
  def perform
    rr = RequestResponse.find(@r_id)
    session = YAML.load @session
    rr.body = session.get(@url).body
    rr.completed = true
    rr.save
  end
end
于 2013-06-26T16:25:57.033 回答
0

My job was silently deleted. I have fixed the problem by creating a file in config/initializers/custom.rb and put this line

require File.expand_path(File.join(File.dirname(__FILE__), "../../lib/my_job"))
于 2013-06-27T14:29:21.827 回答