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 committed Mar 3, 2017
1 parent ba84f03 commit 8ff1b8d
Show file tree
Hide file tree
Showing 2 changed files with 32 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 @@ -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

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'fluent/test'
require 'net/http'
require 'flexmock/test_unit'
require 'timecop'

class TailInputTest < Test::Unit::TestCase
include FlexMock::TestCase
Expand Down Expand Up @@ -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

0 comments on commit 8ff1b8d

Please sign in to comment.