Class: ThreadedRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ ThreadedRunner

Returns a new instance of ThreadedRunner.



2
3
4
# File 'lib/threaded_runner.rb', line 2

def initialize(cmd)
  @cmd = cmd
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/threaded_runner.rb', line 6

def thread
  @thread
end

Instance Method Details

#start(wait_for = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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") ⇒ Object



33
34
35
# File 'lib/threaded_runner.rb', line 33

def stop(sig="TERM")
  Process.kill(sig, @pid)
end

#wait_for(output) ⇒ Object



26
27
28
29
30
31
# File 'lib/threaded_runner.rb', line 26

def wait_for(output)
  @regex = output
  sleep 0.2 until @gotit
  @gotit = false
  nil
end