Rails 5 allows configuring queue name for mailers

Abhishek Jain

Abhishek Jain

February 26, 2016

This blog is part of our  Rails 5 series.

In Rails 4.2, Active Job was integrated with Action Mailer to send emails asynchronously.

Rails provides deliver_later method to enqueue mailer jobs.

1
2class UserMailer < ApplicationMailer
3  def send_notification(user)
4    @user = user
5    mail(to: user.email)
6  end
7end
8
9> UserMailer.send_notification(user).deliver_later
10=> <ActionMailer::DeliveryJob:0x007ff602dd3128 @arguments=["UserMailer", "send_notification", "deliver_now", <User id: 1, name: "John", email: "[email protected]">], @job_id="d0171da0-86d3-49f4-ba03-37b37d4e8e2b", @queue_name="mailers", @priority=nil>
11

Note that the task of delivering email was put in queue called mailers.

In Rails 4.x, all background jobs are given queue named "default" except for mailers. All outgoing mails are given the queue named "mailers" and we do not have the option of changing this queue name from "mailers" to anything else.

Since Rails 4.x comes with minimum of two queues it makes difficult to use queuing services like que which relies on applications having only one queue.

Customizing queue name in Rails 5

In Rails 5, we can now change queue name for mailer jobs using following configuration.

1
2config.action_mailer.deliver_later_queue_name = 'default'
3
4class UserMailer < ApplicationMailer
5  def send_notification(user)
6    @user = user
7    mail(to: user.email)
8  end
9end
10
112.2.2 :003 > user = User.last
12=> <User id: 6, name: "John", email: "[email protected]">
13
142.2.2 :004 > UserMailer.send_notification(user).deliver_later
15=> <ActionMailer::DeliveryJob:0x007fea2182b2d0 @arguments=["UserMailer", "send_notification", "deliver_now", <User id: 1, name: "John", email: "[email protected]">], @job_id="316b00b2-64c8-4a2d-8153-4ce7abafb28d", @queue_name="default", @priority=nil>

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.