Skip to content

Commit

Permalink
Add a deliver! version of GenericMailer.deliver
Browse files Browse the repository at this point in the history
Add a version of `GenericMailer.deliver` which raises exceptions on
failure.
  • Loading branch information
agrare committed May 15, 2024
1 parent 9c9dd1c commit 9beea07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
28 changes: 12 additions & 16 deletions app/mailers/generic_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,46 @@
class GenericMailer < ActionMailer::Base
include Vmdb::Logging

def self.deliver(method, options = {})
def self.deliver!(method, options = {})
_log.info("starting: method: #{method} options: #{options} ")
options[:attachment] &&= blob_to_attachment(options[:attachment])
options[:sent_on] = Time.now

msg = send(method, options)
begin
msg.deliver_now

# catch delivery errors if raised,
rescue Net::SMTPError => e
msg.deliver_now!
rescue Net::SMTPError => e # catch delivery errors if raised
invalid = []

# attempt to resend message to recipients individually
rcpts = [msg.to].flatten
rcpts.each do |rcpt|
rcpt.split(',').each do |to|
options[:to] = to
individual = send(method, options)
begin
individual.deliver_now
individual.deliver_now!
rescue Net::SMTPError
invalid << to
end
end
end

_log.error("method: #{method} options: #{options} delivery-error #{e} recipients #{invalid}")

# connection errors, and other if raised
rescue => e
rescue => e # connection errors, and other if raised
_log.error("method: #{method} delivery-error: #{e} attempting to resend")

# attempt to deliver one more time
begin
msg.deliver_now
rescue => e
_log.error("method: #{method} options: #{options} delivery-error #{e}")
end
msg.deliver_now!
end

msg
end

def self.deliver(method, options = {})
deliver!(method, options)
rescue => e # catch delivery errors if raised,
_log.error("method: #{method} options: #{options} delivery-error #{e}")
end

def self.deliver_queue(method, options = {}, queue_options = {})
return unless MiqRegion.my_region.role_assigned?('notifier')

Expand Down
4 changes: 2 additions & 2 deletions spec/mailers/generic_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
# raises error when delivered
msg = @args.merge(:to => 'me@bedrock.gov, you@bedrock.gov')
notification = GenericMailer.generic_notification(msg)
allow(notification).to receive(:deliver_now).and_raise(Net::SMTPFatalError.new("fake_response"))
allow(notification).to receive(:deliver_now!).and_raise(Net::SMTPFatalError.new("fake_response"))

# send error msg first...
expect(GenericMailer)
Expand All @@ -106,7 +106,7 @@
.and_call_original

# send message
GenericMailer.deliver(:generic_notification)
GenericMailer.deliver(:generic_notification, msg)

# ensure individual messages were sent
expect(ActionMailer::Base.deliveries.size).to eq(2)
Expand Down

0 comments on commit 9beea07

Please sign in to comment.