From 8ff1b8d55268aa3cc6f78a8c29e7f6fd1b1100c7 Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Fri, 17 Feb 2017 20:58:01 +0900 Subject: [PATCH] Merge pull request #1474 from t-osakada/watch_only_recently_modified_files Watch only recently modified files --- lib/fluent/plugin/in_tail.rb | 8 +++++++- test/plugin/test_in_tail.rb | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 93003056c9..bca84e8e48 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -58,6 +58,8 @@ def initialize config_param :from_encoding, :string, default: nil desc 'Add the log path being tailed to records. Specify the field name to be used.' config_param :path_key, :string, default: nil + 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 @@ -154,7 +156,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 e613b4f2ab..07d9b656c0 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -2,6 +2,7 @@ require 'fluent/test' require 'net/http' require 'flexmock/test_unit' +require 'timecop' class TailInputTest < Test::Unit::TestCase include FlexMock::TestCase @@ -1011,4 +1012,28 @@ def test_tail_path_with_multiline_with_multiple_paths assert_equal(files, [emits[2][2]["path"], emits[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