Class: Fluent::FilePositionEntry

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

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FilePositionEntry

Returns a new instance of FilePositionEntry.



6
7
8
# File 'lib/fluent/plugin/file_pos_entry.rb', line 6

def initialize(file_path)
  @pos_file = file_path
end

Instance Method Details

#read_posObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/file_pos_entry.rb', line 26

def read_pos
  min_date = "0000-01-01T00:00:00.000Z"

  return unless @pos_file
  f = Pathname.new(@pos_file)
  unless f.exist? then
    return ({:date=> min_date, :id=> ""})
  end
  
  pos = {}
  begin
    f.open('rb') do |_f|
      pos = Marshal.load(_f)
    end
  rescue => e
    return ({:date=> min_date, :id=> ""})
  end
  
  return (pos)
end

#update_pos(pos) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fluent/plugin/file_pos_entry.rb', line 10

def update_pos(pos)
  return unless @pos_file

  begin
    f = Pathname.new(@pos_file)
    f.open('wb') do |fp|
      Marshal.dump({
        :date => "#{pos[:date]}",
        :id   => "#{pos[:id]}",
      }, fp)
    end
  rescue => e
    $log.warn "Can't write pos_file #{e}"
  end
end