Class: EventMachine::FileGlobWatch

Inherits:
Object
  • Object
show all
Defined in:
lib/em/globwatcher.rb

Overview

A file glob pattern watcher for EventMachine.

If you are unfamiliar with globs, see Wikipedia: en.wikipedia.org/wiki/Glob_(programming)

Any glob supported by Dir#glob will work with this class.

This class will allow you to get notified whenever a file is created or deleted that matches your glob.

If you are subclassing, here are the methods you should implement:

file_found(path)
file_deleted(path)

See alsoe

  • EventMachine::watch_glob

  • EventMachine::FileGlobWatch#file_found

  • EventMachine::FileGlobWatch#file_deleted

Direct Known Subclasses

FileGlobWatchTail

Defined Under Namespace

Classes: FileInfo, FileWatcher

Instance Method Summary collapse

Constructor Details

#initialize(glob, interval = 60) ⇒ FileGlobWatch

Watch a glob

  • glob - a string path or glob, such as “/var/log/*.log”

  • interval - number of seconds between scanning the glob for changes



36
37
38
39
40
41
42
43
44
# File 'lib/em/globwatcher.rb', line 36

def initialize(glob, interval=60)
  @glob = glob
  @files = Hash.new
  @watches = Hash.new
  @logger = Logger.new(STDOUT)
  @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
  @interval = interval
  start
end

Instance Method Details

#file_deleted(path) ⇒ Object

Raises:

  • (NotImplementedError)


91
92
93
94
95
# File 'lib/em/globwatcher.rb', line 91

def file_deleted(path)
  raise NotImplementedError.new("#{self.class.name}#file_deleted is not "\
    "implemented. Did you forget to implement this in your subclass or "\
    "module?")
end

#file_found(path) ⇒ Object

Raises:

  • (NotImplementedError)


78
79
80
81
82
# File 'lib/em/globwatcher.rb', line 78

def file_found(path)
  raise NotImplementedError.new("#{self.class.name}#file_found is not "\
    "implemented. Did you forget to implement this in your subclass or "\
    "module?")
end

#startObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/em/globwatcher.rb', line 56

def start
  # We periodically check here because it is easier than writing our own glob
  # parser (so we can smartly watch globs like /foo/*/bar*/*.log)
  #
  # Reasons to fix this -
  # This will likely perform badly on globs that result in a large number of
  # files.
  EM.next_tick do
    find_files
    @find_files_interval = EM.add_periodic_timer(@interval) do
      find_files
    end
  end # EM.next_tick
end

#stopObject



49
50
51
# File 'lib/em/globwatcher.rb', line 49

def stop
  @find_files_interval.cancel if @find_files_interval
end