From 910ff73552941f1f4c4769c166fca77f2d8b6679 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 15 Jan 2017 21:01:39 -0800 Subject: [PATCH] Added support for async reindexing for Mongoid and NoBrainer --- lib/searchkick/index.rb | 31 ++++++++++++++++++++++--------- test/reindex_test.rb | 9 ++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 17e9c5f..de9738e 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -267,17 +267,21 @@ module Searchkick batches_count.times do |i| batch_id = i + 1 min_id = starting_id + (i * batch_size) - Searchkick::BulkReindexJob.perform_later( - class_name: scope.model_name.name, - min_id: min_id, - max_id: min_id + batch_size - 1, - index_name: name, - batch_id: batch_id - ) - redis.sadd(batches_key, batch_id) if redis + bulk_reindex_job scope, batch_id, min_id: min_id, max_id: min_id + batch_size - 1 end else - raise Searchkick::Error, "async option only supported for ActiveRecord" + batch_id = 1 + items = [] + scope = scope.only(:_id) if scope.respond_to?(:only) + scope.each do |item| + items << item + if items.length == batch_size + bulk_reindex_job scope, batch_id, record_ids: items.map { |i| i.id.to_s } + items = [] + batch_id += 1 + end + end + bulk_reindex_job scope, batch_id, record_ids: items.map { |i| i.id.to_s } end elsif scope.respond_to?(:find_in_batches) if resume @@ -442,6 +446,15 @@ module Searchkick end end + def bulk_reindex_job(scope, batch_id, options) + Searchkick::BulkReindexJob.perform_later({ + class_name: scope.model_name.name, + index_name: name, + batch_id: batch_id + }.merge(options)) + redis.sadd(batches_key, batch_id) if redis + end + def with_retries retries = 0 diff --git a/test/reindex_test.rb b/test/reindex_test.rb index 5d934cb..f779eab 100644 --- a/test/reindex_test.rb +++ b/test/reindex_test.rb @@ -1,12 +1,9 @@ require_relative "test_helper" class ReindexTest < Minitest::Test - def setup + def test_scoped skip if nobrainer? - super - end - def test_scoped store_names ["Product A"] Searchkick.callbacks(false) do store_names ["Product B", "Product C"] @@ -16,6 +13,8 @@ class ReindexTest < Minitest::Test end def test_associations + skip if nobrainer? + store_names ["Product A"] store = Store.create!(name: "Test") Product.create!(name: "Product B", store_id: store.id) @@ -24,7 +23,7 @@ class ReindexTest < Minitest::Test end def test_async - skip unless defined?(ActiveJob) && defined?(ActiveRecord) + skip if !defined?(ActiveJob) Searchkick.callbacks(false) do store_names ["Product A"] -- libgit2 0.21.0