Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch only recently modified files #1474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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