PERF: Update index for category in a background job.

Search indexing can get expensive and there is no need for us to block
the entire request just to wait for index to finish.
This commit is contained in:
Guo Xiang Tan 2020-11-09 13:35:37 +08:00 committed by Alan Guo Xiang Tan
parent 94cd5ac0b1
commit 650da7b626
4 changed files with 16 additions and 5 deletions

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
class Jobs::IndexCategoryForSearch < Jobs::Base
def execute(args)
category = Category.find_by(id: args[:category_id])
raise Discourse::InvalidParameters.new(:category_id) if category.blank?
SearchIndexer.index(category, force: args[:force] || false)
end
end

View File

@ -778,11 +778,10 @@ class Category < ActiveRecord::Base
end
def index_search
if saved_change_to_attribute?(:name)
SearchIndexer.queue_category_posts_reindex(self.id)
end
SearchIndexer.index(self)
Jobs.enqueue(:index_category_for_search,
category_id: self.id,
force: saved_change_to_attribute?(:name),
)
end
def update_reviewables

View File

@ -243,6 +243,7 @@ class SearchIndexer
end
if Category === obj && (obj.saved_change_to_name? || force)
SearchIndexer.queue_category_posts_reindex(obj.id)
SearchIndexer.update_categories_index(obj.id, obj.name)
end

View File

@ -8,6 +8,7 @@ describe Search do
before do
SearchIndexer.enable
Jobs.run_immediately!
end
context 'post indexing' do