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, wdir = '.') ⇒ ThreadedRunner

Returns a new instance of ThreadedRunner.



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

def initialize(cmd, wdir='.')
  @cmd = cmd
  @wdir = wdir
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#start(wait_for = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/threaded_runner.rb', line 9

def start(wait_for=nil)
  @thread = Thread.new do
    Dir.chdir(@wdir) 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
  end
  nil
  wait_for(wait_for) if wait_for
end

#stopObject



36
37
38
# File 'lib/threaded_runner.rb', line 36

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

#wait_for(output) ⇒ Object



29
30
31
32
33
34
# File 'lib/threaded_runner.rb', line 29

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