FEATURE: prioritize sidekiq jobs

This commit introduces 3 queues for sidekiq

"critical" for urgent jobs (weighted at 4x weight)
"default" for standard jobs(weighted at 2x weight)
"low" for less important jobs


"critical jobs"

Reset Password emails has been seperated to its own job
Heartbeat which is required to keep sidekiq running
Test email which needs to return real quick


"low priority jobs"

Notify mailing list
Pull hotlinked images
Update gravatar

"default"

All the rest

Note: for people running sidekiq from command line use

bin/sidekiq -q critical,4 -q default,2 -q low
This commit is contained in:
Sam 2016-04-07 12:56:43 +10:00
parent a5d8dfb07e
commit 8ec7fd84fd
10 changed files with 28 additions and 4 deletions

View File

@ -204,7 +204,7 @@ class SessionController < ApplicationController
user_presence = user.present? && user.id != Discourse::SYSTEM_USER_ID && !user.staged user_presence = user.present? && user.id != Discourse::SYSTEM_USER_ID && !user.staged
if user_presence if user_presence
email_token = user.email_tokens.create(email: user.email) email_token = user.email_tokens.create(email: user.email)
Jobs.enqueue(:user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token) Jobs.enqueue(:forgot_password, user_id: user.id, email_token: email_token.token)
end end
json = { result: "ok" } json = { result: "ok" }

View File

@ -262,6 +262,5 @@ module Jobs
end end
end end
# Require all jobs
Dir["#{Rails.root}/app/jobs/regular/*.rb"].each {|file| require_dependency file } Dir["#{Rails.root}/app/jobs/regular/*.rb"].each {|file| require_dependency file }
Dir["#{Rails.root}/app/jobs/scheduled/*.rb"].each {|file| require_dependency file } Dir["#{Rails.root}/app/jobs/scheduled/*.rb"].each {|file| require_dependency file }

View File

@ -0,0 +1,14 @@
# base.rb uses this style of require, so maintain usage of it here
require_dependency "#{Rails.root}/app/jobs/regular/user_email.rb"
module Jobs
class ForgotPassword < UserEmail
sidekiq_options queue: 'critical'
def execute(args)
args[:type] = :forgot_password
super(args)
end
end
end

View File

@ -2,6 +2,8 @@ module Jobs
class NotifyMailingListSubscribers < Jobs::Base class NotifyMailingListSubscribers < Jobs::Base
sidekiq_options queue: 'low'
def execute(args) def execute(args)
return if SiteSetting.disable_mailing_list_mode return if SiteSetting.disable_mailing_list_mode

View File

@ -4,6 +4,9 @@ require_dependency 'file_helper'
module Jobs module Jobs
class PullHotlinkedImages < Jobs::Base class PullHotlinkedImages < Jobs::Base
sidekiq_options queue: 'low'
def initialize def initialize
# maximum size of the file in bytes # maximum size of the file in bytes
@max_size = SiteSetting.max_image_size_kb.kilobytes @max_size = SiteSetting.max_image_size_kb.kilobytes

View File

@ -1,6 +1,8 @@
module Jobs module Jobs
class RunHeartbeat < Jobs::Base class RunHeartbeat < Jobs::Base
sidekiq_options queue: 'critical'
def self.heartbeat_key def self.heartbeat_key
'heartbeat_last_run' 'heartbeat_last_run'
end end

View File

@ -5,6 +5,8 @@ module Jobs
# Asynchronously send an email # Asynchronously send an email
class TestEmail < Jobs::Base class TestEmail < Jobs::Base
sidekiq_options queue: 'critical'
def execute(args) def execute(args)
raise Discourse::InvalidParameters.new(:to_address) unless args[:to_address].present? raise Discourse::InvalidParameters.new(:to_address) unless args[:to_address].present?

View File

@ -2,6 +2,8 @@ module Jobs
class UpdateGravatar < Jobs::Base class UpdateGravatar < Jobs::Base
sidekiq_options queue: 'low'
def execute(args) def execute(args)
user = User.find_by(id: args[:user_id]) user = User.find_by(id: args[:user_id])
avatar = UserAvatar.find_by(id: args[:avatar_id]) avatar = UserAvatar.find_by(id: args[:avatar_id])

View File

@ -25,7 +25,7 @@ class Demon::Sidekiq < Demon::Base
# parent process is in charge of the file anyway. # parent process is in charge of the file anyway.
Sidekiq::Logging.logger = nil Sidekiq::Logging.logger = nil
cli = Sidekiq::CLI.instance cli = Sidekiq::CLI.instance
cli.parse(["-c", GlobalSetting.sidekiq_workers.to_s]) cli.parse(["-c", GlobalSetting.sidekiq_workers.to_s, "-q", "critical,4", "-q", "default,2", "-q", "low"])
load Rails.root + "config/initializers/100-sidekiq.rb" load Rails.root + "config/initializers/100-sidekiq.rb"
cli.run cli.run

View File

@ -632,7 +632,7 @@ describe SessionController do
end end
it "enqueues an email" do it "enqueues an email" do
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :forgot_password, user_id: user.id)) Jobs.expects(:enqueue).with(:forgot_password, has_entries(user_id: user.id))
xhr :post, :forgot_password, login: user.username xhr :post, :forgot_password, login: user.username
end end
end end