I am using an after_action callback in my mailers to record that email was sent. The emails are sent through Delayed Job. This works, except when we are unable to reach the remote server - in which case, the email is not sent, but we record that it was. Delayed Job retries the email later, and it is successfully delivered, but we've then recorded that two emails were sent.
It looks something like this:
class UserMailer < ActionMailer::Base
after_action :record_email
def record_email
Rails.logger.info("XYZZY: Recording Email")
@user.emails.create!
end
def spam!(user)
@user = user
Rails.logger.info("XYZZY: Sending spam!")
m = mail(to: user.email, subject: 'SPAM!')
Rails.logger.info("XYZZY: mail method finished")
m
end
end
I call this code like this (using delayed job performable mailer):
UserMailer.delay.spam!( User.find(1))
When I step through this in a debugger, it seems that my after_action method is called before the mail is delivered.
[Job:104580969] XYZZY: Sending spam!
[Job:104580969] XYZZY: mail method finished
[Job:104580969] XYZZY: Recording Email
Job UserMailer.app_registration_welcome (id=104580969) FAILED (3 prior attempts) with Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 1025
How can I catch network errors in my mailer methods and record that the email attempt failed, or do nothing at all? I'm using Rails 4.2.4.