2010-12-21 80 views
5

我一直在試圖弄清楚如何使用delayed_job和rails 3發送延遲的郵件。我已經嘗試了幾乎所有可行的可能性組合,我可以想到 - 我可以讓郵件在後臺運行,但我無法讓郵件延遲發送到未來的時間。在DB中delayed_jobs表清除,日誌說「發」的任務,該任務的delayed_job處理器拿起&說無故障發送任務......但郵件可以是:Rails 3/delayed_job - 通緝:延遲郵件的基本示例

  • 立即發送,或
  • 根本沒有到達

如果我嘗試在未來發送。

如果任何人都可以提供未來發送郵件的rails 3 delayed_job的裸機示例,我會非常感激。我敢肯定,很多人都這麼做,所以我懷疑我錯過了一些明顯的東西。一(無數)的組合,我試過如下:

的delayed_job:2.1.2 導軌:3.0.3 的ActionMailer:3.0.3

控制器:

class TestmailerController < ApplicationController 
    def index 
    Testmailer.delay.test_mail 
    end 

end 

梅勒:

class Testmailer < ActionMailer::Base 
    def test_mail 
    mail(:to => '([email protected]', :from => '(removedforprivacy)@gmail.com', :subject => 'Testing Delayed Job', :content_type => 'text/plain').deliver 
    end 
    handle_asynchronously :test_mail, :run_at => Proc.new { 2.minutes.from_now } 


end 

config/environments/development.rb的相關郵件部分:

# Don't care if the mailer can't send 
    config.action_mailer.raise_delivery_errors = true 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    config.action_mailer.default_url_options = { :host => 'localhost:3000' } 

    ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port => 587, 
    :domain => "gmail.com", 
    :user_name => "(removedforprivacy)", 
    :password => "(removedforprivacy)", 
    :authentication => "plain", 
    :enable_starttls_auto => true 
    } 

工作命令:

rake jobs:work 

回答

13

我同意安德烈 - 我是有這種確切的問題,並從sqlite的轉換我的本地開發數據庫到MySQL後,我可以像

Emailer.delay({:run_at => 5.minutes.from_now}).welcome(@user) 

運行代碼,並在5分鐘後發送電子郵件。請注意,您可能需要比五分鐘更長的延遲(以防時區怪異),以確保其正常工作。

+0

你知道如何在沒有延遲的情況下完成這個任務嗎?我想要使​​用handle_async ...方法。我喜歡能夠在所有被調用的地方延遲一個方法,而不是改變所有的調用者。 – baash05 2013-02-22 00:18:52

1

兩者使用.delay方法和設置handle_asynchronously :test_mail是多餘的。嘗試從您的代碼中刪除.delay方法。使用簡單

Testmailer.test_mail # without .deliver due to a delayed_job issue 

不過,我跑配置一些測試,並使用sqlite的run_at被簡單地忽略(不知道爲什麼)時,但在使用mysql2一切工作正常。

1

我發現在Rails 3與mongoid,刪除handle_asynchronously線讓它工作。我遇到了各種各樣的問題,並且看起來delayed_job沒有識別我的Emailer類中的任何對象。刪除handle_asynchronously修復它。