Class: ProcessHelper::ProcessLog

Inherits:
Object
  • Object
show all
Defined in:
lib/process-helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, opts, prefill = []) ⇒ ProcessLog

Returns a new instance of ProcessLog.



99
100
101
102
103
104
105
# 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

Instance Method Details

#drainObject



144
145
146
147
148
149
150
# File 'lib/process-helper.rb', line 144

def drain
  @mutex.synchronize do
    r = @lines.dup
    @lines.clear
    r
  end
end

#eofObject



119
120
121
# File 'lib/process-helper.rb', line 119

def eof
  @mutex.synchronize { !!@eof }
end

#startObject



107
108
109
110
111
112
113
114
115
116
117
# 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_aObject



140
141
142
# File 'lib/process-helper.rb', line 140

def to_a
  @mutex.synchronize { @lines.dup }
end

#to_sObject



152
153
154
# File 'lib/process-helper.rb', line 152

def to_s
  @mutex.synchronize { @lines.join '' }
end

#waitObject



134
135
136
137
138
# File 'lib/process-helper.rb', line 134

def wait
  @thread.join
  @thread = nil
  self
end

#wait_for_output(regex, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# 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