【问题标题】:Delayed_job in rails failingRails 中的 Delayed_job 失败
【发布时间】:2011-06-08 23:21:27
【问题描述】:

我刚刚开始考虑使用 delay_job gem。

为了测试它,我在欢迎电子邮件功能中添加了“延迟”并将该调用更改为

    UserMailer.welcome_email(self).deliver

UserMailer.delay.welcome_email(self)

这在用户模型 after_create 中被调用。函数执行后,我看到延迟作业表中出现了一个条目。现在,当我在命令行上运行“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)

(在前面添加 self.)这可能会有所帮助。但是当我运行 rake jobs:work 时出现以下错误

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 上,安装的延迟作业 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

【问题讨论】:

  • 请为您的邮件类添加代码,UserMailer.welcome_email(self).deliver 工作了吗?

标签: ruby-on-rails ruby-on-rails-3 delayed-job


【解决方案1】:

就用这个

UserMailer.delay.welcome_email(self).deliver

而不是

UserMailer.welcome_email(self).delay.deliver

【讨论】:

    【解决方案2】:

    我的解决方案是在处理程序类中重新定义函数(对你来说它是 UserMailer 类)

    def self.taguri
      'tag:ruby.yaml.org,2002:class'
    end
    

    这是一个 hack,我会尝试找到更好的解决方案,但现在它对我有用。

    (Rails 3.0.9、Ruby 1.9.2-p290、delayed_job 2.1.4)

    【讨论】:

      【解决方案3】:

      https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE 解决了类方法的handles_asynchronously 错误。

      根据上面链接中的 Brandon Keeper,代码如下:

      class ClassName
        class << self
          def foo
          end
          handle_asynchronously :foo
        end
      end
      

      然后使用ClassName.delay.foo

      【讨论】:

        猜你喜欢
        • 2011-05-23
        • 2014-10-30
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        • 2012-04-19
        • 1970-01-01
        • 2013-05-08
        相关资源
        最近更新 更多