From 5b262c75b08f2963c613cb0b17273d64a2e9eecb Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 2 Mar 2020 10:33:37 +0100 Subject: [PATCH] Follow up - ed8a152f28a18e6d070dadbba2e79e524a3b42f7 - Fixes #2911 - Performance: Improved touching assets of users, organizations and tickets. Touching of Organization causes endless SearchIndexJob creation / Organization touch loop. --- app/jobs/application_job.rb | 1 + .../application_job/has_queuing_priority.rb | 15 ++++++++++++ app/jobs/search_index_associations_job.rb | 7 ++++++ app/jobs/search_index_job.rb | 17 ++++++------- .../concerns/has_search_index_backend.rb | 24 +++++++++++++++++-- test/integration/report_test.rb | 1 + 6 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 app/jobs/application_job/has_queuing_priority.rb create mode 100644 app/jobs/search_index_associations_job.rb diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index 9c8c0ec7d..805fc0b4a 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -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 diff --git a/app/jobs/application_job/has_queuing_priority.rb b/app/jobs/application_job/has_queuing_priority.rb new file mode 100644 index 000000000..573463140 --- /dev/null +++ b/app/jobs/application_job/has_queuing_priority.rb @@ -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 diff --git a/app/jobs/search_index_associations_job.rb b/app/jobs/search_index_associations_job.rb new file mode 100644 index 000000000..c9a7e84b1 --- /dev/null +++ b/app/jobs/search_index_associations_job.rb @@ -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 diff --git a/app/jobs/search_index_job.rb b/app/jobs/search_index_job.rb index 26ed95272..a1d7053e4 100644 --- a/app/jobs/search_index_job.rb +++ b/app/jobs/search_index_job.rb @@ -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 diff --git a/app/models/concerns/has_search_index_backend.rb b/app/models/concerns/has_search_index_backend.rb index e65f60410..8250c5558 100644 --- a/app/models/concerns/has_search_index_backend.rb +++ b/app/models/concerns/has_search_index_backend.rb @@ -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 diff --git a/test/integration/report_test.rb b/test/integration/report_test.rb index 6deb899d3..de0792ae4 100644 --- a/test/integration/report_test.rb +++ b/test/integration/report_test.rb @@ -312,6 +312,7 @@ class ReportTest < ActiveSupport::TestCase # execute background jobs Scheduler.worker(true) + SearchIndexBackend.refresh end test 'compare' do