Skip to content

Commit

Permalink
Merge pull request #23037 from agrare/add_generic_mailer_deliver!
Browse files Browse the repository at this point in the history
Add a `deliver!` version of `GenericMailer.deliver`

(cherry picked from commit 671a04b)
  • Loading branch information
Fryguy committed May 24, 2024
1 parent 8d5cd76 commit 719adb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
30 changes: 13 additions & 17 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 All @@ -56,7 +52,7 @@ def self.deliver_queue(method, options = {}, queue_options = {})
queue_options.reverse_merge(
:service => "notifier",
:class_name => name,
:method_name => 'deliver',
:method_name => 'deliver!',
:args => [method, options]
)
)
Expand Down
12 changes: 6 additions & 6 deletions spec/mailers/generic_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
expect(BinaryBlob.count).to eq(0)
GenericMailer.deliver_queue(:generic_notification, @args)
expect(BinaryBlob.count).to eq(1)
expect(MiqQueue.exists?(:method_name => 'deliver',
expect(MiqQueue.exists?(:method_name => 'deliver!',
:class_name => described_class.name,
:role => 'notifier')).to be_truthy
end

it "with automation_notification" do
GenericMailer.deliver_queue(:automation_notification, @args)
expect(MiqQueue.exists?(:method_name => 'deliver',
expect(MiqQueue.exists?(:method_name => 'deliver!',
:class_name => described_class.name,
:role => 'notifier')).to be_truthy
end
Expand All @@ -48,7 +48,7 @@
:message => "Queued the action: [generic_notification]"
)
expect(BinaryBlob.count).to eq(1)
expect(MiqQueue.exists?(:method_name => 'deliver',
expect(MiqQueue.exists?(:method_name => 'deliver!',
:class_name => described_class.name,
:role => 'notifier')).to be_truthy
end
Expand All @@ -60,7 +60,7 @@
:status => MiqTask::STATUS_OK,
:message => "Queued the action: [automation_notification]"
)
expect(MiqQueue.exists?(:method_name => 'deliver',
expect(MiqQueue.exists?(:method_name => 'deliver!',
:class_name => described_class.name,
:role => 'notifier')).to be_truthy
end
Expand Down 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 719adb5

Please sign in to comment.