Class: Fluent::TailPathInput::TailWatcher::IOHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tailpath.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, pe, &receive_lines) ⇒ IOHandler

Returns a new instance of IOHandler.



290
291
292
293
294
295
296
297
# File 'lib/fluent/plugin/in_tailpath.rb', line 290

def initialize(io, pe, &receive_lines)
  $log.info "following tail of #{io.path}"
  @io = io
  @pe = pe
  @receive_lines = receive_lines
  @buffer = ''.force_encoding('ASCII-8BIT')
  @iobuf = ''.force_encoding('ASCII-8BIT')
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



299
300
301
# File 'lib/fluent/plugin/in_tailpath.rb', line 299

def io
  @io
end

Instance Method Details

#closeObject



338
339
340
# File 'lib/fluent/plugin/in_tailpath.rb', line 338

def close
  @io.close unless @io.closed?
end

#on_notifyObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/fluent/plugin/in_tailpath.rb', line 301

def on_notify
  begin
    lines = []
    read_more = false

    begin
      while true
        if @buffer.empty?
          @io.read_nonblock(2048, @buffer)
        else
          @buffer << @io.read_nonblock(2048, @iobuf)
        end
        while line = @buffer.slice!(/.*?\n/m)
          lines << line
        end
        if lines.size >= MAX_LINES_AT_ONCE
          # not to use too much memory in case the file is very large
          read_more = true
          break
        end
      end
    rescue EOFError
    end

    unless lines.empty?
      @receive_lines.call(@io.path, lines)
      @pe.update_pos(@io.pos - @buffer.bytesize)
    end

  end while read_more

rescue
  $log.error $!.to_s
  $log.error_backtrace
  close
end