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, #quit?

Constructor Details

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

Instance Method Details

#controlled_read(watched_file, loop_control) ⇒ Object



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
# File 'lib/filewatch/read_mode/handlers/read_file.rb', line 45

def controlled_read(watched_file, loop_control)
  logger.trace? && logger.trace("reading...", :filename => watched_file.filename, :iterations => loop_control.count, :amount => loop_control.size)
  loop_control.count.times do
    break if quit?
    begin
      result = watched_file.read_extract_lines(loop_control.size) # expect BufferExtractResult
      logger.info(result.warning, result.additional) unless result.warning.empty?
      result.lines.each do |line|
        watched_file.listener.accept(line)
        # sincedb position is independent from the watched_file bytes_read
        delta = line.bytesize + @settings.delimiter_byte_size
        sincedb_collection.increment(watched_file.sincedb_key, delta)
      end
    rescue EOFError => e
      log_error("controlled_read: eof error reading file", watched_file, e)
      loop_control.flag_read_error
      break
    rescue Errno::EWOULDBLOCK, Errno::EINTR => e
      log_error("controlled_read: block or interrupt error reading file", watched_file, e)
      watched_file.listener.error
      loop_control.flag_read_error
      break
    rescue => e
      log_error("controlled_read: general error reading file", watched_file, e)
      watched_file.listener.error
      loop_control.flag_read_error
      break
    end
  end
end

#handle_specifically(watched_file) ⇒ Object



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
# File 'lib/filewatch/read_mode/handlers/read_file.rb', line 14

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)
    seek_to_furthest_position(watched_file)
    loop do
      break if quit?
      loop_control = watched_file.loop_control_adjusted_for_stat_size
      controlled_read(watched_file, loop_control)
      sincedb_collection.request_disk_flush
      break unless loop_control.keep_looping?
    end
    if watched_file.all_read?
      # 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
      key = watched_file.sincedb_key
      if sincedb_collection.get(key)
        sincedb_collection.reading_completed(key)
        sincedb_collection.clear_watched_file(key)
      end
      watched_file.listener.deleted
      # NOTE: on top of un-watching we should also remove from the watched files collection
      # if the file is getting deleted (on completion), that part currently resides in
      # DeleteCompletedFileHandler - triggered above using `watched_file.listener.deleted`
      watched_file.unwatch
    end
  end
end

#log_error(msg, watched_file, error) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/filewatch/read_mode/handlers/read_file.rb', line 76

def log_error(msg, watched_file, error)
  details = { :path => watched_file.path,
              :exception => error.class,
              :message => error.message,
              :backtrace => error.backtrace }
  if logger.debug?
    details[:file] = watched_file
  else
    details[:backtrace] = details[:backtrace].take(8) if details[:backtrace]
  end
  logger.error(msg, details)
end