Class: Ziltoid::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ziltoid/watcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Watcher

Returns a new instance of Watcher.



8
9
10
11
12
13
14
15
# File 'lib/ziltoid/watcher.rb', line 8

def initialize(options = {})
  self.watchlist ||= {}
  @@logger = options[:logger] || Logger.new($stdout)
  @@logger.progname = options[:progname] || "Ziltoid"
  @@logger.level = options[:log_level] || Logger::INFO
  @@notifiers = options[:notifiers] if options[:notifiers]
  @@state_file = options[:state_file] || File.join(File.dirname(__FILE__), "..", "state.ziltoid")
end

Instance Attribute Details

#watchlistObject

Returns the value of attribute watchlist.



6
7
8
# File 'lib/ziltoid/watcher.rb', line 6

def watchlist
  @watchlist
end

Class Method Details

.log(message, level = Logger::INFO) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/ziltoid/watcher.rb', line 38

def self.log(message, level = Logger::INFO)
  @@logger ||= Logger.new($stdout)
  @@logger.add(level, message)
  if level > Logger::INFO
    self.notifiers.each do |n|
      n.send(message)
    end
  end
end

.loggerObject



25
26
27
# File 'lib/ziltoid/watcher.rb', line 25

def self.logger
  @@logger
end

.notifiersObject



33
34
35
36
# File 'lib/ziltoid/watcher.rb', line 33

def self.notifiers
  @@notifiers ||= []
  return @@notifiers
end

.read_stateObject



56
57
58
59
60
# File 'lib/ziltoid/watcher.rb', line 56

def self.read_state
  json = File.read(state_file) if File.exist?(state_file)
  json = "{}" if json.nil? || json.empty?
  JSON.load(json)
end

.state_fileObject



52
53
54
# File 'lib/ziltoid/watcher.rb', line 52

def self.state_file
  @@state_file
end

.write_state(state = {}) ⇒ Object



62
63
64
65
66
# File 'lib/ziltoid/watcher.rb', line 62

def self.write_state(state = {})
  File.open(state_file, "w+") do |file|
    file.puts JSON.generate(state)
  end
end

Instance Method Details

#add(watchable) ⇒ Object



17
18
19
# File 'lib/ziltoid/watcher.rb', line 17

def add(watchable)
  self.watchlist[watchable.name] = watchable
end

#loggerObject



21
22
23
# File 'lib/ziltoid/watcher.rb', line 21

def logger
  Ziltoid::Watcher.logger
end

#notifiersObject



29
30
31
# File 'lib/ziltoid/watcher.rb', line 29

def notifiers
  Ziltoid::Watcher.notifiers
end

#restart!Object



89
90
91
92
# File 'lib/ziltoid/watcher.rb', line 89

def restart!
  Watcher.log("Ziltoid is now on duty : all watchables restarting !")
  run!(:restart)
end

#run(command = :watch) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ziltoid/watcher.rb', line 94

def run(command = :watch)
  case command
  when :watch
    watch!
  when :start
    start!
  when :stop
    stop!
  when :restart
    restart!
  end
end

#run!(command = :watch) ⇒ Object



68
69
70
71
72
# File 'lib/ziltoid/watcher.rb', line 68

def run!(command = :watch)
  watchlist.values.each do |watchable|
    watchable.send("#{command}!".to_sym)
  end
end

#start!Object



79
80
81
82
# File 'lib/ziltoid/watcher.rb', line 79

def start!
  Watcher.log("Ziltoid is now on duty : all watchables starting !")
  run!(:start)
end

#state_fileObject



48
49
50
# File 'lib/ziltoid/watcher.rb', line 48

def state_file
  Ziltoid::Watcher.state_file
end

#stop!Object



84
85
86
87
# File 'lib/ziltoid/watcher.rb', line 84

def stop!
  Watcher.log("Ziltoid is now on duty : all watchables stoping !")
  run!(:stop)
end

#watch!Object



74
75
76
77
# File 'lib/ziltoid/watcher.rb', line 74

def watch!
  Watcher.log("Ziltoid is now on duty : watching all watchables !")
  run!(:watch)
end