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

Returns a new instance of Process.

Parameters:



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

Parameters:

  • (Integer, String)


47
48
49
# File 'lib/komenda/process.rb', line 47

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

#pidInteger

Returns:

  • (Integer)


41
42
43
44
# File 'lib/komenda/process.rb', line 41

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

#resultKomenda::Result

Returns:



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

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

#runKomenda::Result

Returns:



35
36
37
38
# File 'lib/komenda/process.rb', line 35

def run
  start unless started?
  wait_for
end

#running?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


52
53
54
# File 'lib/komenda/process.rb', line 52

def running?
  started? && !@pid.nil? && !finished?
end

#startThread

Returns:

  • (Thread)


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

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

#wait_forKomenda::Result

Returns:



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

def wait_for
  fail 'Process not started' unless started?
  @thread.join
  result
end