Commit 083720616dc9ea847b028abb1a47de3b4c2b2503

Authored by Andrew Kane
1 parent 7b6be02b

Added wait option to reindex sync

CHANGELOG.md
... ... @@ -2,6 +2,8 @@
2 2  
3 3 - Added `_all` and `default_fields` options
4 4 - Added global `index_prefix` option
  5 +- Added `wait` option to async reindex
  6 +- Raise error for `reindex_status` when Redis not configured
5 7  
6 8 ## 2.3.1
7 9  
... ...
lib/searchkick.rb
... ... @@ -153,6 +153,8 @@ module Searchkick
153 153 completed: batches_left == 0,
154 154 batches_left: batches_left
155 155 }
  156 + else
  157 + raise Searchkick::Error, "Redis not configured"
156 158 end
157 159 end
158 160  
... ...
lib/searchkick/index.rb
... ... @@ -228,7 +228,8 @@ module Searchkick
228 228 end
229 229  
230 230 # check if alias exists
231   - if alias_exists?
  231 + alias_exists = alias_exists?
  232 + if alias_exists
232 233 # import before promotion
233 234 index.import_scope(scope, resume: resume, async: async, full: true) if import
234 235  
... ... @@ -246,6 +247,24 @@ module Searchkick
246 247 end
247 248  
248 249 if async
  250 + if async.is_a?(Hash) && async[:wait]
  251 + puts "Created index: #{index.name}"
  252 + puts "Jobs queued. Waiting..."
  253 + loop do
  254 + sleep 3
  255 + status = Searchkick.reindex_status(index.name)
  256 + break if status[:completed]
  257 + puts "Batches left: #{status[:batches_left]}"
  258 + end
  259 + # already promoted if alias didn't exist
  260 + if alias_exists
  261 + puts "Jobs complete. Promoting..."
  262 + promote(index.name, update_refresh_interval: !refresh_interval.nil?)
  263 + end
  264 + clean_indices unless retain
  265 + puts "SUCCESS!"
  266 + end
  267 +
249 268 {index_name: index.name}
250 269 else
251 270 index.refresh
... ...