簡體   English   中英

Rails 中的 Delayed_job 失敗

[英]Delayed_job in rails failing

我剛剛開始考慮使用 delay_job gem。

為了測試它,我在歡迎 email function 中添加了“延遲”並將該呼叫從

    UserMailer.welcome_email(self).deliver

UserMailer.delay.welcome_email(self)

這在用戶 model after_create 內部調用。 在 function 執行后,我看到延遲作業表中出現了一個條目。 現在,當我在命令行上運行“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 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>

現在似乎知道 class 為 UserMailer,但不知何故看不到 class 方法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.delay.welcome_email(self).deliver

代替

UserMailer.welcome_email(self).delay.deliver

我的解決方案是在處理程序 class 處重新定義 function (對你來說它是 UserMailer 類)

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

這是一個 hack,我會嘗試找到一個更好的解決方案,但現在它對我有用。

(導軌 3.0.9、Ruby 1.9.2-p290、delayed_job 2.1.4)

https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE為 class 方法解決了我的 handles_asynchronously 錯誤。

根據上面鏈接中的 Brandon Keeper,代碼如下:

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

然后使用ClassName.delay.foo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM