2011-11-22 23 views
0

我有一個解決方案,我可以發送設計'股票電子郵件'默認包含在延遲作業Devise中的郵件。以異步的方式。因此,我使用下面的代碼:設計異步/延期工作 - 發送延遲工作的自定義電子郵件?

/initializers/devise_acync.rb

module Devise 


    module Models 

    module Confirmable 
     alias_method :send_confirmation_instructions_without_delay, :send_confirmation_instructions 
     handle_asynchronously :send_confirmation_instructions 
    end 

    module Recoverable 
     alias_method :send_reset_password_instructions_without_delay, :send_reset_password_instructions 
     handle_asynchronously :send_reset_password_instructions 
    end 

    module Lockable 
     alias_method :send_unlock_instructions_without_delay, :send_unlock_instructions 
     handle_asynchronously :send_unlock_instructions 
    end 

    module Invitable 
     alias_method :deliver_invitation_without_delay, :deliver_invitation 
     handle_asynchronously :deliver_invitation 
    end 


    end 

end 

在我的用戶模型遺贈是鏈接到該模型我做

def confirm! 
    welcome_message 
    super 
    end 

private 

    def welcome_message 
    ::Devise.mailer.welcome_instructions(self).deliver 
    end 

的一個大問題這讓我眼花繚亂: 我將如何能夠發送這個歡迎消息true delayed_job? 你將如何添加其他電子郵件是自定義的,而不是包括設計,所以他們也發送真正delayed_job?

回答

0

您是否嘗試過做這樣的:

def welcome_message 
    ::Devise.mailer.delay.welcome_instructions(self) 
end 
+0

愚蠢的沒有嘗試,因爲它如此明顯,我相信它的作品至少在發展,thx! – Rubytastic

3

我發現這是一個完美的解決方案 - http://danseaver.com/blog/2011/10/18/sending-devise-emails-with-delayed-job

我改變了它稍微處理全部流出色器件電子郵件,運行軌道3.0.7 + DJ 3.0.1

配置/初始化/ devise.rb

# override Devise::Mailer to use queue outgoing mail into DJ 
class DeviseMailer < Devise::Mailer 

    def confirmation_instructions(record) 
    Devise::Mailer.delay.confirmation_instructions(record) 
    end 

    def reset_password_instructions(record) 
    Devise::Mailer.delay.reset_password_instructions(record) 
    end 

    def unlock_instructions(record) 
    Devise::Mailer.delay.unlock_instructions(record) 
    end 
end 

# Use this hook to configure devise mailer, warden hooks and so forth. The first 
# four configuration values can also be set straight in your models. 
Devise.setup do |config| 
    # ==> Mailer Configuration 
    # Configure the e-mail address which will be shown in DeviseMailer. 
    config.mailer_sender = APP_CONFIG[:member_email] 

    # Configure the class responsible to send e-mails. 
    config.mailer = "DeviseMailer" 
... 
+0

thx,現在使用resque但會對其他人有用。 – Rubytastic

+0

jackquack的答案下面是更好的一個 –

1

我發現以上都不適合我。我正在使用Devise 2.0.4和Rails 3.2.2 with delayed_job_active_record 0.3.2

devise實際上談論在代碼的註釋中做這樣的事情的方式是覆蓋User類中的方法。因此,我解決它像這樣,它完美的作品:

應用程序/模型/ User.rb

def send_on_create_confirmation_instructions 
    Devise::Mailer.delay.confirmation_instructions(self) 
end 
def send_reset_password_instructions 
    Devise::Mailer.delay.reset_password_instructions(self) 
end 
def send_unlock_instructions 
    Devise::Mailer.delay.unlock_instructions(self) 
end 
+0

檢查一個我發佈http://stackoverflow.com/a/14799039/179855。它的工作對我來說非常好。 –

0

這其實很簡單通過覆蓋用戶模型上的單個方法來完成。 Devise通過send_devise_notification方法發送所有電子郵件,因此只需將該代碼更改爲異步友好。

def send_devise_notification(notification, opts = {}) 
    devise_mailer.delay.send(notification, self, opts) 
end 

此代碼假設Sidekiq,雖然語法可能與Resque或DelayedJob兼容。

0

(問題是近3歲,但仍然很容易通過谷歌找到,因此,這裏是我的貢獻)

最佳做法是按照設備的使用作爲源https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L129

class User 
    devise :database_authenticatable, :confirmable 

    after_commit :send_pending_notifications 

    protected 

    def send_devise_notification(notification, *args) 
    # If the record is new or changed then delay the 
    # delivery until the after_commit callback otherwise 
    # send now because after_commit will not be called. 
    if new_record? || changed? 
     pending_notifications << [notification, args] 
    else 
     devise_mailer.send(notification, self, *args).deliver 
    end 
    end 

    def send_pending_notifications 
    pending_notifications.each do |notification, args| 
     devise_mailer.send(notification, self, *args).deliver 
    end 

    # Empty the pending notifications array because the 
    # after_commit hook can be called multiple times which 
    # could cause multiple emails to be sent. 
    pending_notifications.clear 
    end 

    def pending_notifications 
    @pending_notifications ||= [] 
    end 
end 

描述在您只需要更換2次出現的

devise_mailer.send(notification, self, *args).deliver 

通過

devise_mailer.delay.send(notification, self, *args)