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
28
# File 'lib/threaded_runner.rb', line 9

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

#stop(sig = "TERM") ⇒ Object



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

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

#wait_for(output) ⇒ Object



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

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