Class: ProcRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/packman/proc_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(*args) {|runner| ... } ⇒ Object

Yields:

  • (runner)


4
5
6
7
8
# File 'lib/packman/proc_runner.rb', line 4

def self.run(*args)
  runner = new
  yield runner
  runner.start!(*args)
end

Instance Method Details

#each_line(&blk) ⇒ Object



30
# File 'lib/packman/proc_runner.rb', line 30

def each_line(&blk); @each_line = blk; end

#exited(&blk) ⇒ Object



31
# File 'lib/packman/proc_runner.rb', line 31

def exited(&blk); @exited = blk; end

#kill!Object



25
26
27
# File 'lib/packman/proc_runner.rb', line 25

def kill!
  Process.kill(:TERM, @pid)
end

#start!(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/packman/proc_runner.rb', line 10

def start!(*args)
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    @pid = wait_thr.pid
    begin
      @started.call if @started
      while true
        line = stdout.readline
        @each_line.call(line) if @each_line
      end
    rescue EOFError
    end
    exit_status = wait_thr.value
  end
end

#started(&blk) ⇒ Object



29
# File 'lib/packman/proc_runner.rb', line 29

def started(&blk); @started = blk; end