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.
17 18 19 20 |
# File 'lib/aruba/processes/spawn_process.rb', line 17 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.
23 24 25 |
# File 'lib/aruba/processes/spawn_process.rb', line 23 def command_array @command_array end |
#cwd ⇒ Object
Returns the value of attribute cwd.
22 23 24 |
# File 'lib/aruba/processes/spawn_process.rb', line 22 def cwd @cwd end |
#environment ⇒ Object
Returns the value of attribute environment.
22 23 24 |
# File 'lib/aruba/processes/spawn_process.rb', line 22 def environment @environment end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
23 24 25 |
# File 'lib/aruba/processes/spawn_process.rb', line 23 def pid @pid end |
#stderr ⇒ Object
Returns the value of attribute stderr.
22 23 24 |
# File 'lib/aruba/processes/spawn_process.rb', line 22 def stderr @stderr end |
#stdout ⇒ Object
Returns the value of attribute stdout.
22 23 24 |
# File 'lib/aruba/processes/spawn_process.rb', line 22 def stdout @stdout end |
Instance Method Details
#exit_code ⇒ Object
82 83 84 |
# File 'lib/aruba/processes/spawn_process.rb', line 82 def exit_code @exit_status&.exitstatus end |
#exited? ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/aruba/processes/spawn_process.rb', line 57 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
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/aruba/processes/spawn_process.rb', line 70 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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/aruba/processes/spawn_process.rb', line 25 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
36 37 38 |
# File 'lib/aruba/processes/spawn_process.rb', line 36 def stdin @stdin_w end |
#stop ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/aruba/processes/spawn_process.rb', line 40 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
52 53 54 55 |
# File 'lib/aruba/processes/spawn_process.rb', line 52 def wait _, status = Process.waitpid2 @pid @exit_status = status end |