Skip to content

Instantly share code, notes, and snippets.

@hassox
Created September 13, 2010 23:59
Show Gist options
  • Select an option

  • Save hassox/578285 to your computer and use it in GitHub Desktop.

Select an option

Save hassox/578285 to your computer and use it in GitHub Desktop.
module EmGlobWatcher
# Setup to watch a glob on the file system.
# If the glob is detected, the path of the matching file will be yielded to the block
# You may pass it an interval on how often to scan the directory.
# The block will be executed before the next interval is set
# @param glob - The file glob to watch for
# @param interval - The interval in seconds to poll the file system
def self.watch(glob, interval = 0.5, &blk)
raise "EventMacine must be running" unless EM.reactor_running?
watcher = nil
watcher = lambda do
Dir.glob(glob).each do |f|
blk.call f
end
EM.add_timer(interval, watcher)
end
EM.add_timer(interval, watcher)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment