Heroku timeout when sending emails

xps15z

I am on Heroku with a custom domain, and I have the Redis add-on. I need help understanding how to create a background worker for email notifications. Users can inbox message each other, and I would like to send a email notification to the user for each new message received. I have the notifications working in development, but I am not good with creating background jobs which is required for Heroku, otherwise the server would timeout.

Messages Controller:

  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

User model:

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 = "[email protected]"

  #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 which one are you using
  #Supported enignes: [:solr,:sphinx]
  config.search_enabled = false
  config.search_engine = :sphinx
end
JeffD23

Sidekiq is definitely the way to go with Heroku. I don't think mailboxer supports background configuration out of the box. Thankfully, it's still really easy with sidekiq's queueing process.

  1. Add gem 'sidekiq' to your gemfile and run bundle.
  2. Create a worker file 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, body, subject) 
  end
end
  1. Update your Controller to Queue Up the Worker

Remove: current_user.send_message(@recipient, params[:body], params[:subject])

Add: MessageWorker.perform_async(current_user.id, @recipient.id, params[:body], params[:subject])

Note: You should never pass workers ActiveRecord objects. That's why I setup this method to pass the User ids and look them up in the worker's perform method, instead of the entire object.

  1. Finally, restart your server and run bundle exec sidekiq. Now your app should be sending the email background.

  2. When you deploy, you will need a separate dyno for the worker which should look like this: worker: bundle exec sidekiq. You will also need Heroku's redis add-on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Emails not sending when requesting signatures for DocuSign API

Foreign letters failing when sending emails

Error when sending emails using Nodemailer

PEAR mail authentication failure when sending emails

smtplib disconnected when sending multiple emails

Rails 5 app deployed to heroku not sending sendgrid emails in production

Problem with sending all emails to the same person when sending email with PHPMailer

Connection Timeout When Sending HTTP Request in Java

Boot timeout when deploying an sbt project to heroku

Do subdomains protect a domain's reputation when sending emails?

Is it a problem when sending email where the 'from address' has multiple emails?

How come my forEach loop gets error when sending emails

Sending Emails with Mailtrap When automated script fails (Selenium - Java)

Personalizations field error when sending multiple dynamic template emails in sendgrid

Postfix trying to contact wrong server when sending emails?

Sending emails from node server fails when using forever

Do I need to escape POST when sending emails?

Laravel Trying to get property of non-object when sending emails

How to translate emails when sending them asynchronously with Symfony Messenger?

How to use Disconnect when sending bulk emails with Mailkit?

Sender name not display when sending emails via Mailgun

Microsoft.Exchange.WebServices.Data.ServiceResponseException: When Sending Emails

SendGrid personalization when sending emails on N to N relationship

Customizing cell format/script when sending emails from Gsheets?

How do I remove the recipent's name when sending emails?

When sending automated emails with attachments sometimes files are duplicated

Sending Emails in Production Environment

Gitlab failing at sending emails

AppScript is not sending Emails automatically