Class: Fluent::Plugin::InStaticFile::FileTracker

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

Overview

FileTracker stores handled file information

Constant Summary collapse

FILE_TRACKER_ENTRY_REGEX =
/^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.freeze
FILE_TRACKER_ENTRY_FORMAT =
"%s\t%016x\t%016x\t%016x\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, follow_inodes: false, logger: nil) ⇒ FileTracker

Returns a new instance of FileTracker.



15
16
17
18
19
20
21
22
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 15

def initialize(file: nil, follow_inodes: false, logger: nil)
  @file = file
  @follow_inodes = follow_inodes
  @logger = logger

  @file_mutex = Mutex.new
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 13

def cache
  @cache
end

Instance Method Details

#add(file_info) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 60

def add(file_info)
  return false unless file_info

  key = @follow_inodes ? file_info.ino : file_info.path
  @file_mutex.synchronize do
    @file.seek(0, IO::SEEK_END)
    @file.write(file_info.to_file_tracker_entry_format)
    @cache[key] = file_info
  end
end

#has?(file_info) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 52

def has?(file_info)
  return false unless file_info

  key = @follow_inodes ? file_info.ino : file_info.path

  return @cache[key] if @cache[key] == file_info
end

#load_from_fileObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 29

def load_from_file
  return unless @file

  @file_mutex.synchronize do
    @file.pos = 0

    @file.each_line do |line|
      m = FILE_TRACKER_ENTRY_REGEX.match(line)
      next if m.nil?

      path = m[1]
      ino = m[2].to_i(16)
      mtime_s = m[3].to_i(16)
      mtime_ns = m[4].to_i(16)

      file_info = FileInfo.new(path, ino, mtime_s, mtime_ns)
      key = @follow_inodes ? file_info.ino : file_info.path

      @cache[key] = file_info
    end
  end
end

#reloadObject



24
25
26
27
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 24

def reload
  @cache = {}
  load_from_file
end

#remove(file_info) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/in_static_file/file_tracker.rb', line 71

def remove(file_info)
  return false unless file_info

  key = @follow_inodes ? file_info.ino : file_info.path
  @file_mutex.synchronize do
    @cache.delete(key)

    @file.pos = 0
    @file.truncate(0)
    @file.write(@cache.values.map(&:to_file_tracker_entry_format).join)
  end
end