class Aruba::Processes::ProcessRunner

@private
Wrapper around Process.spawn that broadly follows the ChildProcess interface

def exit_code

def exit_code
  @exit_status&.exitstatus
end

def exited?

def exited?
  return true if @exit_status
  pid, status = Process.waitpid2 @pid, Process::WNOHANG | Process::WUNTRACED
  if pid
    @exit_status = status
    return true
  end
  false
end

def initialize(command_array)

def initialize(command_array)
  @command_array = command_array
  @exit_status = nil
end

def poll_for_exit(exit_timeout)

def poll_for_exit(exit_timeout)
  start = Time.now
  wait_until = start + exit_timeout
  loop do
    return true if exited?
    break if Time.now >= wait_until
    sleep 0.1
  end
  false
end

def send_signal(signal)

def send_signal(signal)
  Process.kill signal, @pid
end

def start

def start
  @stdin_r, @stdin_w = IO.pipe
  @pid = Process.spawn(environment, *command_array,
                       unsetenv_others: true,
                       in: @stdin_r,
                       out: stdout.fileno,
                       err: stderr.fileno,
                       close_others: true,
                       chdir: cwd)
end

def stdin

def stdin
  @stdin_w
end

def stop

def stop
  return if @exit_status
  if Aruba.platform.term_signal_supported?
    send_signal "TERM"
    return if poll_for_exit(3)
  end
  send_signal "KILL"
  wait
end

def wait

def wait
  _, status = Process.waitpid2 @pid
  @exit_status = status
end