require 'configuration' require 'check' require 'database_check' require 'website_check' class Checks def self.store_path @store_path ||= File.expand_path(File.join(File.dirname(__FILE__), "..", "run", "check.dat")) end class Worker @@config = "checks.yml" def self.config Configuration.load(@@config) end def initialize @checks = [] end def reload_config return unless !@config || @config.stale? @config ||= self.class.config @checks = @config.map do |check| Kernel.const_get(check[:class]).new(check) end end def check reload_config threads = @checks.map {|check| Thread.new { check.do_it } } threads.each(&:join) store end def passed? @checks.map(&:passed?).uniq == [ true ] end def results @checks end def store File.open(Checks.store_path, File::RDWR|File::CREAT, 0644) do |fp| fp.flock(File::LOCK_EX) fp.seek(0) fp.truncate(0) fp.write(Marshal.dump(@checks)) fp.flush fp.flock(File::LOCK_UN) end end end class Web def load File.open(Checks.store_path, File::RDONLY|File::CREAT, 0644) do |fp| fp.flock(File::LOCK_SH) fp.seek(0) @checks = Marshal.load(fp.read) fp.flock(File::LOCK_UN) end rescue StandardError => err @checks = [] end def check load end def passed? @checks.count > 0 && @checks.map(&:passed?).uniq == [ true ] && @checks.map(&:stale?).uniq == [ false ] end def results @checks end end end