Skip to content

Instantly share code, notes, and snippets.

@memoxmrdl
Forked from maxivak/00.md
Created May 18, 2016 12:56
Show Gist options
  • Select an option

  • Save memoxmrdl/8b7dade95f621077cea8e935ced45859 to your computer and use it in GitHub Desktop.

Select an option

Save memoxmrdl/8b7dade95f621077cea8e935ced45859 to your computer and use it in GitHub Desktop.
Sending emails with ActionMailer and Sidekiq

Sending emails with ActionMailer and Sidekiq

Send email asynchroniously using Sidekiq.

ActionMailer

Create your mailer us usual:

# app/mailers/users_mailer.rb

class UsersMailer < ActionMailer::Base

def welcome_email(user_id)
    @user = User.find(user_id)

    mail(   :to      => @user.email,
            :subject => "Welcome"
    ) do |format|
      format.text
      format.html
    end
  end
end

Send email:

user = User.find(1)

mail = UsersMailer.welcome_email(user.id)
#mail.deliver_now
mail.deliver_later

    

Sidekiq

Gemfile:

gem 'sidekiq'

Install Redis

Redis provides data storage for Sidekiq. It holds all the job data along with runtime and historical data

Configure Sidekiq

To make work mailer.deliver_later we need to tell ActiveJob to use Sidekiq:

# config/initializers/active_job.rb

ActiveJob::Base.queue_adapter = :sidekiq

As long as Active Job is setup to use Sidekiq we can use #deliver_later:

# config/environments/development.rb

Rails.application.configure do
  ...
  
  config.active_job.queue_adapter = :sidekiq
  
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :smtp
  
  config.action_mailer.smtp_settings = { ... }
  

end

Read more about ActionJob and Sidekiq: https://github.com/mperham/sidekiq/wiki/Active-Job

Configure Sidekiq

config/sidekiq.yml:

---
:concurrency: 1
:queues:
  - default
  - mailers

Specify queue name for different environments:

# config/initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0', namespace: "mysite_sidekiq_#{Rails.env}" }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379/0', namespace: "mysite_sidekiq_#{Rails.env}" }
end

Run Sidekiq

bundle exec sidekiq --environment development -C config/sidekiq.yml 

God + Sidekiq

Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e

Sidekiq and Devise

Read this: https://github.com/mperham/sidekiq/wiki/Devise

RSpec tests for Sidekiq

test environment:

# config/environments/test.rb

Rails.application.configure do
 ...
 

spec/rails_helper.rb:

# sidekiq
require 'sidekiq/testing'
Sidekiq::Testing.fake!  # by default it is fake

User Sidekiq::Worker.jobs.size to see the number of jobs in the queue.

Test that email was enqueued:


RSpec.describe "Test sending email with sidekiq", :type => :request do

  it 'send email to sidekiq' do
   
    expect{
      user = User.first
      mail = UsersMailer.welcome_email(user.id)
      mail.deliver_later
    }.to change( Sidekiq::Worker.jobs, :size).by(1)

  end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment