[DRE-commits] [ruby-aruba] 13/74: Refactor stopping/terminating commands to make it easier to maintain

Hideki Yamane henrich at moszumanska.debian.org
Sat Nov 28 01:16:29 UTC 2015


This is an automated email from the git hooks/post-receive script.

henrich pushed a commit to branch debian/sid
in repository ruby-aruba.

commit be0bb380f3a20033829ebf8fee490a46fb57a56d
Author: Max Meyer <dev at fedux.org>
Date:   Mon Nov 23 16:37:03 2015 +0100

    Refactor stopping/terminating commands to make it easier to maintain
---
 lib/aruba/processes/spawn_process.rb | 99 +++++++++++++++---------------------
 1 file changed, 41 insertions(+), 58 deletions(-)

diff --git a/lib/aruba/processes/spawn_process.rb b/lib/aruba/processes/spawn_process.rb
index c3e7c6d..c95ac24 100644
--- a/lib/aruba/processes/spawn_process.rb
+++ b/lib/aruba/processes/spawn_process.rb
@@ -90,7 +90,7 @@ module Aruba
 
       # Access to stdout of process
       def stdin
-        return if @process.nil?
+        return if @process.exited?
 
         @process.io.stdin
       end
@@ -106,11 +106,9 @@ module Aruba
       # @return [String]
       #   The content of stdout
       def stdout(opts = {})
-        return @stdout_cache if @process.nil?
+        return @stdout_cache if stopped?
 
-        wait_for_io = opts.fetch(:wait_for_io, @io_wait)
-
-        wait_for_io wait_for_io do
+        wait_for_io opts.fetch(:wait_for_io, @io_wait) do
           @process.io.stdout.flush
           open(@stdout_file.path).read
         end
@@ -127,11 +125,9 @@ module Aruba
       # @return [String]
       #   The content of stderr
       def stderr(opts = {})
-        return @stderr_cache if @process.nil?
-
-        wait_for_io = opts.fetch(:wait_for_io, @io_wait)
+        return @stderr_cache if stopped?
 
-        wait_for_io wait_for_io do
+        wait_for_io opts.fetch(:wait_for_io, @io_wait) do
           @process.io.stderr.flush
           open(@stderr_file.path).read
         end
@@ -146,7 +142,7 @@ module Aruba
       end
 
       def write(input)
-        return if @process.nil?
+        return if stopped?
 
         @process.io.stdin.write(input)
         @process.io.stdin.flush
@@ -155,7 +151,7 @@ module Aruba
       end
 
       def close_io(name)
-        return if @process.nil?
+        return if stopped?
 
         if RUBY_VERSION < '1.9'
           @process.io.send(name.to_sym).close
@@ -166,34 +162,19 @@ module Aruba
 
       # rubocop:disable Metrics/MethodLength
       def stop(reader)
-        @stopped = true
-
-        return @exit_status unless @process
+        return @exit_status if stopped?
 
         begin
-          @process.poll_for_exit(@exit_timeout) unless @process.exited?
+          @process.poll_for_exit(@exit_timeout)
         rescue ChildProcess::TimeoutError
           @timed_out = true
-
-          if @stop_signal
-            # send stop signal ...
-            send_signal @stop_signal
-            # ... and set the exit status
-            wait
-          else
-            @process.stop
-          end
         end
 
-        @exit_status = @process.exit_code
-        @process   = nil
-
-        close_and_cache_out
-        close_and_cache_err
+        terminate
 
         if reader
-          reader.announce :stdout, stdout
-          reader.announce :stderr, stderr
+          reader.announce :stdout, @stdout_cache
+          reader.announce :stderr, @stderr_cache
         end
 
         @exit_status
@@ -202,22 +183,35 @@ module Aruba
 
       # Wait for command to finish
       def wait
-        return if @process.nil?
-
         @process.wait
       end
 
       # Terminate command
       def terminate
-        return unless @process
+        return @exit_status if stopped?
 
-        if @stop_signal
-          send_signal @stop_signal
-        else
-          @process.stop
+        unless @process.exited?
+          if @stop_signal
+            # send stop signal ...
+            send_signal @stop_signal
+            # ... and set the exit status
+            wait
+          else
+            @process.stop
+          end
         end
 
-        stop nil
+        @exit_status  = @process.exit_code
+
+        @stdout_cache = read_temporary_output_file @stdout_file
+        @stderr_cache = read_temporary_output_file @stderr_file
+
+        # @stdout_file = nil
+        # @stderr_file = nil
+
+        @stopped      = true
+
+        @exit_status
       end
 
       # Output pid of process
@@ -238,28 +232,17 @@ module Aruba
       private
 
       def wait_for_io(time_to_wait, &block)
-        return if @process.nil?
-
-        sleep time_to_wait
-
-        yield
-      end
-
-      def read(io)
-        io.rewind
-        io.read
+        sleep time_to_wait.to_i
+        block.call
       end
 
-      def close_and_cache_out
-        @stdout_cache = read @stdout_file
-        @stdout_file.close
-        @stdout_file = nil
-      end
+      def read_temporary_output_file(file)
+        file.flush
+        file.rewind
+        data = file.read
+        file.close
 
-      def close_and_cache_err
-        @stderr_cache = read @stderr_file
-        @stderr_file.close
-        @stderr_file = nil
+        data
       end
     end
   end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-aruba.git



More information about the Pkg-ruby-extras-commits mailing list