Skip to content

Instantly share code, notes, and snippets.

@timfel
Created May 7, 2012 16:12
Show Gist options
  • Save timfel/2628695 to your computer and use it in GitHub Desktop.
Save timfel/2628695 to your computer and use it in GitHub Desktop.
inotify example
class TfsWatcher
def initialize(directory, host, port, recurse)
@i = Inotify.new
@f = Fam.new
@f.protocol "FAM"
@f.open host, port
recurse ? Find.find(directory) {|dir| add(dir) } : add(directory)
end
def add(dir)
@i.add_watch(dir, Inotify::CREATE | Inotify::DELETE | Inotify::MODIFY | Inotify::OPEN | Inotify::CLOSE)
end
def start
@t = Thread.new do
@i.each_event do |ev|
dir, file = ev.name.split("/")[0..-1], File.basename(ev.name)
case ev.mask
when Inotify::CREATE
@f.send "Created", dir, file
when Inotify::DELETE
@f.send "Deleted", dir, file
when Inotify::MODIFY
@f.send "Changed", dir, file
when Inotify::OPEN
@f.send "StartExecuting", dir, file
when Inotify::CLOSE_WRITE, Inotify::CLOSE_NOWRITE
@f.send "StopExecuting", dir, file
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment