Skip to content

Commit

Permalink
Maintenance: Does skip the job creation if no changes happened and fi…
Browse files Browse the repository at this point in the history
…xes missing search index update (zammad#4306).
  • Loading branch information
rolfschmidt committed Oct 19, 2022
1 parent a45293e commit 886cd32
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
24 changes: 3 additions & 21 deletions app/models/concerns/has_search_index_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ module HasSearchIndexBackend
extend ActiveSupport::Concern

included do
after_create :search_index_update
after_update :search_index_update
after_touch :search_index_update_touch
after_commit :search_index_update, if: :persisted?
after_destroy :search_index_destroy
end

Expand All @@ -25,26 +23,10 @@ def search_index_update
# start background job to transfer data to search index
return true if !SearchIndexBackend.enabled?

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?
return true if previous_changes.blank?

SearchIndexJob.perform_later(self.class.to_s, id)
SearchIndexAssociationsJob.perform_later(self.class.to_s, id)
true
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/ticket/enqueues_user_ticket_counter_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def enqueue_user_ticket_counter_job

return true if !customer_id

return true if previous_changes.blank?

# send background job
TicketUserTicketCounterJob.perform_later(
customer_id,
Expand Down
2 changes: 2 additions & 0 deletions app/models/ticket/touches_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def ticket_touch_associations
# return if we run import mode
return true if Setting.get('import_mode')

return true if saved_changes.blank?

# touch old customer if changed
customer_id_changed = saved_changes['customer_id']
if customer_id_changed && customer_id_changed[0] != customer_id_changed[1] && customer_id_changed[0]
Expand Down
25 changes: 19 additions & 6 deletions spec/models/ticket/has_search_index_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
require 'rails_helper'

RSpec.describe 'HasSearchIndexBackend', type: :model, searchindex: true, performs_jobs: true do
before do
article && organization

searchindex_model_reload([::Ticket, ::Organization])
end

describe 'Updating referenced data between ticket and organizations' do
let(:organization) { create(:organization, name: 'Tomato42') }
let(:user) { create(:customer, organization: organization) }
Expand All @@ -25,6 +19,12 @@
article
end

before do
article && organization

searchindex_model_reload([::Ticket, ::Organization])
end

it 'finds added tickets' do
result = SearchIndexBackend.search('organization.name:Tomato42', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
Expand Down Expand Up @@ -81,4 +81,17 @@
expect(Ticket).not_to be_search_index_attribute_relevant('updated_by_id')
end
end

describe 'Updating group settings causes huge numbers of delayed jobs #4306' do
let(:ticket) { create(:ticket) }

before do
ticket
Delayed::Job.destroy_all
end

it 'does not create any jobs if nothing has changed' do
expect { ticket.update(title: ticket.title) }.not_to change(Delayed::Job, :count)
end
end
end
25 changes: 19 additions & 6 deletions spec/models/user/has_search_index_backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
require 'rails_helper'

RSpec.describe 'HasSearchIndexBackend', type: :model, searchindex: true do
before do
user && organization

searchindex_model_reload([::User, ::Organization])
end

describe 'Updating referenced data between user and organizations' do
let(:organization) { create(:organization, name: 'Tomato42') }
let(:user) { create(:customer, organization: organization) }

before do
user && organization

searchindex_model_reload([::User, ::Organization])
end

it 'finds added users' do
result = SearchIndexBackend.search('organization.name:Tomato42', 'User', sort_by: ['updated_at'], order_by: ['desc'])
expect(result).to eq([{ id: user.id.to_s, type: 'User' }])
Expand Down Expand Up @@ -40,4 +40,17 @@
expect(organization).to be_search_index_indexable_bulk_updates(User)
end
end

describe 'Updating group settings causes huge numbers of delayed jobs #4306' do
let(:user) { create(:user) }

before do
user
Delayed::Job.destroy_all
end

it 'does not create any jobs if nothing has changed' do
expect { user.update(firstname: user.firstname) }.not_to change(Delayed::Job, :count)
end
end
end

0 comments on commit 886cd32

Please sign in to comment.