Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Devise emails through Resque

I'm trying to send Devise emails through Resque.

Regular emails are getting sent through Resque just fine. And Devise emails are sent just fine, but not Devise emails through Resque. I get "Could not find a valid mapping" which implies that my helper overrides aren't getting picked up.

I'm following this http://shaker.4-dogs.biz/2011/08/06/using-resque-to-send-mail-for-devise/

The odd thing is that to debug it I'm using a local copy of Devise and adding breakpoints in 'initialize_from_record' in Devise, which gets hit when I just use Devise alone. But when I send the Devise emails through resque the breakpoints don't get hit:

class ResqueMailer < Devise::Mailer
  include Resque::Mailer
end

config.mailer = "ResqueMailer"

And resque instead shows a packaged gem path and not my local source such as:

/Users/mm/.rvm/gems/ruby-1.9.2-p290@evergreen/gems/devise-1.4.9/lib/devise/mailers/helpers.rb:20:in `devise_mail'

Any idea why it's not using my local gem source AND/OR how to get Resque to send my Devise emails?

like image 541
99miles Avatar asked Nov 14 '11 17:11

99miles


2 Answers

An easy way is to use the devise-async gem.

Add the gem to your Gemfile

# Gemfile
gem "devise-async"

Configure Devise to use the proxy mailer.

# config/initializers/devise.rb
config.mailer = "Devise::Async::Proxy"

[Optional] And finally tell DeviseAsync to use Resque to enqueue the emails.

# config/initializers/devise_async.rb
Devise::Async.backend = :resque

The gem also supports Sidekiq and Delayed::Job.

like image 87
mhfs Avatar answered Nov 04 '22 17:11

mhfs


Update: you do not need to do this with resque_mailer >= 2.2.3

The monkey patch at https://github.com/devton/resqued_devise_mailer did not work since it sends the full model as an argument to Resque, which will end up marshaling the object and is frowned upon (see Persistence on https://github.com/defunkt/resque).

Here's what worked for me:

Use the resque_mailer gem: https://github.com/zapnap/resque_mailer

gem 'resque_mailer'

Add lib/devise_resque_mailer.rb: see https://gist.github.com/1375726

That creates a new DeviseResqueMailer class that will not change any existing behavior in Resque::Mailer, so you can use that module in other mailers.

config/initializers/devise.rb:

Devise.setup do |config|
  require 'devise_resque_mailer'
  config.mailer = "DeviseResqueMailer"

Move your devise views from app/views/devise/mailer/ to app/views/devise_resque_mailer/

like image 42
tee Avatar answered Nov 04 '22 18:11

tee