Rails的Delayed_job失败 [英] Delayed_job in rails failing

查看:41
本文介绍了Rails的Delayed_job失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究使用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.com"

  def welcome_email(user)
    @user = user
    @url  = "http://example.com/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_job失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆