加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 发送电子邮件时的Heroku超时

发布时间:2020-12-16 19:37:36 所属栏目:百科 来源:网络整理
导读:我在Heroku上有一个自定义的域名,我有Redis的附件.我需要帮助了解如何为电子邮件通知创建后台工作人员.用户可以收件箱中的消息,我想向用户发送每封收到的新消息的电子邮件通知.我有通知工作在开发,但我不是很好的创建后台作业是需要的Heroku,否则的服务器将
我在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附件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读