Skip to content

Commit

Permalink
Merge pull request #1474 from t-osakada/watch_only_recently_modified_…
Browse files Browse the repository at this point in the history
…files

Watch only recently modified files
  • Loading branch information
repeatedly authored Feb 17, 2017
2 parents 8f9a766 + ee33a6e commit 4f97a36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def initialize
config_param :path_key, :string, default: nil
desc 'Open and close the file on every update instead of leaving it open until it gets rotated.'
config_param :open_on_every_update, :bool, default: false
desc 'Limit the watching files that the modification time is within the specified time range (when use \'*\' in path).'
config_param :limit_recently_modified, :time, default: nil

attr_reader :paths

Expand Down Expand Up @@ -186,7 +188,11 @@ def expand_paths
if path.include?('*')
paths += Dir.glob(path).select { |p|
if File.readable?(p) && !File.directory?(p)
true
if @limit_recently_modified && File.mtime(p) < (date - @limit_recently_modified)
false
else
true
end
else
log.warn "#{p} unreadable. It is excluded and would be examined next time."
false
Expand Down
24 changes: 24 additions & 0 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1174,4 +1174,28 @@ def test_tail_path_with_multiline_with_multiple_paths
assert_equal(files, [events[2][2]["path"], events[3][2]["path"]].sort)
end
end

def test_limit_recently_modified
now = Time.new(2010, 1, 2, 3, 4, 5)
FileUtils.touch("#{TMP_DIR}/tail_unwatch.txt", mtime: (now - 3601))
FileUtils.touch("#{TMP_DIR}/tail_watch1.txt", mtime: (now - 3600))
FileUtils.touch("#{TMP_DIR}/tail_watch2.txt", mtime: now)

config = config_element('', '', {
'tag' => 'tail',
'path' => "#{TMP_DIR}/*.txt",
'format' => 'none',
'limit_recently_modified' => '3600s'
})

expected_files = [
"#{TMP_DIR}/tail_watch1.txt",
"#{TMP_DIR}/tail_watch2.txt"
]

Timecop.freeze(now) do
plugin = create_driver(config, false).instance
assert_equal expected_files, plugin.expand_paths.sort
end
end
end

0 comments on commit 4f97a36

Please sign in to comment.