Rails Delayed Job & Library Class

断了今生、忘了曾经 提交于 2019-12-17 18:59:31

问题


Hey we have a library class (lib/Mixpanel) that calls delayed job as follows:

class Mixpanel

  attr_accessor :options
  attr_accessor :event

  def track!()
   .. 
   dj = send_later :access_api # also tried with self.send_later
   ..
  end

  def access_api
   ..
  end

The problem is that when we run rake jobs:work: we get the following error:

undefined method `access_api' for #<YAML::Object:0x24681b8>

Any idea why?


回答1:


Delayed_job always autoloads ActiveRecord classes, but it doesn't know about other types of classes (like lib) that it has marshaled in the db as YML. So, you need to explicitly trigger the class loader for them. Since DJ starts up the Rails environment, just mention any non-AR marshaled classes in an initializer:

(config/initializers/load_classes_for_dj.rb)

Mixpanel



回答2:


A small gotcha, I followed Jonathan's suggestion, but I needed to add a require before the class name, so I'd use this for load_classes_for_dj.rb:

require 'mixpanel'
Mixpanel

Then it worked fine!



来源:https://stackoverflow.com/questions/2569396/rails-delayed-job-library-class

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