简体   繁体   English

Rails 中的 Delayed_job 失败

[英]Delayed_job in rails failing

I am just beginning to look into using the delayed_job gem.我刚刚开始考虑使用 delay_job gem。

To test it, I added "delayed" to the welcome email function and changed that call from为了测试它,我在欢迎 email function 中添加了“延迟”并将该呼叫从

    UserMailer.welcome_email(self).deliver

to

UserMailer.delay.welcome_email(self)

This is called inside the User model after_create.这在用户 model after_create 内部调用。 I see an entry show up in the delayed_job table after the function executes.在 function 执行后,我看到延迟作业表中出现了一个条目。 Now when I run "rake jobs:work" on command line the task starts but gives errors as below现在,当我在命令行上运行“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 ...

Thinking that if I changed the welcome_email method declaration to a Class method as认为如果我将 welcome_email 方法声明更改为 Class 方法

 def self.welcome_email(user)

(added self. in front) that might help. (添加自我。在前面)这可能会有所帮助。 But then when I run rake jobs:work I get the following error但是当我运行 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>

It seems to now know the class as UserMailer but it somehow doesn't see the class method welcome_email.现在似乎知道 class 为 UserMailer,但不知何故看不到 class 方法welcome_email。

I am on Rails 3.0.5, Ruby 1.9.2p180 and the installed delayed_job gem is 2.1.4 - on Windows我在 Rails 3.0.5、Ruby 1.9.2p180 上,安装的延迟作业 gem 是 2.1.4 - 在 Windows

Can't seem to find any related answers anywhere.似乎在任何地方都找不到任何相关的答案。

Thanks for your thoughts.谢谢你的想法。

-S -S

Adding UserMailer code per @pjammer's request根据@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

Just use this就用这个

UserMailer.delay.welcome_email(self).deliver

instead of代替

UserMailer.welcome_email(self).delay.deliver

My solution was to redefine function at the handler class (for you it's UserMailer class)我的解决方案是在处理程序 class 处重新定义 function (对你来说它是 UserMailer 类)

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

It's a hack and I'll try to find a better solution but now it works for me.这是一个 hack,我会尝试找到一个更好的解决方案,但现在它对我有用。

(Rails 3.0.9, Ruby 1.9.2-p290, delayed_job 2.1.4) (导轨 3.0.9、Ruby 1.9.2-p290、delayed_job 2.1.4)

https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE solved my handles_asynchronously error for class methods. https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE为 class 方法解决了我的 handles_asynchronously 错误。

As per Brandon Keeper in the link above, the code is the following:根据上面链接中的 Brandon Keeper,代码如下:

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

then use ClassName.delay.foo然后使用ClassName.delay.foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM