ActionMailer rescue_from failing when called from ActiveJob dispatched to Sidekiq

553 views Asked by At

I use Postmark for sending email and was looking to capture Postmark::InactiveRecipientError which currently just ends up in my dead jobs in my Sidekiq queue. I found on the postmark-rails wiki, what I thought would be the correct course: https://github.com/wildbit/postmark-rails/wiki/Error-Handling

class ApplicationMailer < ActionMailer::Base
  default from: "[email protected]"
  layout 'mailer'

  rescue_from Postmark::InactiveRecipientError, with: :reactivate_and_retry

  private

  def postmark_client
    ::Postmark::ApiClient.new(Rails.application.credentials.postmark_api_token)
  end

  def reactivate_and_retry(error)
    error.recipients.each do |recipient|
      bounce = postmark_client.bounces(emailFilter: recipient).first
      next unless bounce
      postmark_client.activate_bounce(bounce[:id])
    end

    # Try again immediately
    message.deliver
  end
end

When I test the above in development, everything works. My UserMailer goes to Sidekiq which kicks off and correctly activates a suppressed email which I can see the change made via the Postmark interface. However, in production, my jobs are failing with:

undefined method 'reactivate_and_retry' for class '#<Class:UserMailer>'

I haven't been able to find any resources -- this GitHub Issue seems to be a similar problem but doesn't have much for information -- https://github.com/mperham/sidekiq/issues/4161

I am using Rails 6.1.4.4 and Sidekiq 6.4.0.

1

There are 1 answers

0
GorillaApe On

I am having the same problem. It seems like nobody has this problem. The problem is that the job calls handle_exception_with_mailer_class and this doesn't have an instance of mailer so it tries to call a class level method. If you define it as a class method it should work though, or use a lambda/block.

https://github.com/rails/rails/blob/cf82c9d7826aa36f2481114961af02dbf39896dd/actionmailer/lib/action_mailer/delivery_job.rb#L37