Devise Async provides an easy way to configure Devise to send its emails asynchronously using your preferred queuing backend.
Supported backends:
- Resque
- Sidekiq
- Delayed::Job
- QueueClassic
Add this line to your application's Gemfile:
gem 'devise-async'
And then execute:
$ bundle
Or install it yourself as:
$ gem install devise-async
Add :async
to the devise
call in your model:
class User < ActiveRecord::Base
devise :database_authenticatable, :async, :confirmable # etc ...
end
Set your queuing backend by creating config/initializers/devise_async.rb
:
# Supported options: :resque, :sidekiq, :delayed_job, :queue_classic
Devise::Async.backend = :resque
Tip: it defaults to Resque. You don't need to create the initializer if using it.
Set Devise::Async::Proxy
as Devise's mailer in config/initializers/devise.rb
:
# Configure the class responsible to send e-mails.
config.mailer = "Devise::Async::Proxy"
WARNING 1: before devise 2.1.1 devise-async had issues with background jobs trying to run before the record was committed to the DB if the creation was transactioned.
WARNING 2: I intend to deprecate support for devise < 2.1.1 before 1.0.0 and drop support in the 1.0.0 release. Please consider upgrading your Devise version.
If you inherit Devise::Mailer
to a class of your own for customization purposes,
you'll need to tell Devise::Async
to proxy to that class.
# config/initializers/devise_async.rb
Devise::Async.mailer = "MyCustomMailer"
Let you specify a custom queue where to enqueue your background Devise jobs. Defaults to :mailer.
# config/initializers/devise_async.rb
Devise::Async.queue = :my_custom_queue
To avoid repeating Devise::Async
in the initializer file you can use the block syntax
similar do what Devise
offers.
# config/initializers/devise_async.rb
Devise::Async.setup do |config|
config.backend = :resque
config.mailer = "MyCustomMailer"
config.queue = :my_custom_queue
end
Be aware that since version 0.3.0 devise-async enqueues the background job in active
record's after_commit
hook. If you're using rspec's use_transactional_fixtures
the jobs
might not be enqueued as you'd expect.
More details in this stackoverflow thread.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Released under the MIT License. See the LICENSE file for further details.