Class: DirectoryWatcher::EventableScanner

Inherits:
Object
  • Object
show all
Includes:
Logable
Defined in:
lib/directory_watcher/eventable_scanner.rb

Overview

An Eventable Scanner is one that can be utilized by something that has an Event Loop. It is intended to be subclassed by classes that implement the specific event loop semantics for say EventMachine or Cool.io.

The Events that the EventableScanner is programmed for are:

on_scan - this should be called every interval times on_modified - If the event loop can monitor individual files then this should

be called when the file is modified

on_removed - Similar to on_modified but called when a file is removed.

Sub classes are required to implement the following:

start_loop_with_attached_scan_timer() - Instance Method
  This method is to start up the loop, if necessary assign to @loop_thread
  instance variable the Thread that is controlling the event loop.

  This method must also assign an object to @timer which is what does the
  periodic scanning of the globs. This object must respond to +detach()+ so
  that it may be detached from the event loop.

stop_loop() - Instance Method
  This method must shut down the event loop, or detach these classes from
  the event loop if we just attached to an existing event loop.

Watcher - An Embedded class
  This is a class that must have a class method +watcher(path,scanner)+
  which is used to instantiate a file watcher. The Watcher instance must
  respond to +detach()+ so that it may be independently detached from the
  event loop.

Direct Known Subclasses

CoolioScanner, EmScanner, RevScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logable

default_logger, #logger

Constructor Details

#initialize(config) ⇒ EventableScanner

call-seq:

EventableScanner.new( config )

config - the Configuration instances


43
44
45
46
47
48
49
50
51
# File 'lib/directory_watcher/eventable_scanner.rb', line 43

def initialize( config )
  @config = config
  @scan_and_queue = DirectoryWatcher::ScanAndQueue.new(config.glob, config.collection_queue)
  @watchers = {}
  @stopping = false
  @timer = nil
  @loop_thread = nil
  @paused = false
end

Instance Attribute Details

#iterationsObject (readonly)

Returns the value of attribute iterations


142
143
144
# File 'lib/directory_watcher/eventable_scanner.rb', line 142

def iterations
  @iterations
end

#maximum_iterationsObject

Returns the value of attribute maximum_iterations


141
142
143
# File 'lib/directory_watcher/eventable_scanner.rb', line 141

def maximum_iterations
  @maximum_iterations
end

#watchersObject (readonly)

A Hash of Watcher objects.


36
37
38
# File 'lib/directory_watcher/eventable_scanner.rb', line 36

def watchers
  @watchers
end

Instance Method Details

#collection_queueObject

The queue on which to put FileStat and Scan items.


55
56
57
# File 'lib/directory_watcher/eventable_scanner.rb', line 55

def collection_queue
  @config.collection_queue
end

#finished_iterations?Boolean

Have we completed up to the maximum_iterations?

Returns:

  • (Boolean)

146
147
148
# File 'lib/directory_watcher/eventable_scanner.rb', line 146

def finished_iterations?
  self.iterations >= self.maximum_iterations
end

#intervalObject

The interval at which to scan


61
62
63
# File 'lib/directory_watcher/eventable_scanner.rb', line 61

def interval
  @config.interval
end

#join(limit = nil) ⇒ Object

EventableScanners do not join


119
120
# File 'lib/directory_watcher/eventable_scanner.rb', line 119

def join( limit = nil )
end

#on_modified(watcher, new_stat) ⇒ Object

This callback is invoked by the Watcher instance when it is triggered by the loop for file modifications.


163
164
165
166
# File 'lib/directory_watcher/eventable_scanner.rb', line 163

def on_modified(watcher, new_stat)
  logger.debug "on_modified called"
  queue_item(new_stat)
end

#on_removed(watcher, new_stat) ⇒ Object

This callback is invoked by the Watcher instance when it is triggered by the loop for file removals


171
172
173
174
175
# File 'lib/directory_watcher/eventable_scanner.rb', line 171

def on_removed(watcher, new_stat)
  logger.debug "on_removed called"
  unwatch_file(watcher.path)
  queue_item(new_stat)
end

#on_scanObject

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


154
155
156
157
158
# File 'lib/directory_watcher/eventable_scanner.rb', line 154

def on_scan
  logger.debug "on_scan called"
  scan_and_watch_files
  progress_towards_maximum_iterations
end

#pauseObject

Pause the scanner.

Pausing the scanner does not stop the scanning per se, it stops items from being sent to the collection queue


97
98
99
100
# File 'lib/directory_watcher/eventable_scanner.rb', line 97

def pause
  logger.debug "pausing scanner"
  @paused = true
end

#paused?Boolean

Is the Scanner currently paused.

Returns:

  • (Boolean)

113
114
115
# File 'lib/directory_watcher/eventable_scanner.rb', line 113

def paused?
  @paused
end

#resumeObject

Resume the scanner.

This removes the blockage on sending items to the collection queue.


106
107
108
109
# File 'lib/directory_watcher/eventable_scanner.rb', line 106

def resume
  logger.debug "resuming scanner"
  @paused = false
end

#runObject

Do a single scan and send those items to the collection queue.


124
125
126
127
# File 'lib/directory_watcher/eventable_scanner.rb', line 124

def run
  logger.debug "running scan and queue"
  @scan_and_queue.scan_and_queue
end

#running?Boolean

Returns true if the scanner is currently running. Returns false if this is not the case.

Returns:

  • (Boolean)

68
69
70
71
# File 'lib/directory_watcher/eventable_scanner.rb', line 68

def running?
  return !@stopping if @timer
  return false
end

#startObject

Start up the scanner. If the scanner is already running, nothing happens.


75
76
77
78
79
# File 'lib/directory_watcher/eventable_scanner.rb', line 75

def start
  return if running?
  logger.debug "starting scanner"
  start_loop_with_attached_scan_timer
end

#stopObject

Stop the scanner. If the scanner is not running, nothing happens.


83
84
85
86
87
88
89
90
# File 'lib/directory_watcher/eventable_scanner.rb', line 83

def stop
  return unless running?
  logger.debug "stoping scanner"
  @stopping = true
  teardown_timer_and_watches
  @stopping = false
  stop_loop
end