![](/img/trans.png)
[英]Delayed_Job - handle_asynchronously with ActionMailer?
[英]How does one use delayed_job to make an Rails 3.0 ActionMailer run asynchronously? Encountering ArgumentErrors
我正在嘗試延遲通知 email 在注冊我的應用程序時發送給用戶。 電子郵件是使用我稱之為InitMailer
的ActionMailer
發送的。 我試圖延遲工作的方式是使用collectiveidea的delayed_job
https://github.com/collectiveidea/delayed_job 。 為此,您可以看到我在定義方法initial_email
后指定handle_asynchronously
:
class InitMailer < ActionMailer::Base
default :from => "info@blahblahblah.com"
def initial_email(user)
@user = user
@url = "http://www.blahblahblah.com"
mail(:to => user.email,
:subject => "Welcome to my website!"
)
end
handle_asynchronously :initial_email
end
但是,我在日志文件"delayed_job.log"
中遇到參數錯誤:
Class#initial_email failed with ArgumentError: wrong number of arguments (1 for 0) - 5
failed attempts
供您參考,email 使用以下線路在 controller 中發送:
@user = InitUser.new(params[:init_user])
InitMailer.delay.initial_email(@user)
此外,當我沒有延遲地設置我的代碼時,電子郵件發送沒有問題(除了它減慢了我的應用程序等待 gmail 服務器的速度)
這里的錯誤在哪里? 我怎樣才能讓延遲的郵件正確發送?
由於 Rails3 實現郵件程序的方式,delayed_jobs 有一些不尋常的解決方法。 例如,你已經看到為了延遲郵寄,你寫
ExampleMailer.delay.example(user)
雖然通常您必須在方法定義之后編寫handle_asynchronously
,但對於郵件程序,此聲明(出於某種原因)會阻止延遲的工作正常工作。
所以在這段代碼中,完全放棄聲明:
class InitMailer < ActionMailer::Base
default :from => "info@blahblahblah.com"
def initial_email(user)
@user = user
@url = "http://www.blahblahblah.com"
mail(:to => user.email,
:subject => "Welcome to my website!"
)
end
#No handle_asynchronously needed here
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.