The Scanner is responsible for polling the watched directory at a regular interval and generating events when files are modified, added or removed. These events are passed to the DirectoryWatcher which notifies the registered observers.
The Scanner is a pure Ruby class, and as such it works across all Ruby interpreters on the major platforms. This also means that it can be processor intensive for large numbers of files or very fast update intervals. Your mileage will vary, but it is something to keep an eye on.
If the scanner thread is running, the calling thread will suspend execution and run the scanner thread. This method does not return until the scanner thread is stopped or until limit seconds have passed.
If the scanner thread is not running, this method returns immediately with nil.
# File lib/directory_watcher/scanner.rb, line 86 def join( limit = nil ) return unless running? @thread.join limit end
Reset the scanner state by clearing the stored file list. Passing true to this method will cause the file list to be pre-loaded after it has been cleared effectively skipping the initial round of file added events that would normally be generated.
# File lib/directory_watcher/scanner.rb, line 71 def reset( pre_load = false ) @events.clear @files = (pre_load ? scan_files : Hash.new) end
Performs exactly one scan of the directory for file changes and notifies the observers.
# File lib/directory_watcher/scanner.rb, line 94 def run_once files = scan_files keys = [files.keys, @files.keys] # current files, previous files find_added(files, *keys) find_modified(files, *keys) find_removed(*keys) notify @files = files # store the current file list for the next iteration self end
Returns true if the scanner is currently running. Returns false if this is not the case.
# File lib/directory_watcher/scanner.rb, line 34 def running? !@thread.nil? end
Start the scanner thread. If the scanner is already running, this method will return without taking any action.
# File lib/directory_watcher/scanner.rb, line 41 def start return if running? @stop = false @thread = Thread.new(self) {|scanner| scanner.__send__ :run_loop} self end
Stop the scanner thread. If the scanner is already stopped, this method will return without taking any action.
# File lib/directory_watcher/scanner.rb, line 52 def stop return unless running? @stop = true @thread.wakeup if @thread.status == 'sleep' @thread.join self ensure @thread = nil end
Generated with the Darkfish Rdoc Generator 2.