class ThreadedRunner

Attributes

thread[R]

Public Class Methods

new(cmd) click to toggle source
# File lib/threaded_runner.rb, line 2
def initialize(cmd)
  @cmd = cmd
end

Public Instance Methods

start(wait_for=nil) { |line| ... } click to toggle source
# File lib/threaded_runner.rb, line 8
def start(wait_for=nil)
  @thread = Thread.new do
    IO.popen(@cmd) do |f|
      @pid = f.pid
      f.each_line do |line|
        line.strip!
        yield line if block_given?
        if @regex && line =~ @regex
          @gotit = true
          @regex = nil
        end
      end
    end
  end
  nil
  wait_for(wait_for) if wait_for
end
stop(sig="TERM") click to toggle source
# File lib/threaded_runner.rb, line 33
def stop(sig="TERM")
  Process.kill(sig, @pid)
end
wait_for(output) click to toggle source
# File lib/threaded_runner.rb, line 26
def wait_for(output)
  @regex = output
  sleep 0.2 until @gotit
  @gotit = false
  nil
end