class Aruba::Processes::ProcessRunner

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

Attributes

command_array[R]
cwd[RW]
environment[RW]
pid[R]
stderr[RW]
stdout[RW]

Public Class Methods

new(command_array) click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 15
def initialize(command_array)
  @command_array = command_array
  @exit_status = nil
end

Public Instance Methods

exit_code() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 80
def exit_code
  @exit_status&.exitstatus
end
exited?() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 55
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
poll_for_exit(exit_timeout) click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 68
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
start() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 23
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
stdin() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 34
def stdin
  @stdin_w
end
stop() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 38
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
wait() click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 50
def wait
  _, status = Process.waitpid2 @pid
  @exit_status = status
end

Private Instance Methods

send_signal(signal) click to toggle source
# File lib/aruba/processes/spawn_process.rb, line 86
def send_signal(signal)
  Process.kill signal, @pid
end