commit:     51aea5a8f68dc5d4a49e6795ff83d439c7f03942
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Tue Feb 24 03:39:44 2015 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Tue Feb 24 03:39:44 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/ag.git;a=commit;h=51aea5a8

Working index status check!

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

---
 lib/storage.rb | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)

diff --git a/lib/storage.rb b/lib/storage.rb
index 7ec0861..4135971 100644
--- a/lib/storage.rb
+++ b/lib/storage.rb
@@ -5,28 +5,25 @@ require 'pp'
 module Ag::Storage
   module_function
 
-  def index_states(indexname)
-    status = $es.indices.status(index: indexname)
-    states = status['indices'][indexname]['shards'].map do |key,array|
-      array.map do |v|
-          v['routing']['state']
-      end.flatten
-    end.flatten
+  def index_status(indexname)
+    $es.cluster.health(level: :indices)['indices'][indexname]['status']
   end
+
   def index_ready?(indexname)
-    states = index_states(indexname)
-    not_ready = states.include? 'INITIALIZING' or states.include? 'RELOCATING'
-    return !not_ready
-    pp $es.cluster.health(level: indices)
+    status = index_status(indexname) 
+    # if you call index_status twice, you might get different results!
+    return status == 'green' or status == 'yellow'
   end
 
   # Throws Elasticsearch::Transport::Transport::Errors::NotFound
   # if the list does not exist
   def delete_index(list)
-      $es.indices.delete index: 'ml-' + list
+      $es.indices.delete(index: 'ml-' + list)
   end
 
-  def create_index(list)
+  # Create an index for list
+  # sleep in 5ms intervals until it is ready
+  def create_index(list, sleeptime: 0.005)
     indexname = 'ml-' + list
     $es.indices.create(
       index: indexname,
@@ -103,20 +100,8 @@ module Ag::Storage
         }
       })
 
-    pp $es.cluster.health(level: indices)
     # Give elasticsearch some time to process the new index
-    status = $es.indices.status(index: indexname)
-    while $es.cluster.health['status'] != 'green' do
-      pp status
-      pp status['indices'][indexname]['shards']
-      status = status['indices'][indexname]['shards'].map do |k,v|
-        v[0]['routing']['state']
-      end
-      pp status
-      sleep 0.01
-      status = $es.indices.status(index: indexname)
-    end
-    pp $es.indices.status(index: indexname)
+    sleep sleeptime while not index_ready?(indexname)
   end
 
   def get_content(message, filename)

Reply via email to