Class: ProcessHelper::ProcessHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/process-helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ProcessHelper

opts can contain:

:print_lines

echo the STDOUT and STDERR of the process to STDOUT as well as capturing them.

:poll_rate

rate, in seconds (default is 0.25) to poll the capturered output for matching lines.



11
12
13
14
15
16
# File 'lib/process-helper.rb', line 11

def initialize(opts = {})
  @opts =
    {
      :print_lines => false
    }.merge!(opts)
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



6
7
8
# File 'lib/process-helper.rb', line 6

def exit_status
  @exit_status
end

#pidObject (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/process-helper.rb', line 6

def pid
  @pid
end

Instance Method Details

#get_log(which) ⇒ Object

Gets an array containing all the lines for the specified output stream. which can be either of:

  • :out

  • :err



59
60
61
62
# File 'lib/process-helper.rb', line 59

def get_log(which)
  log = _get_log(which)
  log.nil? ? [] : log.to_a
end

#get_log!(which) ⇒ Object

Gets an array containing all the lines for the specified stream, emptying the stored buffer. which can be either of:

  • :out

  • :err



68
69
70
71
# File 'lib/process-helper.rb', line 68

def get_log!(which)
  log = _get_log(which)
  log.nil? ? [] : log.drain
end

#kill(signal = 'TERM') ⇒ Object

Send the specified signal to the wrapped process.



51
52
53
# File 'lib/process-helper.rb', line 51

def kill(signal = 'TERM')
  Process.kill(signal, @pid)
end

#start(command_and_args = [], output_to_wait_for = nil, wait_timeout = nil, env = {}, opts = {}) ⇒ Object

Starts a process defined by ‘command_and_args`

command_and_args

Array with the first element as the process to start, the rest being the parameters to the new process.

output_to_wait_for

Regex containing expected output, start will block until a line of STDOUT matches this regex, if this is nil, then no waiting occurs.

wait_timeout

Timeout while waiting for particular output.

env

Hash of extra environment variables with which to start the process.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/process-helper.rb', line 23

def start(command_and_args = [], output_to_wait_for = nil, wait_timeout = nil, env = {}, opts = {})
  out_r, out_w = IO.pipe
  @out_log = ProcessLog.new(out_r, @opts).start
  if opts[:stderr]
    err_r, err_w = IO.pipe
    @err_log = ProcessLog.new(err_r, @opts).start
  else
    err_w = out_w
  end
  @pid = spawn(env, *command_and_args, :out => out_w, :err => err_w)
  out_w.close
  err_w.close if opts[:stderr]
  @out_log.wait_for_output(output_to_wait_for, :timeout => wait_timeout) unless output_to_wait_for.nil?
end

#wait_for_exitObject

returns true if the process exited with an exit code of 0.



39
40
41
42
43
44
45
46
47
48
# File 'lib/process-helper.rb', line 39

def wait_for_exit
  @out_log.wait
  @err_log.wait unless @err_log.nil?

  Process.wait(@pid)
  @exit_status = $CHILD_STATUS

  @pid = nil
  @exit_status == 0
end

#wait_for_output(which, regexp, opts = {}) ⇒ Object

Blocks the current thread until the specified regex has been matched in the output. which can be either of:

  • :out

  • :err

opts can contain:

:timeout

timeout in seconds to wait for the specified output.

:poll_rate

rate, in seconds (default is 0.25) to poll the capturered output for matching lines.



80
81
82
# File 'lib/process-helper.rb', line 80

def wait_for_output(which, regexp, opts = {})
  _get_log(which).wait_for_output(regexp, opts)
end