Class: FileWatch::ReadMode::Handlers::ReadFile

Inherits:
Base
  • Object
show all
Defined in:
lib/filewatch/read_mode/handlers/read_file.rb

Instance Attribute Summary

Attributes inherited from Base

#sincedb_collection

Instance Method Summary collapse

Methods inherited from Base

#handle, #initialize

Constructor Details

This class inherits a constructor from FileWatch::ReadMode::Handlers::Base

Instance Method Details

#handle_specifically(watched_file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/filewatch/read_mode/handlers/read_file.rb', line 5

def handle_specifically(watched_file)
  if open_file(watched_file)
    add_or_update_sincedb_collection(watched_file) unless sincedb_collection.member?(watched_file.sincedb_key)
    changed = false
    @settings.file_chunk_count.times do
      begin
        data = watched_file.file_read(@settings.file_chunk_size)
        result = watched_file.buffer_extract(data) # expect BufferExtractResult
        logger.info(result.warning, result.additional) unless result.warning.empty?
        changed = true
        result.lines.each do |line|
          watched_file.listener.accept(line)
          # sincedb position is independent from the watched_file bytes_read
          sincedb_collection.increment(watched_file.sincedb_key, line.bytesize + @settings.delimiter_byte_size)
        end
        # instead of tracking the bytes_read line by line we need to track by the data read size.
        # because we initially seek to the bytes_read not the sincedb position
        watched_file.increment_bytes_read(data.bytesize)
      rescue EOFError
        # flush the buffer now in case there is no final delimiter
        line = watched_file.buffer.flush
        watched_file.listener.accept(line) unless line.empty?
        watched_file.listener.eof
        watched_file.file_close
        # unset_watched_file will set sincedb_value.position to be watched_file.bytes_read
        sincedb_collection.unset_watched_file(watched_file)
        watched_file.listener.deleted
        watched_file.unwatch
        break
      rescue Errno::EWOULDBLOCK, Errno::EINTR
        watched_file.listener.error
        break
      rescue => e
        logger.error("read_to_eof: general error reading #{watched_file.path} - error: #{e.inspect}")
        watched_file.listener.error
        break
      end
    end
    sincedb_collection.request_disk_flush if changed
  end
end