A platform for community discussion. Free, open, simple.
DEV: Have group smtp email job retry (#31202)
Blake Ericksonbc29fbeac8cc
diff --git a/app/jobs/regular/group_smtp_email.rb b/app/jobs/regular/group_smtp_email.rbindex 968bcf45406..c70b49a1709 100644--- a/app/jobs/regular/group_smtp_email.rb+++ b/app/jobs/regular/group_smtp_email.rb@@ -84,6 +84,9 @@ module Jobsbcc_addresses: bcc_addresses,)+ # Idempotency check – if the EmailLog already exists, do not send again.+ return if EmailLog.exists?(message_id: message.message_id)+beginEmail::Sender.new(message, :group_smtp, recipient_user).sendrescue Net::ReadTimeout => err@@ -95,6 +98,7 @@ module Jobsmessage: "Got SMTP read timeout when sending group SMTP email",env: args,)+ raise err # Re-raise the error so Sidekiq's retry mechanism kicks in.end# Create an incoming email record to avoid importing again from IMAPdiff --git a/spec/jobs/regular/group_smtp_email_spec.rb b/spec/jobs/regular/group_smtp_email_spec.rbindex b5ec592a74a..ae96cdb5dc5 100644--- a/spec/jobs/regular/group_smtp_email_spec.rb+++ b/spec/jobs/regular/group_smtp_email_spec.rb@@ -191,12 +191,6 @@ RSpec.describe Jobs::GroupSmtpEmail doexpect { Jobs.enqueue(:group_smtp_email, **args) }.not_to raise_errorend- it "does not retry the job on SMTP read timeouts, because we can't be sure if the send actually failed or if ENTER . ENTER just timed out" do- Jobs.run_immediately!- Email::Sender.any_instance.expects(:send).raises(Net::ReadTimeout)- expect { Jobs.enqueue(:group_smtp_email, **args) }.not_to raise_error- end-context "when there are cc_addresses" doit "has the cc_addresses and cc_user_ids filled in correctly" dojob.execute(args)@@ -322,4 +316,38 @@ RSpec.describe Jobs::GroupSmtpEmail do).to eq(true)endend++ it "re-raises Net::ReadTimeout to trigger Sidekiq retries" do+ allow_any_instance_of(Email::Sender).to receive(:send).and_raise(+ Net::ReadTimeout.new("timeout"),+ )+ expect { job.execute(args) }.to raise_error(Net::ReadTimeout)+ end++ it "does not send email if an EmailLog with the same message_id already exists" do+ message = Mail::Message.new(body: "hello", to: "[email protected]")+ message.message_id = "unique_message_id"+ GroupSmtpMailer+ .expects(:send_mail)+ .with(+ group,+ "[email protected]",+ post,+ cc_addresses: %w[[email protected] [email protected]],+ bcc_addresses: [],+ )+ .returns(message)+ EmailLog.expects(:exists?).with(message_id: message.message_id).returns(true)+ Email::Sender.any_instance.expects(:send).never++ job.execute(args)+ expect(ActionMailer::Base.deliveries.count).to eq(0)+ end++ it "retries the job on SMTP read timeouts when there are no duplicates" do+ Jobs.run_immediately!+ EmailLog.stubs(:exists?).with(message_id: anything).returns(false)+ Email::Sender.any_instance.expects(:send).raises(Net::ReadTimeout)+ expect { Jobs.enqueue(:group_smtp_email, **args) }.to raise_error(Net::ReadTimeout)+ endend