Class: FeatherWatch::Core::LinuxWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/feather_watch/core/linux_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(directories, callback, verbose = false, silence_exceptions = false) ⇒ LinuxWatcher

Returns a new instance of LinuxWatcher.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/feather_watch/core/linux_watcher.rb', line 3

def initialize(directories, callback, verbose= false, silence_exceptions= false)
	@verbose = verbose
	@silence_exceptions = silence_exceptions
	puts "Initializing linux watcher" if @verbose
	@notifiers = []
	directories = [directories] if directories.is_a?(String)
	directories.each do |dir|
		notifier = INotify::Notifier.new
		@notifiers << notifier
		#Avaliable events: :access, :attrib, :close_write, :close_nowrite, :create, :delete, :delete_self, :ignored, :modify, :move_self, :moved_from, :moved_to, :open
		notifier.watch(dir, :recursive, :create, :attrib, :delete, :close_write, :delete_self, :modify, :move_self, :moved_from, :moved_to) do |event|
			#TODO: This information is probably in the event, but I'm on a mac now, so I can't test it properly
			
			begin
				if    !([:attrib, :close_write, :modify] & event.flags ).empty?
					puts "Change on file: #{event.absolute_name}" if @verbose
					callback.call({status: :modified, file: event.absolute_name, event: event})
				elsif !([:moved_to]                      & event.flags ).empty?
					puts "File added: #{event.absolute_name}"     if @verbose
					callback.call({status: :added, file: event.absolute_name, event: event})
				elsif !([:moved_from]                    & event.flags ).empty?
					puts "File removed: #{event.absolute_name}"   if @verbose
					callback.call({status: :removed, file: event.absolute_name, event: event})
				elsif !([:create]                        & event.flags ).empty?
					puts "File added: #{event.absolute_name}"     if @verbose
					callback.call({status: :added, file: event.absolute_name, event: event})
				elsif !([:delete, :delete_self]          & event.flags ).empty?
					puts "File removed: #{event.absolute_name}"   if @verbose
					callback.call({status: :removed, file: event.absolute_name, event: event})
				else
					STDERR.puts "Unhandled status flags: #{event.flags} for file #{event.absolute_name}" if @verbose
				end
			rescue Exception => e
				unless @silence_exceptions
					STDERR.puts "----------------------------"
					STDERR.puts "Error in Feather Watch callback"
					STDERR.puts "Message: #{e.message}"
					STDERR.puts "backtrace: #{e.backtrace * "\n\t >"}"
				end
			end
		end
	end
end

Instance Method Details

#startObject



47
48
49
50
51
52
53
54
# File 'lib/feather_watch/core/linux_watcher.rb', line 47

def start
	puts "Starting linux watcher" if @verbose
	@notifiers.each do |notifier|
		Thread.new do
			notifier.run
		end
	end
end

#stopObject



56
57
58
59
60
61
# File 'lib/feather_watch/core/linux_watcher.rb', line 56

def stop
	puts "Stopping linux watcher" if @verbose
	@notifiers.each do |notifier|
		notifier.stop
	end
end