ruby-on-railsactionmailerrails-activejob

In Rails 6, how ensure a Mailer will not retry, eg, how add ActiveJob retry: false setting when Active job is used implicitly (not explicitly)?


In a new Rails 6.1 app, I want to explicitly disable any retries for mail jobs.

Since ActionMailer automatically uses ActiveJob, how can I add custom ActiveJob configuration for a specific mailer class, such as disabling sidekiq retries?

If there was an explicit ActiveJob class being used, it's easy:

class ExampleJob < ActiveJob::Base
  sidekiq_options retry: false # custom setting allowed here in a JOB

  def perform(*args)
    # Perform Job
  end
end

But in the case like ActionMailer where ActiveJob is used implicitly ("behind the curtain") how does one explicitly disable sidekiq retries for a mail job?

For example, when sending an email like this:

AccountInvitationsMailer.with(account_invitation: self).invite.deliver_later

when the current mailer code is:

class ApplicationMailer < ActionMailer::Base
  default from: "someone@example.com"
  layout "mailer"

  # Include any view helpers from your main app to use in mailers here
  helper ApplicationHelper
end


class AccountInvitationsMailer < ApplicationMailer
  def invite
    @account_invitation = params[:account_invitation]
    @account = @account_invitation.account
    @invited_by = @account_invitation.invited_by

    name = @account_invitation.name
    email = @account_invitation.email

    mail(
      to: "#{name} <#{email}>",
      from: "#{@invited_by.name} <invites@example.com>",
      subject: t(".subject", inviter: @invited_by.name, account: @account.name)
    )
  end
end

Solution

  • Actually you can explicitly set which ActiveJob class to bee used on ActionMailer like:

    class ApplicationMailer < ActionMailer::Base
      self.delivery_job = ExampleJob
    
      # blah blah blah
    end
    

    So you can set anything else later within the ActiveJob class.