Class: FileWatch::Watch
- Inherits:
-
Object
- Object
- FileWatch::Watch
- Defined in:
- lib/filewatch/watch.rb
Defined Under Namespace
Classes: WatchedFile
Instance Attribute Summary collapse
-
#ignore_after ⇒ Object
Returns the value of attribute ignore_after.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #discover ⇒ Object
- #each(&block) ⇒ Object
- #exclude(path) ⇒ Object
-
#initialize(opts = {}) ⇒ Watch
constructor
A new instance of Watch.
- #inode(path, stat) ⇒ Object
- #quit ⇒ Object
- #subscribe(stat_interval = 1, discover_interval = 5, &block) ⇒ Object
-
#unwatch(path) ⇒ Object
def watch.
- #watch(path) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Watch
Returns a new instance of Watch.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/filewatch/watch.rb', line 51 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 @watching = [] @exclude = [] @files = Hash.new { |h, k| h[k] = WatchedFile.new(k, false, false) } @unwatched = Hash.new # we need to be threadsafe about the mutation # of the above 2 ivars because the public # methods each, discover, watch and unwatch # can be called from different threads. @lock = Mutex.new # we need to be threadsafe about the quit mutation @quit = false @quit_lock = Mutex.new end |
Instance Attribute Details
#ignore_after ⇒ Object
Returns the value of attribute ignore_after.
48 49 50 |
# File 'lib/filewatch/watch.rb', line 48 def ignore_after @ignore_after end |
#logger ⇒ Object
Returns the value of attribute logger.
48 49 50 |
# File 'lib/filewatch/watch.rb', line 48 def logger @logger end |
Instance Method Details
#discover ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/filewatch/watch.rb', line 182 def discover synchronized do @watching.each do |path| _discover_file(path) do |filepath, stat| WatchedFile.new_ongoing(filepath, inode(filepath, stat)) end end end end |
#each(&block) ⇒ Object
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 |
# File 'lib/filewatch/watch.rb', line 126 def each(&block) synchronized do # Send any creates. @files.each do |path, watched_file| if !watched_file.create_sent? if watched_file.initial? yield(:create_initial, path) else yield(:create, path) end watched_file.create_sent = true end end @files.each do |path, watched_file| begin stat = File::Stat.new(path) rescue Errno::ENOENT # file has gone away or we can't read it anymore. @files.delete(path) @logger.debug? && @logger.debug("#{path}: stat failed (#{$!}), deleting from @files") yield(:delete, path) next end if expired?(stat, watched_file) if !watched_file.timeout_sent? @logger.debug? && @logger.debug("#{path}: file expired") yield(:timeout, path) watched_file.timeout_sent = true end next end inode = inode(path,stat) old_size = watched_file.size if inode != watched_file.inode @logger.debug? && @logger.debug("#{path}: old inode was #{watched_file.inode.inspect}, new is #{inode.inspect}") yield(:delete, path) yield(:create, path) elsif stat.size < old_size @logger.debug? && @logger.debug("#{path}: file rolled, new size is #{stat.size}, old size #{old_size}") yield(:delete, path) yield(:create, path) elsif stat.size > old_size @logger.debug? && @logger.debug("#{path}: file grew, old size #{old_size}, new size #{stat.size}") yield(:modify, path) end watched_file.update(stat, inode) end end end |
#exclude(path) ⇒ Object
74 75 76 |
# File 'lib/filewatch/watch.rb', line 74 def exclude(path) path.to_a.each { |p| @exclude << p } end |
#inode(path, stat) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/filewatch/watch.rb', line 109 def inode(path,stat) if @iswindows fileId = Winhelper.GetWindowsUniqueFileIdentifier(path) inode = [fileId, 0, 0] # dev_* doesn't make sense on Windows else inode = [stat.ino.to_s, stat.dev_major, stat.dev_minor] end return inode end |
#quit ⇒ Object
293 294 295 |
# File 'lib/filewatch/watch.rb', line 293 def quit @quit_lock.synchronize { @quit = true } end |
#subscribe(stat_interval = 1, discover_interval = 5, &block) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/filewatch/watch.rb', line 193 def subscribe(stat_interval = 1, discover_interval = 5, &block) glob = 0 reset_quit while !quit? each(&block) glob += 1 if glob == discover_interval discover glob = 0 end sleep(stat_interval) end end |
#unwatch(path) ⇒ Object
def watch
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/filewatch/watch.rb', line 91 def unwatch(path) synchronized do result = false if @watching.delete(path) _globbed_files(path).each do |file| deleted = @files.delete(file) @unwatched[file] = deleted if deleted end result = true else result = @files.delete(path) @unwatched[path] = result if result end return !!result end end |
#watch(path) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/filewatch/watch.rb', line 79 def watch(path) synchronized do if !@watching.member?(path) @watching << path _discover_file(path) do |filepath, stat| WatchedFile.new_initial(filepath, inode(filepath, stat)) end end end return true end |