Class: Eye::Checker

Inherits:
Object
  • Object
show all
Extended by:
Dsl::Validation
Includes:
Logger::Helpers
Defined in:
lib/eye/checker.rb

Direct Known Subclasses

Cpu, Defer, FileCTime, FileSize, Memory

Defined Under Namespace

Classes: Cpu, Custom, Defer, FileCTime, FileSize, Http, Memory, Socket

Constant Summary collapse

TYPES =
{:memory => "Memory", :cpu => "Cpu", :http => "Http", 
:ctime => "FileCTime", :fsize => "FileSize", :socket => "Socket"}

Instance Attribute Summary collapse

Attributes included from Dsl::Validation

#defaults, #should_bes, #validates, #variants

Attributes included from Logger::Helpers

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

inherited, param, validate

Constructor Details

#initialize(pid, options = {}, logger_prefix = nil) ⇒ Checker

Returns a new instance of Checker.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eye/checker.rb', line 30

def initialize(pid, options = {}, logger_prefix = nil)
  @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

Instance Attribute Details

#check_countObject

Returns the value of attribute check_count.



14
15
16
# File 'lib/eye/checker.rb', line 14

def check_count
  @check_count
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/eye/checker.rb', line 14

def options
  @options
end

#pidObject

Returns the value of attribute pid.



14
15
16
# File 'lib/eye/checker.rb', line 14

def pid
  @pid
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/eye/checker.rb', line 14

def type
  @type
end

#valueObject

Returns the value of attribute value.



14
15
16
# File 'lib/eye/checker.rb', line 14

def value
  @value
end

#valuesObject

Returns the value of attribute values.



14
15
16
# File 'lib/eye/checker.rb', line 14

def values
  @values
end

Class Method Details

.create(pid, options = {}, logger_prefix = nil) ⇒ Object



22
23
24
# File 'lib/eye/checker.rb', line 22

def self.create(pid, options = {}, logger_prefix = nil)
  get_class(options[:type]).new(pid, options, logger_prefix)
end

.get_class(type) ⇒ Object



16
17
18
19
20
# File 'lib/eye/checker.rb', line 16

def self.get_class(type)
  klass = eval("Eye::Checker::#{TYPES[type]}") rescue nil
  raise "Unknown checker #{type}" unless klass
  klass
end

.validate!(options) ⇒ Object



26
27
28
# File 'lib/eye/checker.rb', line 26

def self.validate!(options)
  get_class(options[:type]).validate(options)
end

Instance Method Details

#checkObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eye/checker.rb', line 52

def check
  @value = get_value_safe
  @values << {:value => @value, :good => good?(value)}

  result = true
  @check_count += 1

  if @values.size == max_tries
    bad_count = @values.count{|v| !v[:good] }
    result = false if bad_count >= min_tries
  end

  info "#{last_human_values} => #{result ? 'OK' : 'Fail'}"
  result
end

#check_nameObject



86
87
88
# File 'lib/eye/checker.rb', line 86

def check_name
  @check_name ||= @type.to_s
end

#get_valueObject



72
73
74
# File 'lib/eye/checker.rb', line 72

def get_value
  raise 'Realize me'
end

#get_value_safeObject



68
69
70
# File 'lib/eye/checker.rb', line 68

def get_value_safe
  get_value
end

#good?(value) ⇒ Boolean

true if check ok false if check bad

Returns:

  • (Boolean)


82
83
84
# File 'lib/eye/checker.rb', line 82

def good?(value)
  raise 'Realize me'
end

#human_value(value) ⇒ Object



76
77
78
# File 'lib/eye/checker.rb', line 76

def human_value(value)
  value.to_s
end

#last_human_valuesObject



43
44
45
46
47
48
49
50
# File 'lib/eye/checker.rb', line 43

def last_human_values
  h_values = @values.map do |v| 
    sign = v[:good] ? '' : '*'
    sign + human_value(v[:value]).to_s
  end

  '[' + h_values * ', ' + ']'
end

#max_triesObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/eye/checker.rb', line 90

def max_tries
  @max_tries ||= if times
    if times.is_a?(Array)
      times[-1].to_i
    else
      times.to_i
    end
  else
    1
  end    
end

#min_triesObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/eye/checker.rb', line 102

def min_tries
  @min_tries ||= if times
    if times.is_a?(Array)
      times[0].to_i
    else
      max_tries
    end
  else
    max_tries
  end
end

#previous_valueObject



114
115
116
# File 'lib/eye/checker.rb', line 114

def previous_value
  @values[-1][:value] if @values.present?
end