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

Constructor Details

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

Instance Method Details

#handle_specifically(watched_file) ⇒ Object



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_zip_file.rb', line 12

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
  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(false))
      watched_file.listener.accept(line)
    end
    watched_file.listener.eof
  rescue ZipException => e
    logger.error("Cannot decompress the gzip file at path: #{watched_file.path}")
    watched_file.listener.error
  else
    sincedb_collection.store_last_read(watched_file.sincedb_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
  sincedb_collection.unset_watched_file(watched_file)
end