我在Heroku上有一个自定义的域名,我有Redis的附件.我需要帮助了解如何为电子邮件通知创建后台工作人员.用户可以收件箱中的消息,我想向用户发送每封收到的新消息的电子邮件通知.我有通知工作在开发,但我不是很好的创建后台作业是需要的Heroku,否则的服务器将超时.
消息控制器
def create @recipient = User.find(params[:user]) current_user.send_message(@recipient,params[:body],params[:subject]) flash[:notice] = "Message has been sent!" if request.xhr? render :json => {:notice => flash[:notice]} else redirect_to :conversations end end
用户型号:
def mailBoxer_email(object) if self.no_email email else nil end end
MailBoxer.rb:
MailBoxer.setup do |config| #Configures if you applications uses or no the email sending for Notifications and Messages config.uses_emails = false #Configures the default from for the email sent for Messages and Notifications of MailBoxer config.default_from = "no-reply@domain.com" #Configures the methods needed by mailBoxer config.email_method = :mailBoxer_email config.name_method = :name #Configures if you use or not a search engine and wich one are you using #Supported enignes: [:solr,:sphinx] config.search_enabled = false config.search_engine = :sphinx end
解决方法
Sidekiq绝对是与Heroku一起去的方式.我不认为邮箱程序支持开箱即用的背景配置.幸运的是,sidekiq的排队流程仍然很容易.
>将gem’sidekiq’添加到您的gemfile并运行bundle.
>创建一个工作文件app / workers / message_worker.rb.
class MessageWorker include Sidekiq::Worker def perform(sender_id,recipient_id,body,subject) sender = User.find(sender_id) recipient = User.find(recipient_id) sender.send_message(recipient,subject) end end
>更新你的控制器来排队工人
删除:current_user.send_message(@recipient,params [:body],params [:subject])
添加:MessageWorker.perform_async(current_user.id,@ recipient.id,params [:subject])
注意:您不应该传递工作人员的ActiveRecord对象.这就是为什么我设置这个方法来传递用户ID,并在工作的执行方法中查找它们,而不是整个对象.
>最后,重新启动服务器并运行bundle exec sidekiq.现在你的应用程序应该发送电子邮件的背景.>部署时,您将需要一个单独的动作,这个工作应该是这样的:worker:bundle exec sidekiq.您还需要Heroku的redis附件.