Rails Custom Delayed Job - uninitialized constant

梦想与她 提交于 2021-01-28 03:12:36

问题


I've been successfully using delayed_job for a couple of years now but recently I have a need to implement some kind of success/failure callback/hooks.

Following the delayed_job guide on github i've set up the following custom job:

class XmlImportJob < Struct.new(:file, :supplier_id, :user_id, :directory)
  def perform
    Product.xml_import(file, supplier_id, user, directory)
  end

  def success(job)
    ProductMailer.xml_import_complete.deliver
  end

  def failure(job)
    ProductMailer.xml_import_failed.deliver
  end
end

When running this with Delayed::Job.enqueue XmlImportJob.new(secure_url, 1, 1, directory) for example, I get a Job failed to load: uninitialized constant XmlImportJob. error.

I've tried saving my custom job which is in a file named xml_import.rb under app/jobs and lib and I get the same error.

At the moment i've only tried running this via rails console. Even when explicitly calling require 'xml_import' which returns true I get the same error.

Does anyone with experience of using custom delayed_jobs successfully have any idea what I'm doing wring here?


回答1:


To answer my own question;

Any custom directories with classes and modules you want to be autoloadable must be added to config/application.rb like so:

config.autoload_paths += %W(
  #{config.root}/app/jobs
)

The files contained within these folders must be named according to rails' conventions so XmlImportJob resides in xml_import_job.rb.



来源:https://stackoverflow.com/questions/20396944/rails-custom-delayed-job-uninitialized-constant

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