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.
This commit is contained in:
Robin Ward 2018-10-01 01:15:45 +08:00 committed by GitHub
parent 75c77c7e7e
commit 02da022c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View File

@ -6,6 +6,10 @@ module Jobs
sidekiq_options queue: 'critical' sidekiq_options queue: 'critical'
def quit_email_early?
false
end
def execute(args) def execute(args)
super(args) super(args)
end end

View File

@ -9,10 +9,20 @@ module Jobs
sidekiq_options queue: 'low' 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) def execute(args)
raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present? raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present?
raise Discourse::InvalidParameters.new(:type) unless args[:type].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 post = nil
notification = nil notification = nil
type = args[:type] type = args[:type]

View File

@ -58,6 +58,26 @@ describe Jobs::UserEmail do
end end
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 context "recently seen" do
let(:post) { Fabricate(:post, user: user) } let(:post) { Fabricate(:post, user: user) }