简体   繁体   中英

Rails Custom Delayed Job - uninitialized constant

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM