我刚刚开始研究使用delay_job gem。
I am just beginning to look into using the delayed_job gem.
为了测试它,我在欢迎电子邮件功能中添加了 delayed,并从
To test it, I added "delayed" to the welcome email function and changed that call from
UserMailer.welcome_email(self).deliver至
UserMailer.delay.welcome_email(self)这在用户模型after_create中调用。函数执行后,我看到一个条目显示在delay_job表中。现在,当我在命令行上运行 rake jobs:work时,任务开始但出现以下错误
This is called inside the User model after_create. I see an entry show up in the delayed_job table after the function executes. Now when I run "rake jobs:work" on command line the task starts but gives errors as below
[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方法,则为
Thinking that if I changed the welcome_email method declaration to a Class method as
def self.welcome_email(user)(在前面添加self。)可能会有所帮助。但是当我运行rake job:work时,出现以下错误
(added self. in front) that might help. But then when I run rake jobs:work I get the following error
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。
It seems to now know the class as UserMailer but it somehow doesn't see the class method welcome_email.
我在Rails 3.0.5,Ruby 1.9.2p180上并且已安装的delay_job gem为2.1.4-在Windows上
I am on Rails 3.0.5, Ruby 1.9.2p180 and the installed delayed_job gem is 2.1.4 - on Windows
似乎在任何地方都找不到任何相关的答案。
Can't seem to find any related answers anywhere.
感谢您的想法。
-S
根据@pjammer的请求添加UserMailer代码
Adding UserMailer code per @pjammer's request
class UserMailer < ActionMailer::Base default :from => "from@example" def welcome_email(user) @user = user @url = "example/login" mail(:to => user.email, :subject => "Welcome to My Awesome Site") end end推荐答案
只需使用
UserMailer.delay.welcome_email(self).deliver而不是
UserMailer.welcome_email(self).delay.deliver更多推荐
Rails的Delayed
发布评论