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.



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_arrayObject (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

#cwdObject

Returns the value of attribute cwd.



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

def cwd
  @cwd
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#pidObject (readonly)

Returns the value of attribute pid.



23
24
25
# File 'lib/aruba/processes/spawn_process.rb', line 23

def pid
  @pid
end

#stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject

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_codeObject



82
83
84
# File 'lib/aruba/processes/spawn_process.rb', line 82

def exit_code
  @exit_status&.exitstatus
end

#exited?Boolean

Returns:

  • (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

#startObject



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

#stdinObject



36
37
38
# File 'lib/aruba/processes/spawn_process.rb', line 36

def stdin
  @stdin_w
end

#stopObject



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

#waitObject



52
53
54
55
# File 'lib/aruba/processes/spawn_process.rb', line 52

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