[Groonga-commit] droonga/droonga-engine at 1f6a143 [master] serf: raise an error when serf exits with failure

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jan 5 17:17:33 JST 2015


Kouhei Sutou	2015-01-05 17:17:33 +0900 (Mon, 05 Jan 2015)

  New Revision: 1f6a14328b061aafbd450b16181f59736963af06
  https://github.com/droonga/droonga-engine/commit/1f6a14328b061aafbd450b16181f59736963af06

  Message:
    serf: raise an error when serf exits with failure

  Modified files:
    bin/droonga-engine-absorb-data
    bin/droonga-engine-join
    bin/droonga-engine-unjoin
    lib/droonga/serf.rb
    lib/droonga/serf_command.rb

  Modified: bin/droonga-engine-absorb-data (+2 -5)
===================================================================
--- bin/droonga-engine-absorb-data    2015-01-05 16:48:18 +0900 (49e4783)
+++ bin/droonga-engine-absorb-data    2015-01-05 17:17:33 +0900 (23b2ced)
@@ -132,11 +132,8 @@ class AbsorbDataCommand
   end
 
   def run_remote_command(target, command, options)
-    serf = Droonga::Serf.new(nil, target)
-    result = serf.send_query(command, options)
-    #puts result[:result]
-    puts result[:error] unless result[:error].empty?
-    result[:response]
+    serf = Droonga::Serf.new(target)
+    serf.send_query(command, options)
   end
 
   def absorber

  Modified: bin/droonga-engine-join (+2 -5)
===================================================================
--- bin/droonga-engine-join    2015-01-05 16:48:18 +0900 (4547634)
+++ bin/droonga-engine-join    2015-01-05 17:17:33 +0900 (55192cb)
@@ -125,11 +125,8 @@ class JoinCommand
   end
 
   def run_remote_command(target, command, options)
-    serf = Droonga::Serf.new(nil, target)
-    result = serf.send_query(command, options)
-    #puts(result[:result])
-    puts(result[:error]) unless result[:error].empty?
-    result[:response]
+    serf = Droonga::Serf.new(target)
+    serf.send_query(command, options)
   end
 
   def absorber

  Modified: bin/droonga-engine-unjoin (+2 -5)
===================================================================
--- bin/droonga-engine-unjoin    2015-01-05 16:48:18 +0900 (634f609)
+++ bin/droonga-engine-unjoin    2015-01-05 17:17:33 +0900 (6ae7df2)
@@ -133,11 +133,8 @@ class UnjoinCommand
   end
 
   def run_remote_command(target, command, options)
-    serf = Droonga::Serf.new(nil, target)
-    result = serf.send_query(command, options)
-    puts(result[:result])
-    puts(result[:error]) unless result[:error].empty?
-    result[:response]
+    serf = Droonga::Serf.new(target)
+    serf.send_query(command, options)
   end
 
   def do_unjoin

  Modified: lib/droonga/serf.rb (+17 -14)
===================================================================
--- lib/droonga/serf.rb    2015-01-05 16:48:18 +0900 (2cfc0a8)
+++ lib/droonga/serf.rb    2015-01-05 17:17:33 +0900 (7d72ef1)
@@ -79,22 +79,25 @@ module Droonga
     def send_query(query, payload)
       options = ["-format", "json"] + additional_options_from_payload(payload)
       options += [query, JSON.generate(payload)]
-      result = run_command("query", *options)
-      result[:result] = JSON.parse(result[:result])
-      if payload["node"]
-        responses = result[:result]["Responses"]
-        response = responses[payload["node"]]
+      raw_serf_response = run_command("query", *options)
+      serf_response = JSON.parse(raw_serf_response)
+
+      node = payload["node"]
+      if node
+        responses = serf_response["Responses"]
+        response = responses[node]
         if response.is_a?(String)
           begin
-            result[:response] = JSON.parse(response)
+            JSON.parse(response)
           rescue JSON::ParserError
-            result[:response] = response
+            response
           end
         else
-          result[:response] = response
+          response
         end
+      else
+        response
       end
-      result
     end
 
     def update_cluster_state
@@ -108,12 +111,12 @@ module Droonga
     end
 
     def current_cluster_state
-      nodes = {}
-      result = run_command("members", "-format", "json")
-      result[:result] = JSON.parse(result[:result])
-      members = result[:result]
+      raw_response = run_command("members", "-format", "json")
+      response = JSON.parse(raw_response)
+
       current_cluster_id = cluster_id
-      members["members"].each do |member|
+      nodes = {}
+      response["members"].each do |member|
         foreign = member["tags"]["cluster_id"] != current_cluster_id
         next if foreign
 

  Modified: lib/droonga/serf_command.rb (+22 -6)
===================================================================
--- lib/droonga/serf_command.rb    2015-01-05 16:48:18 +0900 (f88942f)
+++ lib/droonga/serf_command.rb    2015-01-05 17:17:33 +0900 (3889e6e)
@@ -20,6 +20,20 @@ require "droonga/loggable"
 module Droonga
   class Serf
     class Command
+      class Failed < Error
+        attr_reader :command_line, :exit_status, :output, :error
+        def initialize(command_line, exit_status, output, error)
+          @command_line = command_line
+          @exit_status = exit_status
+          @output = output
+          @error = error
+          message = "Failed to run serf: (#{@exit_status}): "
+          message << "#{@error}[#{@output}]: "
+          message << @command_line..join(" ")
+          super(message)
+        end
+      end
+
       include Loggable
 
       def initialize(serf, command, *options)
@@ -29,12 +43,14 @@ module Droonga
       end
 
       def run
-        stdout, stderror, status = Open3.capture3(@serf, @command, *@options, :pgroup => true)
-        {
-          :result => stdout,
-          :error  => stderror,
-          :status => status,
-        }
+        command_line = [@serf, @command] + @options
+        stdout, stderror, status = Open3.capture3(*command_line,
+                                                  :pgroup => true)
+        unless status.success?
+          raise Failed.new(command_line, status.to_i, stdout, stderror)
+        end
+        logger.error("run: #{stderror}") unless stderror.empty?
+        stdout
       end
 
       def log_tag
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index