class Aruba::SpawnProcess

Public Class Methods

new(cmd, exit_timeout, io_wait) click to toggle source
# File lib/aruba/spawn_process.rb, line 10
def initialize(cmd, exit_timeout, io_wait)
  @exit_timeout = exit_timeout
  @io_wait = io_wait

  @cmd = cmd
  @process = nil
  @exit_code = nil
  @output_cache = nil
  @error_cache = nil
end

Public Instance Methods

output() click to toggle source
# File lib/aruba/spawn_process.rb, line 41
def output
  stdout + stderr
end
read_stdout() click to toggle source
# File lib/aruba/spawn_process.rb, line 53
def read_stdout
  wait_for_io do
    @process.io.stdout.flush
    open(@out.path).read
  end
end
run!() { |self| ... } click to toggle source
# File lib/aruba/spawn_process.rb, line 21
def run!(&block)
  @process = ChildProcess.build(*shellwords(@cmd))
  @out = Tempfile.new("aruba-out")
  @err = Tempfile.new("aruba-err")
  @process.io.stdout = @out
  @process.io.stderr = @err
  @process.duplex = true
  @exit_code = nil
  begin
    @process.start
  rescue ChildProcess::LaunchError => e
    raise LaunchError.new(e.message)
  end
  yield self if block_given?
end
stderr() click to toggle source
# File lib/aruba/spawn_process.rb, line 49
def stderr
  wait_for_io { read(@err) } || @error_cache
end
stdin() click to toggle source
# File lib/aruba/spawn_process.rb, line 37
def stdin
  @process.io.stdin
end
stdout() click to toggle source
# File lib/aruba/spawn_process.rb, line 45
def stdout
  wait_for_io { read(@out) } || @output_cache
end
stop(reader) click to toggle source
# File lib/aruba/spawn_process.rb, line 60
def stop(reader)
  return @exit_code unless @process
  unless @process.exited?
    @process.poll_for_exit(@exit_timeout)
  end
  @exit_code = @process.exit_code
  @process = nil
  close_and_cache_out
  close_and_cache_err
  if reader
    reader.stdout stdout
    reader.stderr stderr
  end
  @exit_code
end
terminate() click to toggle source
# File lib/aruba/spawn_process.rb, line 76
def terminate
  if @process
    @process.stop
    stop nil
  end
end

Private Instance Methods

close_and_cache_err() click to toggle source
# File lib/aruba/spawn_process.rb, line 103
def close_and_cache_err
  @error_cache = read @err
  @err.close
  @err = nil
end
close_and_cache_out() click to toggle source
# File lib/aruba/spawn_process.rb, line 97
def close_and_cache_out
  @output_cache = read @out
  @out.close
  @out = nil
end
read(io) click to toggle source
# File lib/aruba/spawn_process.rb, line 92
def read(io)
  io.rewind
  io.read
end
wait_for_io() { || ... } click to toggle source
# File lib/aruba/spawn_process.rb, line 85
def wait_for_io(&block)
  if @process
    sleep @io_wait
    yield
  end
end