Commit e20ac49685322b75606ed2055a1856aa90aef413

Authored by Andrew Kane
1 parent 85923349

Added safety check for multiple Model.reindex - #1316 #1351

Showing 2 changed files with 20 additions and 0 deletions   Show diff stats
1 ## 4.1.2 (unreleased) 1 ## 4.1.2 (unreleased)
2 2
  3 +- Added safety check for multiple `Model.reindex`
3 - Added `deep_paging` option 4 - Added `deep_paging` option
4 5
5 ## 4.1.1 (2019-11-19) 6 ## 4.1.1 (2019-11-19)
lib/searchkick/index.rb
@@ -249,6 +249,11 @@ module Searchkick @@ -249,6 +249,11 @@ module Searchkick
249 end 249 end
250 end 250 end
251 251
  252 + # private
  253 + def uuid
  254 + settings.values.first["settings"]["index"]["uuid"]
  255 + end
  256 +
252 protected 257 protected
253 258
254 def client 259 def client
@@ -285,6 +290,8 @@ module Searchkick @@ -285,6 +290,8 @@ module Searchkick
285 scope: scope 290 scope: scope
286 } 291 }
287 292
  293 + uuid = index.uuid
  294 +
288 # check if alias exists 295 # check if alias exists
289 alias_exists = alias_exists? 296 alias_exists = alias_exists?
290 if alias_exists 297 if alias_exists
@@ -292,6 +299,7 @@ module Searchkick @@ -292,6 +299,7 @@ module Searchkick
292 299
293 # get existing indices to remove 300 # get existing indices to remove
294 unless async 301 unless async
  302 + check_uuid(uuid, index.uuid)
295 promote(index.name, update_refresh_interval: !refresh_interval.nil?) 303 promote(index.name, update_refresh_interval: !refresh_interval.nil?)
296 clean_indices unless retain 304 clean_indices unless retain
297 end 305 end
@@ -316,6 +324,7 @@ module Searchkick @@ -316,6 +324,7 @@ module Searchkick
316 # already promoted if alias didn't exist 324 # already promoted if alias didn't exist
317 if alias_exists 325 if alias_exists
318 puts "Jobs complete. Promoting..." 326 puts "Jobs complete. Promoting..."
  327 + check_uuid(uuid, index.uuid)
319 promote(index.name, update_refresh_interval: !refresh_interval.nil?) 328 promote(index.name, update_refresh_interval: !refresh_interval.nil?)
320 end 329 end
321 clean_indices unless retain 330 clean_indices unless retain
@@ -334,5 +343,15 @@ module Searchkick @@ -334,5 +343,15 @@ module Searchkick
334 343
335 raise e 344 raise e
336 end 345 end
  346 +
  347 + # safety check
  348 + # still a chance for race condition since its called before promotion
  349 + # ideal is for user to disable automatic index creation
  350 + # https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
  351 + def check_uuid(old_uuid, new_uuid)
  352 + if old_uuid != new_uuid
  353 + raise Searchkick::Error, "Safety check failed - only run one Model.reindex per model at a time"
  354 + end
  355 + end
337 end 356 end
338 end 357 end