]> cat aescling's git repositories - mastodon.git/commitdiff
Fix records not being indexed sometimes (#12024)
authorEugen Rochko <eugen@zeonfederated.com>
Mon, 30 Sep 2019 23:19:11 +0000 (01:19 +0200)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2019 23:19:11 +0000 (01:19 +0200)
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.

app/models/account.rb
app/models/account_stat.rb
app/models/application_record.rb
app/models/favourite.rb
app/models/status.rb
app/models/tag.rb
config/application.rb
config/initializers/chewy.rb
lib/chewy/strategy/custom_sidekiq.rb [new file with mode: 0644]
spec/rails_helper.rb

index 55fe53fae1a66c6f12f1471d572d16decf0d35ec..01d45e36c215b4fd08fcac083be406150bfb67d3 100644 (file)
@@ -129,7 +129,7 @@ class Account < ApplicationRecord
 
   delegate :chosen_languages, to: :user, prefix: false, allow_nil: true
 
-  update_index('accounts#account', :self) if Chewy.enabled?
+  update_index('accounts#account', :self)
 
   def local?
     domain.nil?
index 6d1097cec68967d7e81b47275c07e78bc5ee043d..1351f7d8a4d42be41d8672a6e2a7e75b29b6ca01 100644 (file)
@@ -16,7 +16,7 @@
 class AccountStat < ApplicationRecord
   belongs_to :account, inverse_of: :account_stat
 
-  update_index('accounts#account', :account) if Chewy.enabled?
+  update_index('accounts#account', :account)
 
   def increment_count!(key)
     update(attributes_for_increment(key))
index c1b873da6ac05926e02ef9c1ee80c5550533db4d..5d7d3a09610a375633fde62188cfe514801b2f72 100644 (file)
@@ -5,6 +5,12 @@ class ApplicationRecord < ActiveRecord::Base
 
   include Remotable
 
+  class << self
+    def update_index(_type_name, *_args, &_block)
+      super if Chewy.enabled?
+    end
+  end
+
   def boolean_with_default(key, default_value)
     value = attributes[key]
 
index 17f8c9fa631d2a758512dff8cb458ad2e92e1505..bf0ec4449c4615c6aee68c89b5c890bd18831330 100644 (file)
@@ -13,7 +13,7 @@
 class Favourite < ApplicationRecord
   include Paginable
 
-  update_index('statuses#status', :status) if Chewy.enabled?
+  update_index('statuses#status', :status)
 
   belongs_to :account, inverse_of: :favourites
   belongs_to :status,  inverse_of: :favourites
index 5e74745775d7ffdc83c1a17b7ff24bc1c0d695b8..078a6456635713a2b14634cea5c257ab670434cf 100644 (file)
@@ -39,7 +39,7 @@ class Status < ApplicationRecord
   # will be based on current time instead of `created_at`
   attr_accessor :override_timestamps
 
-  update_index('statuses#status', :proper) if Chewy.enabled?
+  update_index('statuses#status', :proper)
 
   enum visibility: [:public, :unlisted, :private, :direct, :limited], _suffix: :visibility
 
index 9aca3983f240c555238b263c3403318f3a945dcc..82786daa81db633dd143372fd4ffab4bd6354fc6 100644 (file)
@@ -49,7 +49,7 @@ class Tag < ApplicationRecord
 
   after_save :save_account_tag_stat
 
-  update_index('tags#tag', :self) if Chewy.enabled?
+  update_index('tags#tag', :self)
 
   def account_tag_stat
     super || build_account_tag_stat
index 3ced81b8f3414a9be9b2741d3752ccd11301934b..60f73f8bb6c30175f30b186d0da67800213abaff 100644 (file)
@@ -15,6 +15,7 @@ require_relative '../lib/mastodon/snowflake'
 require_relative '../lib/mastodon/version'
 require_relative '../lib/devise/two_factor_ldap_authenticatable'
 require_relative '../lib/devise/two_factor_pam_authenticatable'
+require_relative '../lib/chewy/strategy/custom_sidekiq'
 
 Dotenv::Railtie.load
 
index d5347f2bfd0bcd049b5396489fbe42a0816f75b1..9ff0dccc1b499042ec80560fa23fcc2d6d46b3d9 100644 (file)
@@ -12,8 +12,9 @@ Chewy.settings = {
   sidekiq: { queue: 'pull' },
 }
 
-Chewy.root_strategy    = enabled ? :sidekiq : :bypass
-Chewy.request_strategy = enabled ? :sidekiq : :bypass
+Chewy.root_strategy              = :custom_sidekiq
+Chewy.request_strategy           = :custom_sidekiq
+Chewy.use_after_commit_callbacks = false
 
 module Chewy
   class << self
diff --git a/lib/chewy/strategy/custom_sidekiq.rb b/lib/chewy/strategy/custom_sidekiq.rb
new file mode 100644 (file)
index 0000000..3e54326
--- /dev/null
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Chewy
+  class Strategy
+    class CustomSidekiq < Base
+      class Worker
+        include ::Sidekiq::Worker
+
+        sidekiq_options queue: 'pull'
+
+        def perform(type, ids, options = {})
+          options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
+          type.constantize.import!(ids, options)
+        end
+      end
+
+      def update(type, objects, _options = {})
+        return unless Chewy.enabled?
+
+        ids = type.root.id ? Array.wrap(objects) : type.adapter.identify(objects)
+
+        return if ids.empty?
+
+        Worker.perform_async(type.name, ids)
+      end
+
+      def leave; end
+    end
+  end
+end
index 3a5e7491e515198888d2bc25733fab67fbec2a97..6fbceca539606821c22f4d0b7ec5688d28a63884 100644 (file)
@@ -12,7 +12,7 @@ require 'capybara/rspec'
 Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
 
 ActiveRecord::Migration.maintain_test_schema!
-WebMock.disable_net_connect!
+WebMock.disable_net_connect!(allow: Chewy.settings[:host])
 Redis.current = Redis::Namespace.new("mastodon_test#{ENV['TEST_ENV_NUMBER']}", redis: Redis.current)
 Sidekiq::Testing.inline!
 Sidekiq::Logging.logger = nil