Class: DirectoryWatcher::RevScanner

Inherits:
Scanner
  • Object
show all
Defined in:
lib/directory_watcher/rev_scanner.rb

Overview

The RevScanner uses the Rev 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).

Defined Under Namespace

Classes: Timer, Watcher

Instance Attribute Summary

Attributes inherited from Scanner

#files, #glob, #interval, #stable

Instance Method Summary collapse

Methods inherited from Scanner

#join, #reset, #run_once, #running?

Constructor Details

#initialize(&block) ⇒ RevScanner

call-seq:

RevScanner.new { |events| block }

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



25
26
27
28
# File 'lib/directory_watcher/rev_scanner.rb', line 25

def initialize( &block )
  super(&block)
  @watchers = {}
end

Instance Method Details

#_on_change(watcher) ⇒ Object

:stopdoc:

This callback is invoked by a Watcher instance when some change has occured on the file. The scanner determines if the file has been modified or deleted and notifies the directory watcher accordingly.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/directory_watcher/rev_scanner.rb', line 79

def _on_change( watcher )
  fn = watcher.path
  stat = watcher.stat

  if stat
    if @files[fn] != stat
      @files[fn] = stat
      @events << ::DirectoryWatcher::Event.new(:modified, fn)
    end
  else
    watcher.detach
    @watchers.delete fn
    @files.delete fn
    @events << ::DirectoryWatcher::Event.new(:removed, fn)
  end

  notify
end

#_on_timerObject

This callback is invoked by the Timer instance when it is triggered by the Rev loop. This method will check for added files and stable files and notify the directory watcher accordingly.



102
103
104
105
106
# File 'lib/directory_watcher/rev_scanner.rb', line 102

def _on_timer
  _find_added
  _find_stable
  notify
end

#startObject

Start the Rev scanner loop. If the scanner is already running, this method will return without taking any action.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/directory_watcher/rev_scanner.rb', line 33

def start
  return if running?

  @timer = Timer.new self
  @thread = Thread.new {
    rev_loop = Thread.current._rev_loop
    @files.keys.each do |fn|
      if test ?e, fn
        _watch_file fn
        next
      end

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

    @timer.attach rev_loop
    rev_loop.run
  }
end

#stopObject

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/directory_watcher/rev_scanner.rb', line 57

def stop
  return unless running?

  @timer.detach
  @timer = nil

  @watchers.each_value {|w| w.detach}
  @watchers.clear

  notify

  @thread._rev_loop.stop rescue nil
  @thread.kill    # for some reason the rev loop is not returning after stopping
  @thread = nil
end