From e20ac49685322b75606ed2055a1856aa90aef413 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 16 Dec 2019 14:53:10 -0800 Subject: [PATCH] Added safety check for multiple Model.reindex - #1316 #1351 --- CHANGELOG.md | 1 + lib/searchkick/index.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 0 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be6349a..cbdf476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 4.1.2 (unreleased) +- Added safety check for multiple `Model.reindex` - Added `deep_paging` option ## 4.1.1 (2019-11-19) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 88d22dc..c3c7467 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -249,6 +249,11 @@ module Searchkick end end + # private + def uuid + settings.values.first["settings"]["index"]["uuid"] + end + protected def client @@ -285,6 +290,8 @@ module Searchkick scope: scope } + uuid = index.uuid + # check if alias exists alias_exists = alias_exists? if alias_exists @@ -292,6 +299,7 @@ module Searchkick # get existing indices to remove unless async + check_uuid(uuid, index.uuid) promote(index.name, update_refresh_interval: !refresh_interval.nil?) clean_indices unless retain end @@ -316,6 +324,7 @@ module Searchkick # already promoted if alias didn't exist if alias_exists puts "Jobs complete. Promoting..." + check_uuid(uuid, index.uuid) promote(index.name, update_refresh_interval: !refresh_interval.nil?) end clean_indices unless retain @@ -334,5 +343,15 @@ module Searchkick raise e end + + # safety check + # still a chance for race condition since its called before promotion + # ideal is for user to disable automatic index creation + # https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation + def check_uuid(old_uuid, new_uuid) + if old_uuid != new_uuid + raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time" + end + end end end -- libgit2 0.21.0