Class: DirectoryWatcher::CoolioScanner

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

Overview

The CoolioScanner uses the Coolio 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) ⇒ CoolioScanner

call-seq:

CoolioScanner.new { |events| block }

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



24
25
26
27
# File 'lib/directory_watcher/coolio_scanner.rb', line 24

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.



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

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 Coolio loop. This method will check for added files and stable files and notify the directory watcher accordingly.



101
102
103
104
105
# File 'lib/directory_watcher/coolio_scanner.rb', line 101

def _on_timer
  _find_added
  _find_stable
  notify
end

#startObject

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



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

def start
  return if running?

  @timer = Timer.new self
  @thread = Thread.new {
    coolio_loop = Thread.current._coolio_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(coolio_loop)
    coolio_loop.run
  }
end

#stopObject

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



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

def stop
  return unless running?

  @timer.detach
  @timer = nil

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

  notify

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