我刚开始研究使用delayed_job gem。
为了测试它,我将“延迟”添加到欢迎电子邮件功能并从
更改了该呼叫 UserMailer.welcome_email(self).deliver
到
UserMailer.delay.welcome_email(self)
这在User模型after_create中调用。我看到函数执行后在delayed_job表中显示了一个条目。现在,当我在命令行上运行“rake jobs:work”时,任务开始但是会出现如下错误
[Worker(host:Sanjay-PC pid:7008)] Starting job worker
[Worker(host:Sanjay-PC pid:7008)] Class#welcome_email failed with NoMethodError: undefined method `welcome_email' for #<Class:0x4871d60> - 0 failed attempts
[Worker(host:Sanjay-PC pid:7008)] 1 jobs processed at 0.0939 j/s, 1 failed ...
认为如果我将welcome_email方法声明更改为
的Class方法 def self.welcome_email(user)
(在前面加上自我。)可能有所帮助。但是当我运行rake工作时:工作我得到以下错误
rake aborted!
undefined method `welcome_email' for class `UserMailer'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.4/lib/delayed/message_sending.rb:50:in `handle_asynchronously'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:10:in `<class:UserMailer>'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:1:in `<top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load'
<Stack truncated>
现在似乎将该类知道为UserMailer,但它不知何故看不到类方法welcome_email。
我在Rails 3.0.5,Ruby 1.9.2p180和已安装的delayed_job gem是2.1.4 - 在Windows上
似乎无法在任何地方找到任何相关答案。
感谢您的想法。
-S
根据@ pjammer的请求添加UserMailer代码
class UserMailer < ActionMailer::Base
default :from => "from@example.com"
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
end
答案 0 :(得分:2)
请使用此
UserMailer.delay.welcome_email(self).deliver
而不是
UserMailer.welcome_email(self).delay.deliver
答案 1 :(得分:1)
我的解决方案是在处理程序类中重新定义函数(对你来说是UserMailer类)
def self.taguri
'tag:ruby.yaml.org,2002:class'
end
这是一个黑客,我会尝试找到一个更好的解决方案,但现在它适用于我。
(Rails 3.0.9,Ruby 1.9.2-p290,delayed_job 2.1.4)
答案 2 :(得分:0)
https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE解决了类方法的handle_asynchronously错误。
根据上面链接中的Brandon Keeper,代码如下:
class ClassName
class << self
def foo
end
handle_asynchronously :foo
end
end
然后使用ClassName.delay.foo