lib/eye/checker.rb in reel-eye-0.3.2 vs lib/eye/checker.rb in reel-eye-0.4

- old
+ new

@@ -1,59 +1,75 @@ class Eye::Checker - include Eye::Logger::Helpers autoload :Memory, 'eye/checker/memory' autoload :Cpu, 'eye/checker/cpu' autoload :Http, 'eye/checker/http' autoload :FileCTime, 'eye/checker/file_ctime' autoload :FileSize, 'eye/checker/file_size' autoload :Socket, 'eye/checker/socket' - TYPES = {:memory => "Memory", :cpu => "Cpu", :http => "Http", + TYPES = {:memory => "Memory", :cpu => "Cpu", :http => "Http", :ctime => "FileCTime", :fsize => "FileSize", :socket => "Socket"} - attr_accessor :value, :values, :options, :pid, :type, :check_count + attr_accessor :value, :values, :options, :pid, :type, :check_count, :process + extend Eye::Dsl::Validation + param :every, [Fixnum, Float], false, 5 + param :times, [Fixnum, Array], nil, 1 + param :fire, Symbol, nil, nil, [:stop, :restart, :unmonitor, :nothing] + def self.name_and_class(type) type = type.to_sym return {:name => type, :type => type} if TYPES[type] if type =~ /\A(.*?)_?[0-9]+\z/ ctype = $1.to_sym return {:name => type, :type => ctype} if TYPES[ctype] - end + end end def self.get_class(type) klass = eval("Eye::Checker::#{TYPES[type]}") rescue nil raise "Unknown checker #{type}" unless klass klass end - def self.create(pid, options = {}, logger_prefix = nil) - get_class(options[:type]).new(pid, options, logger_prefix) + def self.create(pid, options = {}, process = nil) + get_class(options[:type]).new(pid, options, process) end def self.validate!(options) get_class(options[:type]).validate(options) end - def initialize(pid, options = {}, logger_prefix = nil) + def initialize(pid, options = {}, process = nil) + @process = process @pid = pid @options = options @type = options[:type] - @logger = Eye::Logger.new(logger_prefix, "check:#{check_name}") debug "create checker, with #{options}" - + @value = nil @values = Eye::Utils::Tail.new(max_tries) @check_count = 0 end + def inspect + "<#{self.class} @process='#{@process.full_name}' @options=#{@options} @pid=#{@pid}>" + end + + def logger_tag + @process.logger.prefix + end + + def logger_sub_tag + "check:#{check_name}" + end + def last_human_values - h_values = @values.map do |v| + h_values = @values.map do |v| sign = v[:good] ? '' : '*' sign + human_value(v[:value]).to_s end '[' + h_values * ', ' + ']' @@ -88,11 +104,11 @@ end # true if check ok # false if check bad def good?(value) - raise 'Realize me' + value end def check_name @check_name ||= @type.to_s end @@ -104,11 +120,11 @@ else times.to_i end else 1 - end + end end def min_tries @min_tries ||= if times if times.is_a?(Array) @@ -123,26 +139,40 @@ def previous_value @values[-1][:value] if @values.present? end - extend Eye::Dsl::Validation - param :every, [Fixnum, Float], false, 5 - param :times, [Fixnum, Array] - param :fire, Symbol, nil, nil, [:stop, :restart, :unmonitor, :nothing] + def run_in_process_context(p) + process.instance_exec(&p) if process.alive? + end + def defer(&block) + Celluloid::Future.new(&block).value + end + class Defer < Eye::Checker def get_value_safe Celluloid::Future.new{ get_value }.value end end - class Custom < Defer + def self.register(base) + name = base.to_s.gsub("Eye::Checker::", '') + type = name.underscore.to_sym + Eye::Checker::TYPES[type] = name + Eye::Checker.const_set(name, base) + end + + class Custom < Eye::Checker def self.inherited(base) super - name = base.to_s - type = name.underscore.to_sym - Eye::Checker::TYPES[type] = name - Eye::Checker.const_set(name, base) + register(base) + end + end + + class CustomDefer < Defer + def self.inherited(base) + super + register(base) end end end