Collector reads items from a collection Queue and processes them to see if FileEvents should be put onto the notification Queue.
Create a new StatCollector from the given Configuration, and an optional
Scan.
configuration - The Collector uses from Configuration:
collection_queue - The Queue to read items from the Scanner on
notification_queue - The Queue to submit the Events to the Notifier on
stable - The number of times we see a file hasn't changed before
emitting a stable event
sort_by - the method used to sort events during on_scan results
order_by - The method used to order events from call to on_scan
pre_load_scan - A Scan to use to load our internal state from before. No
events will be emitted for the FileStat's in this scan.
def initialize( notification_queue, collection_queue, options = {} )
# File lib/directory_watcher/collector.rb, line 23 def initialize( config ) @stats = Hash.new @stable_counts = Hash.new @config = config on_scan( DirectoryWatcher::Scan.new( config.glob ), false ) if config.pre_load? self.interval = 0.01 # yes this is a fast loop end
The queue from which to read items from the scanners. See Configuration.
# File lib/directory_watcher/collector.rb, line 51 def collection_queue @config.collection_queue end
Write the current stats to the given IO object as a YAML document.
io - The IO object to write the document to.
Returns nothing.
# File lib/directory_watcher/collector.rb, line 122 def dump_stats( io ) YAML.dump(@stats, io) end
Read the current stats from the given IO object. Any existing stats in the Collector will be overwritten
io - The IO object from which to read the document.
Returns nothing.
# File lib/directory_watcher/collector.rb, line 132 def load_stats( io ) @stats = YAML.load(io) end
The queue to write Events for the Notifier. See Configuration.
# File lib/directory_watcher/collector.rb, line 57 def notification_queue @config.notification_queue end
Given the scan, update the set of stats with the results from the Scan and emit events to the notification queue as appropriate.
scan - The Scan containing all the new FileStat items emit_events - Should events be emitted for the events in the scan
(default: true)
There is one odd thing that happens here. Scanners that are EventableScanners use on_stat to emit removed events, and the standard threaded Scanner only uses Scans. So we make sure and only emit removed events in this method if the scanner that gave us the scan was the basic threaded Scanner.
TODO: Possibly fix this through another abstraction in the Scanners. No idea about what that would be yet.
Returns nothing.
# File lib/directory_watcher/collector.rb, line 78 def on_scan( scan, emit_events = true ) seen_paths = Set.new logger.debug "Sorting by #{sort_by} #{order_by}" sorted_stats( scan.run ).each do |stat| on_stat(stat, emit_events) seen_paths << stat.path end emit_removed_events(seen_paths) if @config.scanner.nil? end
Process a single stat and emit an event if necessary.
stat - The new FileStat to process and see if an event should
be emitted
emit_event - Whether or not an event should be emitted.
Returns nothing
# File lib/directory_watcher/collector.rb, line 95 def on_stat( stat, emit_event = true ) orig_stat = update_stat( stat ) logger.debug "Emitting event for on_stat #{stat}" emit_event_for( orig_stat, stat ) if emit_event end
How to order Scan results. See Configuration.
# File lib/directory_watcher/collector.rb, line 45 def order_by @config.order_by end
Remove one item from the collection queue and process it.
This method is required by the Threaded API
Returns nothing
# File lib/directory_watcher/collector.rb, line 106 def run case thing = collection_queue.deq when ::DirectoryWatcher::Scan on_scan(thing) when ::DirectoryWatcher::FileStat on_stat(thing) else raise "Unknown item in the queue: #{thing}" end end
How to sort Scan results. See Configuration.
# File lib/directory_watcher/collector.rb, line 39 def sort_by @config.sort_by end
The number of times we see a file hasn't changed before emitting a stable count. See Configuration#stable
# File lib/directory_watcher/collector.rb, line 33 def stable_threshold @config.stable end
Generated with the Darkfish Rdoc Generator 2.