Class: Komenda::Process

Inherits:
Object
  • Object
show all
Includes:
Events::Emitter
Defined in:
lib/komenda/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process_builder) ⇒ Process



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/komenda/process.rb', line 8

def initialize(process_builder)
  @process_builder = process_builder
  @output = { stdout: '', stderr: '', combined: '' }
  @exit_status = @thread = @pid = nil

  on(:stdout) { |data| @output[:stdout] += data }
  on(:stderr) { |data| @output[:stderr] += data }
  on(:output) { |data| @output[:combined] += data }
  process_builder.events.each do |event|
    on(event[:type], &event[:listener])
  end
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/komenda/process.rb', line 3

def output
  @output
end

Instance Method Details

#kill(signal = 'TERM') ⇒ Object



50
51
52
# File 'lib/komenda/process.rb', line 50

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

#pidInteger



44
45
46
47
# File 'lib/komenda/process.rb', line 44

def pid
  raise 'No PID available' if @pid.nil?
  @pid
end

#resultKomenda::Result



63
64
65
66
67
# File 'lib/komenda/process.rb', line 63

def result
  raise 'Process not started' unless started?
  raise 'Process not finished' unless finished?
  Komenda::Result.new(@output, @exit_status)
end

#runKomenda::Result



38
39
40
41
# File 'lib/komenda/process.rb', line 38

def run
  start unless started?
  wait_for
end

#running?TrueClass, FalseClass



55
56
57
58
59
60
# File 'lib/komenda/process.rb', line 55

def running?
  return false if @pid.nil?
  1 == ::Process.kill(0, @pid)
rescue Errno::ESRCH
  false
end

#startThread



22
23
24
25
# File 'lib/komenda/process.rb', line 22

def start
  raise 'Already started' if started?
  @thread = Thread.new { run_process(@process_builder) }
end

#wait_forKomenda::Result



28
29
30
31
32
33
34
35
# File 'lib/komenda/process.rb', line 28

def wait_for
  raise 'Process not started' unless started?
  @thread.join
  if @process_builder.fail_on_fail && result.error?
    raise "Process failed with exit status `#{result.exitstatus}` and output:\n#{result.output}"
  end
  result
end