Class: Fluent::Plugin::DirectoryInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_directory.rb

Instance Method Summary collapse

Instance Method Details

#startObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/in_directory.rb', line 41

def start
  super

  begin
    # Scan files indefinitely
    loop do
      # Use a stream to submit multiple events at the same time
      multiEventStream = MultiEventStream.new

      # Use the current time as the event time
      time = Fluent::Engine.now

      # Read filenames in the directory
      Dir.glob(@path + '/*') do |filename|
        # Ignore already processed files
        next if filename.end_with? @extension

        # Add the record to the stream
        multiEventStream.add(
          time,
          { @content_key => File.read(filename), @file_key => filename }
        )

        # Mark the file as processed
        File.rename(filename, filename + @extension)
      end

      # Send the events
      router.emit_stream(tag, multiEventStream)

      # Wait before the next scan
      sleep(@run_interval)
    end
  rescue Exception => e
    $log.warn 'Directory input error: ', e
    $log.debug_backtrace(e.backtrace)
  end
end