Class: Ziltoid::Watcher
- Inherits:
-
Object
- Object
- Ziltoid::Watcher
- Defined in:
- lib/ziltoid/watcher.rb
Instance Attribute Summary collapse
-
#watchlist ⇒ Object
Returns the value of attribute watchlist.
Class Method Summary collapse
- .log(message, level = Logger::INFO) ⇒ Object
- .logger ⇒ Object
- .notifiers ⇒ Object
- .read_state ⇒ Object
- .state_file ⇒ Object
- .write_state(state = {}) ⇒ Object
Instance Method Summary collapse
- #add(watchable) ⇒ Object
-
#initialize(options = {}) ⇒ Watcher
constructor
A new instance of Watcher.
- #logger ⇒ Object
- #notifiers ⇒ Object
- #restart! ⇒ Object
- #run(command = :watch) ⇒ Object
- #run!(command = :watch) ⇒ Object
- #start! ⇒ Object
- #state_file ⇒ Object
- #stop! ⇒ Object
- #watch! ⇒ Object
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( = {}) self.watchlist ||= {} @@logger = [:logger] || Logger.new($stdout) @@logger.progname = [:progname] || "Ziltoid" @@logger.level = [:log_level] || Logger::INFO @@notifiers = [:notifiers] if [:notifiers] @@state_file = [:state_file] || File.join(File.dirname(__FILE__), "..", "state.ziltoid") end |
Instance Attribute Details
#watchlist ⇒ Object
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(, level = Logger::INFO) @@logger ||= Logger.new($stdout) @@logger.add(level, ) if level > Logger::INFO self.notifiers.each do |n| n.send() end end end |
.logger ⇒ Object
25 26 27 |
# File 'lib/ziltoid/watcher.rb', line 25 def self.logger @@logger end |
.notifiers ⇒ Object
33 34 35 36 |
# File 'lib/ziltoid/watcher.rb', line 33 def self.notifiers @@notifiers ||= [] return @@notifiers end |
.read_state ⇒ Object
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_file ⇒ Object
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 |
#logger ⇒ Object
21 22 23 |
# File 'lib/ziltoid/watcher.rb', line 21 def logger Ziltoid::Watcher.logger end |
#notifiers ⇒ Object
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_file ⇒ Object
48 49 50 |
# File 'lib/ziltoid/watcher.rb', line 48 def state_file Ziltoid::Watcher.state_file end |