DirectoryWatcher::EmScanner

The EmScanner uses the EventMachine reactor loop to monitor changes to files in the watched directory. This scanner is more efficient than the pure Ruby scanner because it relies on the operating system kernel notifictions instead of a periodic polling and stat of every file in the watched directory (the technique used by the Scanner class).

EventMachine cannot notify us when a file is added to the watched directory; therefore, added files are only picked up when we apply the glob pattern to the directory. This is done at the configured interval.

Notes:

* Kqueue does not generate notifications when "touch" is used to update
  a file's timestamp. This applies to Mac and BSD systems.

* New files are detected only when the watched directory is polled at the
  configured interval.

Public Class Methods

new { |events| block } click to toggle source

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

# File lib/directory_watcher/em_scanner.rb, line 39
def initialize( &block )
  super(&block)
  @timer = nil
  @run_loop = lambda {_run_loop}
  @watchers = {}
end

Public Instance Methods

join( limit = nil ) click to toggle source

This is a no-op method for the EventMachine file scanner.

# File lib/directory_watcher/em_scanner.rb, line 104
def join( limit = nil )
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/em_scanner.rb, line 49
def running?
  !@timer.nil?
end
start() click to toggle source

Start the EventMachine scanner. If the scanner has already been started this method will return without taking any action.

If the EventMachine reactor is not running, it will be started by this method.

# File lib/directory_watcher/em_scanner.rb, line 59
def start
  return if running?

  unless EventMachine.reactor_running?
    @thread = Thread.new {EventMachine.run}
    Thread.pass until EventMachine.reactor_running?
  end

  @files.keys.each do |fn|
    if test ee, fn
      _watch_file fn
      next
    end

    @files.delete fn
    @events << ::DirectoryWatcher::Event.new(:removed, fn)
  end

  _run_loop
end
stop() click to toggle source

Stop the EventMachine scanner. If the scanner is already stopped this method will return without taking any action.

The EventMachine reactor will not be stopped by this method. It is up to the user to stop the reactor using the EventMachine#stop_event_loop method.

# File lib/directory_watcher/em_scanner.rb, line 87
def stop
  return unless running?

  EventMachine.cancel_timer @timer rescue nil
  @timer = nil

  @watchers.each_value {|w| w.stop_watching if w.active?}
  @watchers.clear

  notify
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.