Class: FileWatch::ReadMode::Handlers::ReadZipFile

Inherits:
Base
  • Object
show all
Defined in:
lib/filewatch/read_mode/handlers/read_zip_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

#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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/filewatch/read_mode/handlers/read_zip_file.rb', line 14

def handle_specifically(watched_file)
  add_or_update_sincedb_collection(watched_file) unless sincedb_collection.member?(watched_file.sincedb_key)
  # can't really stripe read a zip file, its all or nothing.
  watched_file.listener.opened
  # what do we do about quit when we have just begun reading the zipped file (e.g. pipeline reloading)
  # should we track lines read in the sincedb and
  # fast forward through the lines until we reach unseen content?
  # meaning that we can quit in the middle of a zip file
  key = watched_file.sincedb_key

  if @settings.check_archive_validity && corrupted?(watched_file)
    watched_file.unwatch
  else
    begin
      file_stream = FileInputStream.new(watched_file.path)
      gzip_stream = GZIPInputStream.new(file_stream)
      decoder = InputStreamReader.new(gzip_stream, "UTF-8")
      buffered = BufferedReader.new(decoder)
      while (line = buffered.readLine())
        watched_file.listener.accept(line)
        # can't quit, if we did then we would incorrectly write a 'completed' sincedb entry
        # what do we do about quit when we have just begun reading the zipped file (e.g. pipeline reloading)
        # should we track lines read in the sincedb and
        # fast forward through the lines until we reach unseen content?
        # meaning that we can quit in the middle of a zip file
      end
      watched_file.listener.eof
    rescue ZipException => e
      logger.error("Cannot decompress the gzip file at path: #{watched_file.path}", :exception => e.class,
                   :message => e.message, :backtrace => e.backtrace)
      watched_file.listener.error
    else
      sincedb_collection.store_last_read(key, watched_file.last_stat_size)
      sincedb_collection.request_disk_flush
      watched_file.listener.deleted
      watched_file.unwatch
    ensure
      # rescue each close individually so all close attempts are tried
      close_and_ignore_ioexception(buffered) unless buffered.nil?
      close_and_ignore_ioexception(decoder) unless decoder.nil?
      close_and_ignore_ioexception(gzip_stream) unless gzip_stream.nil?
      close_and_ignore_ioexception(file_stream) unless file_stream.nil?
    end
  end
  sincedb_collection.clear_watched_file(key)
end