Class: Aruba::Processes::ProcessRunner
- Inherits:
-
Object
- Object
- Aruba::Processes::ProcessRunner
- Defined in:
- lib/aruba/processes/spawn_process.rb
Overview
Wrapper around Process.spawn that broadly follows the ChildProcess interface
Instance Attribute Summary collapse
-
#command_array ⇒ Object
readonly
Returns the value of attribute command_array.
-
#cwd ⇒ Object
Returns the value of attribute cwd.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Instance Method Summary collapse
- #exit_code ⇒ Object
- #exited? ⇒ Boolean
-
#initialize(command_array) ⇒ ProcessRunner
constructor
A new instance of ProcessRunner.
- #poll_for_exit(exit_timeout) ⇒ Object
- #start ⇒ Object
- #stdin ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
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_array ⇒ Object (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 |
#cwd ⇒ Object
Returns the value of attribute cwd.
20 21 22 |
# File 'lib/aruba/processes/spawn_process.rb', line 20 def cwd @cwd end |
#environment ⇒ Object
Returns the value of attribute environment.
20 21 22 |
# File 'lib/aruba/processes/spawn_process.rb', line 20 def environment @environment end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
21 22 23 |
# File 'lib/aruba/processes/spawn_process.rb', line 21 def pid @pid end |
#stderr ⇒ Object
Returns the value of attribute stderr.
20 21 22 |
# File 'lib/aruba/processes/spawn_process.rb', line 20 def stderr @stderr end |
#stdout ⇒ Object
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_code ⇒ Object
80 81 82 |
# File 'lib/aruba/processes/spawn_process.rb', line 80 def exit_code @exit_status&.exitstatus end |
#exited? ⇒ 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 |
#start ⇒ Object
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 |
#stdin ⇒ Object
34 35 36 |
# File 'lib/aruba/processes/spawn_process.rb', line 34 def stdin @stdin_w end |
#stop ⇒ Object
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 |
#wait ⇒ Object
50 51 52 53 |
# File 'lib/aruba/processes/spawn_process.rb', line 50 def wait _, status = Process.waitpid2 @pid @exit_status = status end |