Class: Aruba::Processes::ProcessRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba/processes/spawn_process.rb

Overview

Wrapper around Process.spawn that broadly follows the ChildProcess interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_array) ⇒ ProcessRunner

Returns a new instance of ProcessRunner.



15
16
17
18
# File 'lib/aruba/processes/spawn_process.rb', line 15

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

Instance Attribute Details

#command_arrayObject (readonly)

Returns the value of attribute command_array.



21
22
23
# File 'lib/aruba/processes/spawn_process.rb', line 21

def command_array
  @command_array
end

#cwdObject

Returns the value of attribute cwd.



20
21
22
# File 'lib/aruba/processes/spawn_process.rb', line 20

def cwd
  @cwd
end

#environmentObject

Returns the value of attribute environment.



20
21
22
# File 'lib/aruba/processes/spawn_process.rb', line 20

def environment
  @environment
end

#pidObject (readonly)

Returns the value of attribute pid.



21
22
23
# File 'lib/aruba/processes/spawn_process.rb', line 21

def pid
  @pid
end

#stderrObject

Returns the value of attribute stderr.



20
21
22
# File 'lib/aruba/processes/spawn_process.rb', line 20

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



20
21
22
# File 'lib/aruba/processes/spawn_process.rb', line 20

def stdout
  @stdout
end

Instance Method Details

#exit_codeObject



80
81
82
# File 'lib/aruba/processes/spawn_process.rb', line 80

def exit_code
  @exit_status&.exitstatus
end

#exited?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# 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

#startObject



23
24
25
26
27
28
29
30
31
32
# 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

#stdinObject



34
35
36
# File 'lib/aruba/processes/spawn_process.rb', line 34

def stdin
  @stdin_w
end

#stopObject



38
39
40
41
42
43
44
45
46
47
48
# 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

#waitObject



50
51
52
53
# File 'lib/aruba/processes/spawn_process.rb', line 50

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