From 02da022c7088269a7075e5e62ce461bb7691046e Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 1 Oct 2018 01:15:45 +0800 Subject: [PATCH] PERF: Quit out of the email job quickly if disabled (#6423) This prevents sidekiq from doing a bunch of queries when email is disabled. Critical emails are a special case and will be sent. --- app/jobs/regular/critical_user_email.rb | 4 ++++ app/jobs/regular/user_email.rb | 10 ++++++++++ spec/jobs/user_email_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/app/jobs/regular/critical_user_email.rb b/app/jobs/regular/critical_user_email.rb index 4add2d7db30..2ad08e35b59 100644 --- a/app/jobs/regular/critical_user_email.rb +++ b/app/jobs/regular/critical_user_email.rb @@ -6,6 +6,10 @@ module Jobs sidekiq_options queue: 'critical' + def quit_email_early? + false + end + def execute(args) super(args) end diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb index be2a5de02c1..cfe8bf96339 100644 --- a/app/jobs/regular/user_email.rb +++ b/app/jobs/regular/user_email.rb @@ -9,10 +9,20 @@ module Jobs sidekiq_options queue: 'low' + # Can be overridden by subclass, for example critical email + # should always consider being sent + def quit_email_early? + SiteSetting.disable_emails == 'yes' + end + def execute(args) raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present? raise Discourse::InvalidParameters.new(:type) unless args[:type].present? + # This is for performance. Quit out fast without doing a bunch + # of extra work when emails are disabled. + return if quit_email_early? + post = nil notification = nil type = args[:type] diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb index 202d2df331d..299aaba6c93 100644 --- a/spec/jobs/user_email_spec.rb +++ b/spec/jobs/user_email_spec.rb @@ -58,6 +58,26 @@ describe Jobs::UserEmail do end end + context "disable_emails setting" do + it "sends when no" do + SiteSetting.disable_emails = 'no' + Email::Sender.any_instance.expects(:send).once + Jobs::UserEmail.new.execute(type: :confirm_new_email, user_id: user.id) + end + + it "does not send an email when yes" do + SiteSetting.disable_emails = 'yes' + Email::Sender.any_instance.expects(:send).never + Jobs::UserEmail.new.execute(type: :confirm_new_email, user_id: user.id) + end + + it "sends when critical" do + SiteSetting.disable_emails = 'yes' + Email::Sender.any_instance.expects(:send) + Jobs::CriticalUserEmail.new.execute(type: :confirm_new_email, user_id: user.id) + end + end + context "recently seen" do let(:post) { Fabricate(:post, user: user) }