From df14c1cd53aa035d696d8f56e9566289bc34a2ba Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Tue, 30 Jul 2024 15:08:31 +0100 Subject: [PATCH] Update RequestMailer to use deliver_later Deliver emails in the background job instead of doing it inline. --- app/mailers/request_mailer.rb | 12 ++++++------ app/models/comment.rb | 2 +- app/models/info_request.rb | 6 +++--- spec/controllers/classifications_controller_spec.rb | 2 +- spec/support/email_helpers.rb | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/mailers/request_mailer.rb b/app/mailers/request_mailer.rb index 712bb4362d..3cda0a1178 100644 --- a/app/mailers/request_mailer.rb +++ b/app/mailers/request_mailer.rb @@ -325,13 +325,13 @@ def self.alert_overdue_requests overdue_alert( info_request, info_request.user - ).deliver_now + ).deliver_later elsif calculated_status == 'waiting_response_very_overdue' RequestMailer. very_overdue_alert( info_request, info_request.user - ).deliver_now + ).deliver_later else raise "unknown request status" end @@ -387,7 +387,7 @@ def self.alert_new_response_reminders_internal(days_since, type_code) new_response_reminder_alert( info_request, last_response_message - ).deliver_now + ).deliver_later store_sent.save! end end @@ -434,7 +434,7 @@ def self.alert_not_clarified_request RequestMailer.not_clarified_alert( info_request, last_response_message - ).deliver_now + ).deliver_later end store_sent.save! end @@ -508,12 +508,12 @@ def self.alert_comment_on_request info_request, count, earliest_unalerted_comment_event.comment - ).deliver_now + ).deliver_later elsif count == 1 RequestMailer.comment_on_alert( info_request, last_comment_event.comment - ).deliver_now + ).deliver_later else raise "internal error" end diff --git a/app/models/comment.rb b/app/models/comment.rb index 458f26856f..6f99d48738 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -158,7 +158,7 @@ def report!(reason, message, user) "comment: #{comment_url(self)} " \ "\nadmin: #{edit_admin_comment_url(self)}" - RequestMailer.requires_admin(info_request, user, message).deliver_now + RequestMailer.requires_admin(info_request, user, message).deliver_later info_request.log_event( 'report_comment', diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 5598591bc1..7d77ca936c 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -951,7 +951,7 @@ def receive(email, raw_email_data, *args) ) user.notify(info_request_event) else - RequestMailer.new_response(self, incoming_message).deliver_now + RequestMailer.new_response(self, incoming_message).deliver_later end end end @@ -1022,13 +1022,13 @@ def set_described_state(new_state, set_by = nil, message = "") if old_described_state != described_state && requires_admin? # Check there is someone to send the message "from" if set_by && user - RequestMailer.requires_admin(self, set_by, message).deliver_now + RequestMailer.requires_admin(self, set_by, message).deliver_later end end unless set_by.nil? || is_actual_owning_user?(set_by) || described_state == 'attention_requested' RequestMailer. - old_unclassified_updated(self).deliver_now unless is_external? + old_unclassified_updated(self).deliver_later unless is_external? end end diff --git a/spec/controllers/classifications_controller_spec.rb b/spec/controllers/classifications_controller_spec.rb index df9125c5df..9f8abf62d3 100644 --- a/spec/controllers/classifications_controller_spec.rb +++ b/spec/controllers/classifications_controller_spec.rb @@ -196,7 +196,7 @@ def post_status(status, message: nil) it 'should send an email to the requester letting them know someone has updated the status of their request' do mail_mock = double('mail') - allow(mail_mock).to receive :deliver_now + allow(mail_mock).to receive :deliver_later expect(RequestMailer).to receive(:old_unclassified_updated). and_return(mail_mock) post_status('rejected') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 57262e870c..cbce3256b4 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -82,6 +82,7 @@ def rebuild_raw_emails(info_request) end def deliveries + perform_enqueued_jobs @deliveries ||= [] @deliveries += ActionMailer::Base.deliveries ActionMailer::Base.deliveries.clear