简体   繁体   中英

Rails Not Sending Emails in Background with Heroku Redis

I'm having trouble sending emails in my rails app in the background. I'm able to successfully send the very same welcome email if I attempt to do it in a non-background situation. I'm testing this app on heroku and have added heroku redis and have enabled the worker functionality in the dashboard. I don't have enough experience working with redis or sidekiq to troubleshoot this one myself. Looking for suggestions as to how to correct this issue.

Here's the command in my Devise Registrations Controller:

class RegistrationsController < Devise::RegistrationsController

  def create
    super
    if @user.persisted?
      WelcomeEmailJob.perform_later(current_user.id)
    end
  end
end

The background job code:

class WelcomeEmailJob < ActiveJob::Base
  queue_as :default

  def perform(user_id)
    user = User.find(user_id)
    WelcomeMailer.welcome_email(user).deliver       
  end
end

The welcome email:

class WelcomeMailer < ApplicationMailer
 def welcome_email(user)
    @user = user
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Website')
  end
end

I keep seeing something like this response in the heroku logs

2016-04-20T00:11:25+00:00 app[heroku-redis]: source=REDIS sample#active-connections=1 sample#load-avg-1m=0.09 sample#load-avg-5m=0.125 sample#load-avg-15m=0.12 sample#read-iops=0 sample#write-iops=0.020492 sample#memory-total=15405632.0kB sample#memory-free=13388156.0kB sample#memory-cached=493144kB sample#memory-redis=295880bytes sample#hit-rate=1 sample#evicted-keys=0

Any suggestions?

How are you triggering the job? Have you got a cron job setup ? Also deliver has been deprecated and replaced with deliver_now if you're on a recentish version of Rails.

Try:

class RegistrationsController < Devise::RegistrationsController

  def create
    super
    if @user.persisted?
      WelcomeMailer.welcome_email(user).deliver_later
    end
  end
end

This will enqueue it in the mailers queue. No extra job class necessary as action mailer will take care of it for you. You just need to make sure all your workers are fired up and processing jobs which I believe requires following heroku specific documentation to setup (I don't use heroku so not entirely sure what this entails).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM