delayed_job_mongoid is not writing data in backend

2019-08-28 17:35发布

问题:

I have mongoid model

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

and according to rails cast I have a class in my lib folder

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

I called some where in my controller

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

I can see with

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

And the result is not stored in the table for rr if i check

rr.body

it is still nill any one can help me out Thanks in advance

回答1:

Struct.new creates instances variable for you that you can access using either self or @

Try this

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


回答2:

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"))