From 3f6d9d24ce49fa863560e5b02c02d64e38e7c020 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 15 Jan 2017 13:12:56 -0800 Subject: [PATCH] Fixed retries for BulkReindexJob --- lib/searchkick/bulk_reindex_job.rb | 18 ++++++++---------- lib/searchkick/index.rb | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/searchkick/bulk_reindex_job.rb b/lib/searchkick/bulk_reindex_job.rb index b9888b4..aaa85b0 100644 --- a/lib/searchkick/bulk_reindex_job.rb +++ b/lib/searchkick/bulk_reindex_job.rb @@ -6,16 +6,14 @@ module Searchkick klass = class_name.constantize index = index_name ? Searchkick::Index.new(index_name) : klass.searchkick_index record_ids ||= min_id..max_id - Searchkick.callbacks(:bulk) do - index.import_scope( - Searchkick.load_records(klass, record_ids), - method_name: method_name, - batch: true, - batch_id: batch_id, - delete_missing: delete_missing, - record_ids: record_ids - ) - end + index.import_scope( + Searchkick.load_records(klass, record_ids), + method_name: method_name, + batch: true, + batch_id: batch_id, + delete_missing: delete_missing, + record_ids: record_ids + ) end end end diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index f6a9245..69e6bee 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -445,8 +445,12 @@ module Searchkick begin # bulk reindex - method_name ? bulk_update(records, method_name) : import(records) - bulk_delete(delete_records) + possibly_bulk do + if records.any? + method_name ? bulk_update(records, method_name) : import(records) + end + bulk_delete(delete_records) if delete_records.any? + end rescue Faraday::ClientError => e if retries < 1 retries += 1 @@ -458,6 +462,17 @@ module Searchkick end end + # use bulk if no callbacks value set + def possibly_bulk + if Searchkick.callbacks_value + yield + else + Searchkick.callbacks(:bulk) do + yield + end + end + end + def batches_key "searchkick:reindex:#{name}:batches" end -- libgit2 0.21.0