Class: Komenda::Process
- Inherits:
-
Object
- Object
- Komenda::Process
- Includes:
- Events::Emitter
- Defined in:
- lib/komenda/process.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#initialize(process_builder) ⇒ Process
constructor
A new instance of Process.
- #kill(signal = 'TERM') ⇒ Object
- #pid ⇒ Integer
- #result ⇒ Komenda::Result
- #run ⇒ Komenda::Result
- #running? ⇒ TrueClass, FalseClass
- #start ⇒ Thread
- #wait_for ⇒ Komenda::Result
Constructor Details
#initialize(process_builder) ⇒ Process
Returns a new instance of 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
#output ⇒ Object (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
47 48 49 |
# File 'lib/komenda/process.rb', line 47 def kill(signal = 'TERM') ::Process.kill(signal, pid) end |
#pid ⇒ Integer
41 42 43 44 |
# File 'lib/komenda/process.rb', line 41 def pid fail 'No PID available' if @pid.nil? @pid end |
#result ⇒ Komenda::Result
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 |
#run ⇒ Komenda::Result
35 36 37 38 |
# File 'lib/komenda/process.rb', line 35 def run start unless started? wait_for end |
#running? ⇒ TrueClass, FalseClass
52 53 54 |
# File 'lib/komenda/process.rb', line 52 def running? started? && !@pid.nil? && !finished? end |
#start ⇒ 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_for ⇒ Komenda::Result
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 |