Class: Vm::Watcher::VmWatcher

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/vm-watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ VmWatcher

Returns a new instance of VmWatcher.



29
30
31
32
33
# File 'lib/vm-watcher.rb', line 29

def initialize(options = {})
  @last_mtime = @last_ctime = Time.now
  @watch = options[:watch]
  @sleep_interval = options[:interval]
end

Instance Method Details

#notify(modified) ⇒ Object



55
56
57
58
59
# File 'lib/vm-watcher.rb', line 55

def notify(modified)
  @last_mtime, @last_ctime = modified.mtime, modified.ctime
  changed
  notify_observers
end

#watchObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vm-watcher.rb', line 35

def watch
  files = Dir.glob(@watch).map {|f| Pathname(f).expand_path }
  if files.empty?
    abort "No files to watch under #{@watch}"
  end

  loop do
    last_modified = files.max {|a, b| a.mtime <=> b.mtime}

    if last_modified.mtime > @last_mtime
      notify(last_modified)
    else
      last_changed = files.max {|a, b| a.ctime <=> b.ctime}
      notify(last_changed) if last_changed.ctime > @last_ctime
    end

    sleep @sleep_interval
  end
end