Rails自定义延迟作业-未初始化的常量 [英] Rails Custom Delayed Job - uninitialized constant
问题描述
我已经成功使用了delay_job两年了,但是最近我需要实现某种成功/失败回调/挂钩.
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.
在github上的delay_job指南之后,我设置了以下自定义作业:
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
例如,当使用 Delayed :: Job.enqueue XmlImportJob.new(secure_url,1,1,directory)
运行此代码时,我得到一个 Job加载失败:未初始化的常量XmlImportJob.
错误.
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.
我尝试将我的自定义作业保存在app/jobs和lib下名为 xml_import.rb
的文件中,但出现相同的错误.
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.
此刻,我只尝试通过Rails控制台运行它.即使显式调用 require'xml_import'
返回true,我也会遇到相同的错误.
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.
任何有使用自定义delay_jobs经验的人都知道我在这里干什么吗?
Does anyone with experience of using custom delayed_jobs successfully have any idea what I'm doing wring here?
推荐答案
回答我自己的问题;
任何带有要自动加载的类和模块的自定义目录都必须添加到config/application.rb中,如下所示:
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
)
这些文件夹中包含的文件必须根据rails的约定进行命名,以便XmlImportJob驻留在xml_import_job.rb中.
The files contained within these folders must be named according to rails' conventions so XmlImportJob resides in xml_import_job.rb.
这篇关于Rails自定义延迟作业-未初始化的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!