Class: Utils::Subprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/meshx-plugin-sdk.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd, &block) ⇒ Subprocess

Returns a new instance of Subprocess.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/meshx-plugin-sdk.rb', line 42

def initialize(cmd, &block)
  Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
    { :out => stdout, :err => stderr }.each do |key, stream|
      Thread.new do
        until (line = stream.gets).nil?
          if key == :out
            yield line, nil, thread if block_given?
          else
            yield nil, line, thread if block_given?
          end
        end
      end
    end
    thread.join # don't exit until the external process is done
    exit_code = thread.value
    if (exit_code != 0)
      puts("Failed to execute_cmd #{cmd} exit code: #{exit_code}")
      Kernel.exit(false)
    end
  end
end