我目前正在构建一个rails平台,我已经使用了设计进行身份验证,现在想要使用sidekiq将默认设计电子邮件移动到后台进程中。我正在使用devise-async并执行了以下操作:
添加了devise_async.rb文件:
#config/initializers/devise_async.rb
Devise::Async.backend = :sidekiq
在设计模型中添加了async命令:
#user.rb
devise :database_authenticatable, :async #etc.
宝石的版本如下:
Devise 2.1.2
Devise-async 0.4.0
Sidekiq 2.5.3
我遇到的问题是电子邮件是在sidekiq队列中传递的,但工作人员从不执行发送电子邮件。我也看了devise async not working with sidekiq,他似乎有同样的问题。但我认为hostname命令没有问题。
关于这个问题的任何想法?
答案 0 :(得分:19)
答案很简单。您只需要告诉sidekiq使用mailer
队列,通过bundle exec sidekiq -q mailer
启动sidekiq。这样就可以处理邮件程序队列,而没有选项,sidekiq将只依赖于default
队列。
答案 1 :(得分:0)
在2019年,由于 device-async 尚未更新,并且如果您有ActiveJob和sidekiq设置,documentation here就完成了。与交易邮件相关的 send_devise_notification 实例方法,如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
答案 2 :(得分:0)
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