我目前正在构建一个rails平台,我已经使用devise进行了身份验证,现在我想使用sidekiq将默认的devise电子邮件移到后台进程中。为此,我使用了devise-async,并完成了以下操作:
添加了devise_async.rb文件:
#config/initializers/devise_async.rb
Devise::Async.backend = :sidekiq向devise模型添加了async命令:
#user.rb
devise :database_authenticatable, :async #etc.gems的版本如下:
Devise 2.1.2
Devise-async 0.4.0
Sidekiq 2.5.3我遇到的问题是,电子邮件在sidekiq队列中传递,但工作人员从不执行发送电子邮件。我也看过devise async not working with sidekiq,他似乎也有同样的问题。但是我不认为我对hostname命令有什么问题。
对这个问题有什么想法吗?
发布于 2012-11-21 17:21:59
答案很简单。您只需通过使用bundle exec sidekiq -q mailer启动sidekiq,告诉sidekiq使用mailer队列。这样邮件队列将被处理,没有选项sidekiq将简单地依赖于default队列。
发布于 2019-05-10 02:13:48
在2019年,由于 device -async不是最新的,并且如果您已完成ActiveJob和sidekiq设置,则最简单的解决方案是覆盖与事务邮件相关的devicedocumentation here实例方法,如图所示的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发布于 2021-06-11 12:45:40
现在,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
endhttps://stackoverflow.com/questions/13452020
复制相似问题