Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of ActionMailer queue name #18587

Conversation

chrismcg
Copy link
Contributor

Currently all mails sent with deliver_later are put on the mailers queue. This patch keeps that default but allows the queue name to be configured globally.

The motivation was trying to use https://github.com/chanks/que as the queuing backend. It's a good fit for a simple queue for a single dyno heroku app but by default only listens to one queue per application. As ActiveJob will use the default queue and ActionMailer the mailers queue jobs wouldn't get run.

The Que library might change to support more than one queue but in the meantime this will allow Rails to work with it. It will also be useful for users of other backends who might need to change where mails are queued.

require "#{app_path}/config/environment"
require "mail"

_ = ActionMailer::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing I could set the configuration separately from using it.

@senny
Copy link
Member

senny commented Jan 28, 2015

We need to configure the default AM queue as well in Queue Classic.

@mandrews
Copy link

+1

@cristianbica
Copy link
Member

I'm thinking of a cleaner approach. ActiveJob::Base accepts a Proc for the queue_as and that proc is executed when trying to enqueue the job so we can leverage that:

  class DeliveryJob < ActiveJob::Base # :nodoc:
    queue_as do
      ActionMailer::Base. deliver_later_queue_name || :mailers
    end
  end

what do you think @chrismcg?

@senny
Copy link
Member

senny commented Feb 12, 2015

@cristianbica I'm not sure I understand the benefit of that approach. Why not set :mailers as the default for that option? This way you can at least use introspection on deliver_later_queue_name to see where the jobs are going. Otherwise the default is hidden away.

@cristianbica
Copy link
Member

@senny yeah the || :mailers makes no sense here but my main suggesting was to keep defining the deliver_later_queue_name on ActionMailer base use use that in the DeliveryJob's queue_as. In this PR implementation the queue name is set in the MessageDelivery wrapper.

@senny
Copy link
Member

senny commented Feb 12, 2015

@cristianbica 👍 got it.

@chrismcg
Copy link
Contributor Author

@cristianbica sorry, I've only just seen your comment. I didn't know you could use a proc there so I'll have a look at it later in the week when I've a bit more time.

@jakegavin
Copy link
Contributor

+1
Just came across this while upgrading to 4.2. This would be nice to have. Thanks for working on it.

@chrismcg
Copy link
Contributor Author

@cristianbica finally got around to doing that change. I think it's clearer this way.

@cristianbica
Copy link
Member

Please squash your commits. Otherwise seems good to me.

@chrismcg chrismcg force-pushed the allow_deliver_later_queue_name_to_be_configured branch from 1948144 to f5a131a Compare June 2, 2015 09:50
@chrismcg
Copy link
Contributor Author

chrismcg commented Jun 2, 2015

Commits squashed, thanks!

@rafaelfranca rafaelfranca merged commit f5a131a into rails:master Jun 3, 2015
rafaelfranca added a commit that referenced this pull request Jun 3, 2015
…e_to_be_configured

Allow configuration of ActionMailer queue name
rafaelfranca added a commit that referenced this pull request Jun 3, 2015
Also add a CHANGELOG entry for #18587

[ci skip]
@michaelsauter
Copy link

Is there a specific reason why this didn't make it into 4.2.4?

Judging by this PR, I thought it would be included since this has been merged on June 3, but 4.2.4 wasn't released until August 24.

@matthewd
Copy link
Member

@michaelsauter new features aren't backported to bugfix releases.

@michaelsauter
Copy link

@matthewd Ah okay, thanks. Though I think this almost counts as a bugfix ;)

@rafaelfranca rafaelfranca modified the milestones: 5.0.0, 5.0.0 [temp] Dec 30, 2015
@rossinkiwi
Copy link

I'm trying to set up separate mailer queues for transaction mail processing and bulk mail processing in my Rails 5.1.3 application. I was hoping to be able to designate the queue by setting the deliver_later_queue_name for each of my inherited ActionMailer's:

class ApplicationMailer < ActionMailer::Base
end

class UserMailer < ApplicationMailer  self.deliver_later_queue_name = 'mail'
  self.deliver_later_queue_name = 'mail'
  self.smtp_settings = {
    address: "smtp.sendgrid.net",
    port: 587,
    ...
}

class BulkMailer < ApplicationMailer
  self.deliver_later_queue_name = 'bulkmail'
  self.smtp_settings = {
    address: "email-smtp.us-west-2.amazonaws.com",
    port: 587,
    ...
  }

This works fine for the smtp_settings, but the deliver_later_queue_name appears to be a class variable? and so when I set it in the BulkMailer or UserMailer, it changes it for all the ActionMailer's.

Is there a way for me to set separate deliver_later_queue_name's for my BulkMailer and UserMailer?

@JamesLivengood
Copy link

Also curious about @rossinkiwi 's question

packagethief added a commit to packagethief/rails that referenced this pull request Feb 15, 2023
The `deliver_later_queue_name` is already configurable on ActionMailer::Base,
however the value is inherited by all subclasses. Use a class-inheritable
attribute instead, so that subclasses can override.

Refs:
- rails#18587 (comment)
packagethief added a commit to packagethief/rails that referenced this pull request Feb 15, 2023
The `deliver_later_queue_name` is already configurable on ActionMailer::Base,
however the value is inherited by all subclasses. Use a class-inheritable
attribute instead, so that subclasses can override.

Refs:
- rails#18587 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet