Class: FileWatch::Watch
- Inherits:
-
Object
- Object
- FileWatch::Watch
- Defined in:
- lib/filewatch/watch.rb
Overview
TODO make a WatchedFilesDb class that holds the watched_files instead of a hash it should support an ‘identity’ of path + inode it should be serializable instead of the sincedb it should be deserializable to recreate the exact state all files were in as last seen some parts of the each method should be handled by it, e.g. wfs_db.<state>_iterator{|wf| }, trapping the Errno::ENOENT, auto_delete and yield wtached_file
Constant Summary collapse
- MAX_FILES_WARN_INTERVAL =
ENV.fetch("FILEWATCH_MAX_FILES_WARN_INTERVAL", 20).to_i
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#max_active ⇒ Object
readonly
Returns the value of attribute max_active.
Class Method Summary collapse
Instance Method Summary collapse
- #close_older=(value) ⇒ Object
-
#discover ⇒ Object
def each.
-
#each(&block) ⇒ Object
Calls &block with params [event_type, path] event_type can be one of: :create_initial - initially present file (so start at end for tail) :create - file is created (new file after initial globs, start at 0) :modify - file is modified (size increases) :delete - file is deleted :timeout - file is closable :unignore - file was ignored, but since then it received new content.
- #exclude(path) ⇒ Object
- #ignore_older=(value) ⇒ Object
-
#initialize(opts = {}) ⇒ Watch
constructor
A new instance of Watch.
-
#inode(path, stat) ⇒ Object
def watch.
- #max_open_files=(value) ⇒ Object
-
#quit ⇒ Object
def subscribe.
-
#quit? ⇒ Boolean
def quit.
- #subscribe(stat_interval = 1, discover_interval = 5, &block) ⇒ Object
- #watch(path) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Watch
Returns a new instance of Watch.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/filewatch/watch.rb', line 39 def initialize(opts={}) if opts[:logger] @logger = opts[:logger] else @logger = Logger.new(STDERR) @logger.level = Logger::INFO end @watching = [] @exclude = [] @files = Hash.new # we need to be threadsafe about the mutation # of the above 2 ivars because the public # methods each, discover and watch # can be called from different threads. @lock = Mutex.new # we need to be threadsafe about the quit mutation @quit = false @quit_lock = Mutex.new self.max_open_files = ENV["FILEWATCH_MAX_OPEN_FILES"].to_i @lastwarn_max_files = 0 end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
36 37 38 |
# File 'lib/filewatch/watch.rb', line 36 def delimiter @delimiter end |
#logger ⇒ Object
Returns the value of attribute logger.
35 36 37 |
# File 'lib/filewatch/watch.rb', line 35 def logger @logger end |
#max_active ⇒ Object (readonly)
Returns the value of attribute max_active.
37 38 39 |
# File 'lib/filewatch/watch.rb', line 37 def max_active @max_active end |
Class Method Details
.inode(path, stat) ⇒ Object
31 32 33 |
# File 'lib/filewatch/watch.rb', line 31 def self.inode(path, stat) send(FILEWATCH_INODE_METHOD, path, stat) end |
.nix_inode(path, stat) ⇒ Object
27 28 29 |
# File 'lib/filewatch/watch.rb', line 27 def self.nix_inode(path, stat) [stat.ino.to_s, stat.dev_major, stat.dev_minor] end |
.win_inode(path, stat) ⇒ Object
22 23 24 25 |
# File 'lib/filewatch/watch.rb', line 22 def self.win_inode(path, stat) fileId = Winhelper.GetWindowsUniqueFileIdentifier(path) [fileId, 0, 0] # dev_* doesn't make sense on Windows end |
Instance Method Details
#close_older=(value) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/filewatch/watch.rb', line 79 def close_older=(value) if !value.nil? val = value.to_f val = val <= 0 ? nil : val end @close_older = val end |
#discover ⇒ Object
def each
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/filewatch/watch.rb', line 264 def discover synchronized do @watching.each do |path| _discover_file(path) do |filepath, stat| WatchedFile.new_ongoing( filepath, inode(filepath, stat), stat ).init_vars(@delimiter, @ignore_older, @close_older) end end end end |
#each(&block) ⇒ Object
Calls &block with params [event_type, path] event_type can be one of:
:create_initial - initially present file (so start at end for tail)
:create - file is created (new file after initial globs, start at 0)
:modify - file is modified (size increases)
:delete - file is deleted
:timeout - file is closable
:unignore - file was ignored, but since then it received new content
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/filewatch/watch.rb', line 117 def each(&block) synchronized do return if @files.empty? begin file_deletable = [] # creates this array just once watched_files = @files.values # look at the closed to see if its changed watched_files.select {|wf| wf.closed? }.each do |watched_file| path = watched_file.path break if quit? begin stat = watched_file.restat if watched_file.size_changed? || watched_file.inode_changed?(inode(path,stat)) # if the closed file changed, move it to the watched state # not to active state because we want to use MAX_OPEN_FILES throttling. watched_file.watch end rescue Errno::ENOENT # file has gone away or we can't read it anymore. file_deletable << path @logger.debug? && @logger.debug("each: closed?: stat failed: #{path}: (#{$!}), deleting from @files") rescue => e @logger.error("each: closed?: #{path}: (#{e.inspect})") end end return if quit? # look at the ignored to see if its changed watched_files.select {|wf| wf.ignored? }.each do |watched_file| path = watched_file.path break if quit? begin stat = watched_file.restat if watched_file.size_changed? || watched_file.inode_changed?(inode(path,stat)) # if the ignored file changed, move it to the watched state # not to active state because we want to use MAX_OPEN_FILES throttling. # this file has not been yielded to the block yet # but we must have the tail to start from the end, so when the file # was first ignored we updated the bytes_read to the stat.size at that time. # by adding this to the sincedb so that the subsequent modify # event can detect the change watched_file.watch yield(:unignore, watched_file) end rescue Errno::ENOENT # file has gone away or we can't read it anymore. file_deletable << path @logger.debug? && @logger.debug("each: ignored: stat failed: #{path}: (#{$!}), deleting from @files") rescue => e @logger.error("each: ignored?: #{path}: (#{e.inspect})") end end return if quit? # Send any creates. if (to_take = @max_active - watched_files.count{|wf| wf.active?}) > 0 watched_files.select {|wf| wf.watched? }.take(to_take).each do |watched_file| break if quit? path = watched_file.path begin stat = watched_file.restat watched_file.activate # don't do create again next if watched_file.state_history_any?(:closed, :ignored) # if the file can't be opened during the yield # its state is set back to watched sym = watched_file.initial? ? :create_initial : :create yield(sym, watched_file) rescue Errno::ENOENT # file has gone away or we can't read it anymore. file_deletable << path watched_file.unwatch yield(:delete, watched_file) next @logger.debug? && @logger.debug("each: closed: stat failed: #{path}: (#{$!}), deleting from @files") rescue => e @logger.error("each: watched?: #{path}: (#{e.inspect})") end end else now = Time.now.to_i if (now - @lastwarn_max_files) > MAX_FILES_WARN_INTERVAL waiting = @files.size - @max_active specific = if @close_older.nil? ", try setting close_older. There are #{waiting} unopened files" else ", files yet to open: #{waiting}" end @logger.warn(@max_warn_msg + specific) @lastwarn_max_files = now end end return if quit? # wf.active means the actual files were opened # and have been read once - unless they were empty at the time watched_files.select {|wf| wf.active? }.each do |watched_file| path = watched_file.path break if quit? begin stat = watched_file.restat rescue Errno::ENOENT # file has gone away or we can't read it anymore. file_deletable << path @logger.debug? && @logger.debug("each: active: stat failed: #{path}: (#{$!}), deleting from @files") watched_file.unwatch yield(:delete, watched_file) next rescue => e @logger.error("each: active?: #{path}: (#{e.inspect})") next end if watched_file.file_closable? @logger.debug? && @logger.debug("each: active: file expired: #{path}") yield(:timeout, watched_file) watched_file.close next end _inode = inode(path,stat) read_thus_far = watched_file.bytes_read # we don't update the size here, its updated when we actually read if watched_file.inode_changed?(_inode) @logger.debug? && @logger.debug("each: new inode: #{path}: old inode was #{watched_file.inode.inspect}, new is #{_inode.inspect}") watched_file.update_inode(_inode) yield(:delete, watched_file) yield(:create, watched_file) elsif stat.size < read_thus_far @logger.debug? && @logger.debug("each: file rolled: #{path}: new size is #{stat.size}, old size #{read_thus_far}") yield(:delete, watched_file) yield(:create, watched_file) elsif stat.size > read_thus_far @logger.debug? && @logger.debug("each: file grew: #{path}: old size #{read_thus_far}, new size #{stat.size}") yield(:modify, watched_file) end end ensure file_deletable.each {|f| @files.delete(f)} end end end |
#exclude(path) ⇒ Object
87 88 89 |
# File 'lib/filewatch/watch.rb', line 87 def exclude(path) path.to_a.each { |p| @exclude << p } end |
#ignore_older=(value) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/filewatch/watch.rb', line 70 def ignore_older=(value) #nil is allowed but 0 and negatives are made nil if !value.nil? val = value.to_f val = val <= 0 ? nil : val end @ignore_older = val end |
#inode(path, stat) ⇒ Object
def watch
105 106 107 |
# File 'lib/filewatch/watch.rb', line 105 def inode(path, stat) self.class.inode(path, stat) end |
#max_open_files=(value) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/filewatch/watch.rb', line 63 def max_open_files=(value) val = value.to_i val = 4095 if value.nil? || val <= 0 @max_warn_msg = "Reached open files limit: #{val}, set by the 'max_open_files' option or default" @max_active = val end |
#quit ⇒ Object
def subscribe
293 294 295 |
# File 'lib/filewatch/watch.rb', line 293 def quit @quit_lock.synchronize { @quit = true } end |
#quit? ⇒ Boolean
def quit
297 298 299 |
# File 'lib/filewatch/watch.rb', line 297 def quit? @quit_lock.synchronize { @quit } end |
#subscribe(stat_interval = 1, discover_interval = 5, &block) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/filewatch/watch.rb', line 276 def subscribe(stat_interval = 1, discover_interval = 5, &block) glob = 0 reset_quit while !quit? each(&block) break if quit? glob += 1 if glob == discover_interval discover glob = 0 end break if quit? sleep(stat_interval) end @files.values.each(&:file_close) end |
#watch(path) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/filewatch/watch.rb', line 91 def watch(path) synchronized do if !@watching.member?(path) @watching << path _discover_file(path) do |filepath, stat| WatchedFile.new_initial( filepath, inode(filepath, stat), stat ).init_vars(@delimiter, @ignore_older, @close_older) end end end return true end |