PERF: Defer indexing post for search when saving a post.

Indexing a post for search is slow and there is no reason for us to have
to block saving a post due to search indexing.
This commit is contained in:
Guo Xiang Tan 2020-08-21 07:52:43 +08:00
parent f4a0d200ee
commit 337f062f0f
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 6 additions and 4 deletions

View File

@ -57,7 +57,7 @@ class Post < ActiveRecord::Base
validates_with PostValidator, unless: :skip_validation
after_save :index_search
after_commit :index_search
# We can pass several creating options to a post via attributes
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check, :skip_validation
@ -898,7 +898,9 @@ class Post < ActiveRecord::Base
end
def index_search
SearchIndexer.index(self)
Scheduler::Defer.later "Index post for search" do
SearchIndexer.index(self)
end
end
def locked?

View File

@ -201,9 +201,9 @@ class SearchIndexer
if Post === obj && obj.raw.present? &&
(
force ||
obj.saved_change_to_cooked? ||
obj.saved_change_to_topic_id? ||
force
obj.saved_change_to_topic_id?
)
if topic