class Ziltoid::Watcher
Attributes
watchlist[RW]
Public Class Methods
log(message, level = Logger::INFO)
click to toggle source
# 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
logger()
click to toggle source
# File lib/ziltoid/watcher.rb, line 25 def self.logger @@logger end
new(options = {})
click to toggle source
# 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
notifiers()
click to toggle source
# File lib/ziltoid/watcher.rb, line 33 def self.notifiers @@notifiers ||= [] return @@notifiers end
read_state()
click to toggle source
# 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_file()
click to toggle source
# File lib/ziltoid/watcher.rb, line 52 def self.state_file @@state_file end
write_state(state = {})
click to toggle source
# 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
Public Instance Methods
add(watchable)
click to toggle source
# File lib/ziltoid/watcher.rb, line 17 def add(watchable) self.watchlist[watchable.name] = watchable end
logger()
click to toggle source
# File lib/ziltoid/watcher.rb, line 21 def logger Ziltoid::Watcher.logger end
notifiers()
click to toggle source
# File lib/ziltoid/watcher.rb, line 29 def notifiers Ziltoid::Watcher.notifiers end
restart!()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/ziltoid/watcher.rb, line 68 def run!(command = :watch) watchlist.values.each do |watchable| watchable.send("#{command}!".to_sym) end end
start!()
click to toggle source
# File lib/ziltoid/watcher.rb, line 79 def start! Watcher.log("Ziltoid is now on duty : all watchables starting !") run!(:start) end
state_file()
click to toggle source
# File lib/ziltoid/watcher.rb, line 48 def state_file Ziltoid::Watcher.state_file end
stop!()
click to toggle source
# File lib/ziltoid/watcher.rb, line 84 def stop! Watcher.log("Ziltoid is now on duty : all watchables stoping !") run!(:stop) end
watch!()
click to toggle source
# File lib/ziltoid/watcher.rb, line 74 def watch! Watcher.log("Ziltoid is now on duty : watching all watchables !") run!(:watch) end