Module: Servitor::ChildProcessHelper

Included in:
EnvdirDeployer, VagrantBox, VeeweeBox
Defined in:
lib/helpers/child_process_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



33
34
35
36
# File 'lib/helpers/child_process_helper.rb', line 33

def self.included(base)
  # this can work as a class method too
  base.extend(ChildProcessHelper)
end

Instance Method Details

#execute_child_process(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/helpers/child_process_helper.rb', line 4

def execute_child_process(*args)
  options = args.pop if args.last.is_a? Hash
  options ||= {}
  #puts "Executing: #{args.inspect}"
  process = ChildProcess.build(*args)
  if block_given?
    yield process
  else
    process.io.inherit!
  end
  process.start
  exit_code = process.wait
  unless exit_code == 0 || options[:ignore_exit_code]
    raise ServitorChildProcessError, "#{args.inspect}: exited with code #{exit_code}"
  end
end

#execute_child_process_and_capture_output(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helpers/child_process_helper.rb', line 21

def execute_child_process_and_capture_output(*args)
  output = nil
  Tempfile.open('ChildProcessHelper') do |tempfile|
    execute_child_process(*args) do |process|
      process.io.stdout = process.io.stderr = tempfile
    end
    tempfile.rewind
    output = tempfile.read
  end
  output
end