Skip to content

Commit c498970

Browse files
Gargronhiyuki2578
authored andcommitted
Fix records not being indexed sometimes (mastodon#12024)
It's possible that after commit callbacks were not firing when exceptions occurred in the process. Also, the default Sidekiq strategy does not push indexing jobs immediately, which is not necessary and could be part of the issue too.
1 parent 5a455be commit c498970

10 files changed

Lines changed: 46 additions & 8 deletions

File tree

app/models/account.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Account < ApplicationRecord
130130

131131
delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
132132

133-
update_index('accounts#account', :self) if Chewy.enabled?
133+
update_index('accounts#account', :self)
134134

135135
def local?
136136
domain.nil?

app/models/account_stat.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class AccountStat < ApplicationRecord
1717
belongs_to :account, inverse_of: :account_stat
1818

19-
update_index('accounts#account', :account) if Chewy.enabled?
19+
update_index('accounts#account', :account)
2020

2121
def increment_count!(key)
2222
update(attributes_for_increment(key))

app/models/application_record.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ class ApplicationRecord < ActiveRecord::Base
55

66
include Remotable
77

8+
class << self
9+
def update_index(_type_name, *_args, &_block)
10+
super if Chewy.enabled?
11+
end
12+
end
13+
814
def boolean_with_default(key, default_value)
915
value = attributes[key]
1016

app/models/favourite.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Favourite < ApplicationRecord
1414
include Paginable
1515

16-
update_index('statuses#status', :status) if Chewy.enabled?
16+
update_index('statuses#status', :status)
1717

1818
belongs_to :account, inverse_of: :favourites
1919
belongs_to :status, inverse_of: :favourites

app/models/status.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Status < ApplicationRecord
3939
# will be based on current time instead of `created_at`
4040
attr_accessor :override_timestamps
4141

42-
update_index('statuses#status', :proper) if Chewy.enabled?
42+
update_index('statuses#status', :proper)
4343

4444
enum visibility: [:public, :unlisted, :private, :direct, :limited], _suffix: :visibility
4545

app/models/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Tag < ApplicationRecord
4949

5050
after_save :save_account_tag_stat
5151

52-
update_index('tags#tag', :self) if Chewy.enabled?
52+
update_index('tags#tag', :self)
5353

5454
def account_tag_stat
5555
super || build_account_tag_stat

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require_relative '../lib/mastodon/version'
1616
require_relative '../lib/devise/two_factor_ldap_authenticatable'
1717
require_relative '../lib/devise/two_factor_pam_authenticatable'
18+
require_relative '../lib/chewy/strategy/custom_sidekiq'
1819

1920
Dotenv::Railtie.load
2021

config/initializers/chewy.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
sidekiq: { queue: 'pull' },
1313
}
1414

15-
Chewy.root_strategy = enabled ? :sidekiq : :bypass
16-
Chewy.request_strategy = enabled ? :sidekiq : :bypass
15+
Chewy.root_strategy = :custom_sidekiq
16+
Chewy.request_strategy = :custom_sidekiq
17+
Chewy.use_after_commit_callbacks = false
1718

1819
module Chewy
1920
class << self
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module Chewy
4+
class Strategy
5+
class CustomSidekiq < Base
6+
class Worker
7+
include ::Sidekiq::Worker
8+
9+
sidekiq_options queue: 'pull'
10+
11+
def perform(type, ids, options = {})
12+
options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
13+
type.constantize.import!(ids, options)
14+
end
15+
end
16+
17+
def update(type, objects, _options = {})
18+
return unless Chewy.enabled?
19+
20+
ids = type.root.id ? Array.wrap(objects) : type.adapter.identify(objects)
21+
22+
return if ids.empty?
23+
24+
Worker.perform_async(type.name, ids)
25+
end
26+
27+
def leave; end
28+
end
29+
end
30+
end

spec/rails_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
1313

1414
ActiveRecord::Migration.maintain_test_schema!
15-
WebMock.disable_net_connect!
15+
WebMock.disable_net_connect!(allow: Chewy.settings[:host])
1616
Redis.current = Redis::Namespace.new("mastodon_test#{ENV['TEST_ENV_NUMBER']}", redis: Redis.current)
1717
Sidekiq::Testing.inline!
1818
Sidekiq::Logging.logger = nil

0 commit comments

Comments
 (0)