Class: Eye::Checker

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

Direct Known Subclasses

Cpu, CustomCell, Defer, FileCTime, FileSize, Memory, Nop

Defined Under Namespace

Classes: Cpu, Custom, CustomCell, CustomDefer, Defer, FileCTime, FileSize, Http, Memory, Nop, Socket

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes included from Dsl::Validation

#defaults, #should_bes, #validates, #variants

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

inherited, param, validate

Constructor Details

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

Returns a new instance of Checker.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/eye/checker.rb', line 50

def initialize(pid, options = {}, process = nil)
  @process = process
  @pid = pid
  @options = options
  @type = options[:type]
  @full_name = @process.full_name if @process

  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.



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

def check_count
  @check_count
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#pidObject

Returns the value of attribute pid.



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

def pid
  @pid
end

#processObject

Returns the value of attribute process.



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

def process
  @process
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Class Method Details

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



38
39
40
41
42
43
44
# File 'lib/eye/checker.rb', line 38

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

rescue Exception, Timeout::Error => ex
  log_ex(ex)
  nil
end

.get_class(type) ⇒ Object



32
33
34
35
36
# File 'lib/eye/checker.rb', line 32

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

.name_and_class(type) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/eye/checker.rb', line 22

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

.register(base) ⇒ Object



168
169
170
171
172
173
# File 'lib/eye/checker.rb', line 168

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

.validate!(options) ⇒ Object



46
47
48
# File 'lib/eye/checker.rb', line 46

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

Instance Method Details

#checkObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/eye/checker.rb', line 85

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

rescue Exception, Timeout::Error => ex
  log_ex(ex)
end

#check_nameObject



122
123
124
# File 'lib/eye/checker.rb', line 122

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

#defer(&block) ⇒ Object



158
159
160
# File 'lib/eye/checker.rb', line 158

def defer(&block)
  Celluloid::Future.new(&block).value
end

#get_valueObject



108
109
110
# File 'lib/eye/checker.rb', line 108

def get_value
  raise 'Realize me'
end

#get_value_safeObject



104
105
106
# File 'lib/eye/checker.rb', line 104

def get_value_safe
  get_value
end

#good?(value) ⇒ Boolean

true if check ok false if check bad

Returns:

  • (Boolean)


118
119
120
# File 'lib/eye/checker.rb', line 118

def good?(value)
  value
end

#human_value(value) ⇒ Object



112
113
114
# File 'lib/eye/checker.rb', line 112

def human_value(value)
  value.to_s
end

#inspectObject



64
65
66
# File 'lib/eye/checker.rb', line 64

def inspect
  "<#{self.class} @process='#{@full_name}' @options=#{@options} @pid=#{@pid}>"
end

#last_human_valuesObject



76
77
78
79
80
81
82
83
# File 'lib/eye/checker.rb', line 76

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

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

#logger_sub_tagObject



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

def logger_sub_tag
  "check:#{check_name}"
end

#logger_tagObject



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

def logger_tag
  @process.logger.prefix
end

#max_triesObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/eye/checker.rb', line 126

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



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/eye/checker.rb', line 138

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



150
151
152
# File 'lib/eye/checker.rb', line 150

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

#run_in_process_context(p) ⇒ Object



154
155
156
# File 'lib/eye/checker.rb', line 154

def run_in_process_context(p)
  process.instance_exec(&p) if process.alive?
end