11

I'm currently building a rails platform and I've used devise for authentication and now want to move the default devise emails into a background process using sidekiq. I'm using devise-async for this and have done the following:

Added the devise_async.rb file:

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

Added the async command to the devise model:

#user.rb
devise :database_authenticatable, :async #etc.

The versions of the gems are the following:

Devise 2.1.2
Devise-async 0.4.0
Sidekiq 2.5.3

The issue that I'm having is that the emails are passed in the sidekiq queue but the workers never execute sending the emails. I've also looked at devise async not working with sidekiq and he seemed to have the same problem. But I don't think I have an issue with the hostname command.

Any thoughts on the issue?

Community
  • 1
  • 1
Julius
  • 576
  • 5
  • 12
  • It might be dumb to ask, but did you have a separate worker process running via `bundle exec sidekiq -C config.yaml` or something similar? – number5 Nov 20 '12 at 02:32
  • It's actually not dumb to ask, since I think stuff like that happens all the time. I actually started up redis using `redis-server` and then sidekiq using `bundle exec sidekiq`. The thing is that I know sidekiq is working since I'm also sending mails in a background process for invites, which is separate from devise. – Julius Nov 20 '12 at 09:07
  • 1
    Try `bundle exec sidekiq -q mailer` I think devise-async use mailer queue not default. – number5 Nov 20 '12 at 14:24
  • Thank you very much :) It actually resolved the issue. I guess sidekiq by default only serves its `default` queue, so you have to tell sidekiq to use the `mailer` queue. I'll be sure to pass it on to the devise-async module so that they can include it in their documentation! – Julius Nov 21 '12 at 09:17

3 Answers3

19

The answer is rather simple. You just need to tell sidekiq to use the mailer queue, by starting sidekiq with bundle exec sidekiq -q mailer. That way the mailer queue will be processed, without the option sidekiq will simply rely on the default queue.

Julius
  • 576
  • 5
  • 12
  • 6
    To do both you can just use `bundle exec sidekiq -q mailer -q default` Note this also prioritizes the mailer queue over the default queue. – crizCraig Jun 06 '13 at 21:41
0

In 2019, since device-async is not up to date and If you have your ActiveJob and sidekiq set-up is done documentation here.The most simple solution is to override you device send_devise_notification instance methods involved with the transactions mails like shown here

class User < ApplicationRecord
  # whatever association you have here
  devise :database_authenticatable, :confirmable
  after_commit :send_pending_devise_notifications
  # whatever methods you have here

 protected
  def send_devise_notification(notification, *args)
    if new_record? || changed?
      pending_devise_notifications << [notification, args]
    else
      render_and_send_devise_message(notification, *args)
    end
  end

  private

  def send_pending_devise_notifications
    pending_devise_notifications.each do |notification, args|
      render_and_send_devise_message(notification, *args)
    end

    pending_devise_notifications.clear
  end

  def pending_devise_notifications
    @pending_devise_notifications ||= []
  end

  def render_and_send_devise_message(notification, *args)
    message = devise_mailer.send(notification, self, *args)

    # Deliver later with Active Job's `deliver_later`
    if message.respond_to?(:deliver_later)
      message.deliver_later
    # Remove once we move to Rails 4.2+ only, as `deliver` is deprecated.
    elsif message.respond_to?(:deliver_now)
      message.deliver_now
    else
      message.deliver
    end
  end

end
0

This is now supported by Devise https://github.com/heartcombo/devise#activejob-integration

class User < ApplicationRecord

  devise ...

  # Override devise: send emails in the background
  def send_devise_notification(notification, *args)
    devise_mailer.send(notification, self, *args).deliver_later
  end

end
Frexuz
  • 4,732
  • 2
  • 37
  • 54