问题
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