diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index fe6e0ba847..3fe47c90c4 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -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 @@ -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 diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index dd6e2b7360..590c5016c5 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -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