Module: FileWatch::TailBase

Included in:
ObservingTail, YieldingTail
Defined in:
lib/filewatch/tail_base.rb

Defined Under Namespace

Classes: NoSinceDBPathGiven

Constant Summary collapse

OPEN_WARN_INTERVAL =

how often (in seconds) we @logger.warn a failed file open, per path.

ENV.fetch("FILEWATCH_OPEN_WARN_INTERVAL", 300).to_i

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/filewatch/tail_base.rb', line 18

def logger
  @logger
end

Instance Method Details

#close_file(path) ⇒ Object

close_file(path) is to be used by external code when it knows that it is completely done with a file. Other files or folders may still be being watched. Caution, once unwatched, a file can’t be watched again unless a new instance of this class begins watching again. The sysadmin should rename, move or delete the file.



224
225
226
227
# File 'lib/filewatch/tail_base.rb', line 224

def close_file(path)
  @watch.unwatch(path)
  _sincedb_write
end

#initialize(opts = {}) ⇒ Object

TODO move sincedb to watch.rb see TODO there



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
60
61
62
63
# File 'lib/filewatch/tail_base.rb', line 25

def initialize(opts={})
  @iswindows = ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) != nil)

  if opts[:logger]
    @logger = opts[:logger]
  else
    @logger = Logger.new(STDERR)
    @logger.level = Logger::INFO
  end
  @lastwarn = Hash.new { |h, k| h[k] = 0 }
  @buffers = {}
  @watch = FileWatch::Watch.new
  @watch.logger = @logger
  @sincedb_last_write = 0
  @sincedb = {}
  @opts = {
    :sincedb_write_interval => 10,
    :stat_interval => 1,
    :discover_interval => 5,
    :exclude => [],
    :start_new_files_at => :end,
    :delimiter => "\n"
  }.merge(opts)
  if !@opts.include?(:sincedb_path)
    @opts[:sincedb_path] = File.join(ENV["HOME"], ".sincedb") if ENV.include?("HOME")
    @opts[:sincedb_path] = ENV["SINCEDB_PATH"] if ENV.include?("SINCEDB_PATH")
  end
  if !@opts.include?(:sincedb_path)
    raise NoSinceDBPathGiven.new("No HOME or SINCEDB_PATH set in environment. I need one of these set so I can keep track of the files I am following.")
  end
  @watch.exclude(@opts[:exclude])
  @watch.close_older = @opts[:close_older]
  @watch.ignore_older = @opts[:ignore_older]
  @watch.delimiter = @opts[:delimiter]
  @watch.max_open_files = @opts[:max_open_files]
  @delimiter_byte_size = @opts[:delimiter].bytesize

  _sincedb_open
end

#quitObject

quit is a sort-of finalizer, it should be called for clean up before the instance is disposed of.



210
211
212
213
214
# File 'lib/filewatch/tail_base.rb', line 210

def quit
  @watch.quit # <-- should close all the files
  # and that should allow the sincedb_write to succeed if it could not before
  _sincedb_write
end

#sincedb_record_uid(path, stat) ⇒ Object



77
78
79
80
# File 'lib/filewatch/tail_base.rb', line 77

def sincedb_record_uid(path, stat)
  # retain this call because its part of the public API
  @watch.inode(path, stat)
end

#sincedb_write(reason = nil) ⇒ Object



166
167
168
169
# File 'lib/filewatch/tail_base.rb', line 166

def sincedb_write(reason=nil)
  @logger.debug? && @logger.debug("caller requested sincedb write (#{reason})")
  _sincedb_write
end

#tail(path) ⇒ Object



72
73
74
# File 'lib/filewatch/tail_base.rb', line 72

def tail(path)
  @watch.watch(path)
end