class ProcessHelper::ProcessLog

Public Class Methods

new(io, opts, prefill = []) click to toggle source
# File lib/process-helper.rb, line 99
def initialize(io, opts, prefill = [])
  @io = io
  @lines = prefill.dup
  @mutex = Mutex.new
  @opts = opts
  @eof = false
end

Public Instance Methods

drain() click to toggle source
# File lib/process-helper.rb, line 144
def drain
  @mutex.synchronize do
    r = @lines.dup
    @lines.clear
    r
  end
end
eof() click to toggle source
# File lib/process-helper.rb, line 119
def eof
  @mutex.synchronize { !!@eof }
end
start() click to toggle source
# File lib/process-helper.rb, line 107
def start
  @thread = Thread.new do
    @io.each_line do |l|
      l = TimestampedString.new(l)
      STDOUT.puts l if @opts[:print_lines]
      @mutex.synchronize { @lines.push l }
    end
    @mutex.synchronize { @eof = true }
  end
  self
end
to_a() click to toggle source
# File lib/process-helper.rb, line 140
def to_a
  @mutex.synchronize { @lines.dup }
end
to_s() click to toggle source
# File lib/process-helper.rb, line 152
def to_s
  @mutex.synchronize { @lines.join '' }
end
wait() click to toggle source
# File lib/process-helper.rb, line 134
def wait
  @thread.join
  @thread = nil
  self
end
wait_for_output(regex, opts = {}) click to toggle source
# File lib/process-helper.rb, line 123
def wait_for_output(regex, opts = {})
  opts = { :poll_rate => 0.25 }.merge(opts)
  opts[:timeout] ||= 30
  cutoff = Time.now + opts[:timeout].to_i
  until _any_line_matches(regex)
    sleep(opts[:poll_rate])
    fail "Timeout of #{opts[:timeout]} seconds exceeded while waiting for output that matches '#{regex}'" if Time.now > cutoff
    fail "EOF encountered while waiting for output that matches '#{regex}'" if eof and !_any_line_matches(regex)
  end
end

Private Instance Methods

_any_line_matches(regex) click to toggle source
# File lib/process-helper.rb, line 158
def _any_line_matches(regex)
  to_a.any? { |line| line.match(regex) }
end