Follow up - ed8a152f28 - Fixes #2911 - Performance: Improved touching assets of users, organizations and tickets. Touching of Organization causes endless SearchIndexJob creation / Organization touch loop.

This commit is contained in:
Thorsten Eckel 2020-03-02 10:33:37 +01:00
parent 5bacbadf2f
commit 5b262c75b0
6 changed files with 55 additions and 10 deletions

View file

@ -1,5 +1,6 @@
class ApplicationJob < ActiveJob::Base
include ApplicationJob::HasDelayedJobMonitoringCompatibilty
include ApplicationJob::HasQueuingPriority
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

View file

@ -0,0 +1,15 @@
class ApplicationJob
module HasQueuingPriority
extend ActiveSupport::Concern
included do
queue_with_priority 200
end
class_methods do
def low_priority
queue_with_priority 300
end
end
end
end

View file

@ -0,0 +1,7 @@
class SearchIndexAssociationsJob < SearchIndexJob
def update_search_index(record)
record.search_index_update_associations_delta
record.search_index_update_associations_full
end
end

View file

@ -1,28 +1,29 @@
class SearchIndexJob < ApplicationJob
include HasActiveJobLock
low_priority
retry_on StandardError, attempts: 20, wait: lambda { |executions|
executions * 10.seconds
}
def lock_key
# "SearchIndexJob/User/42/true"
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}/#{arguments[2]}"
# "SearchIndexJob/User/42"
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
end
def perform(object, o_id, update_associations = true)
def perform(object, o_id)
@object = object
@o_id = o_id
record = @object.constantize.lookup(id: @o_id)
return if !exists?(record)
update_search_index(record)
end
def update_search_index(record)
record.search_index_update_backend
return if !update_associations
record.search_index_update_associations_delta
record.search_index_update_associations_full
end
private

View file

@ -5,7 +5,7 @@ module HasSearchIndexBackend
included do
after_create :search_index_update
after_update :search_index_update
after_touch :search_index_update
after_touch :search_index_update_touch
after_destroy :search_index_destroy
end
@ -24,6 +24,26 @@ update search index, if configured - will be executed automatically
# start background job to transfer data to search index
return true if !SearchIndexBackend.enabled?
SearchIndexJob.perform_later(self.class.to_s, id)
SearchIndexAssociationsJob.perform_later(self.class.to_s, id)
true
end
=begin
update search index, if configured - will be executed automatically
model = Model.find(123)
model.search_index_update_touch
=end
def search_index_update_touch
return true if ignore_search_indexing?(:update)
# start background job to transfer data to search index
return true if !SearchIndexBackend.enabled?
SearchIndexJob.perform_later(self.class.to_s, id)
true
end
@ -49,7 +69,7 @@ returns
# we can not use the delta function for this because of the excluded
# ticket article attachments. see explain in delta function
Ticket.select('id').where(organization_id: id).order(id: :desc).limit(10_000).pluck(:id).each do |ticket_id|
SearchIndexJob.perform_later('Ticket', ticket_id, false)
SearchIndexJob.perform_later('Ticket', ticket_id)
end
end

View file

@ -312,6 +312,7 @@ class ReportTest < ActiveSupport::TestCase
# execute background jobs
Scheduler.worker(true)
SearchIndexBackend.refresh
end
test 'compare' do