Parent

DirectoryWatcher::Scanner

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.

Attributes

files[RW]
glob[RW]
interval[RW]
stable[RW]

Public Class Methods

new { |events| block } click to toggle source

Create a thread-based scanner that will generate file events and pass those events (as an array) to the given block.

# File lib/directory_watcher/scanner.rb, line 25
def initialize( &block )
  @events = []
  @thread = nil
  @notify = block;
end

Public Instance Methods

join( limit = nil ) click to toggle source

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( pre_load = false ) click to toggle source

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
run_once() click to toggle source

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
running?() click to toggle source

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() click to toggle source

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() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.