Class: Eye::Checker
Defined Under Namespace
Classes: Cpu, Custom, CustomDefer, 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
#defaults, #should_bes, #validates, #variants
Class Method Summary
collapse
Instance Method Summary
collapse
inherited, param, validate
Constructor Details
#initialize(pid, options = {}, process = nil) ⇒ Checker
Returns a new instance of Checker.
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/eye/checker.rb', line 44
def initialize(pid, options = {}, process = nil)
@process = process
@pid = pid
@options = options
@type = options[:type]
debug "create checker, with #{options}"
@value = nil
@values = Eye::Utils::Tail.new(max_tries)
@check_count = 0
end
|
Instance Attribute Details
#check_count ⇒ Object
Returns the value of attribute check_count.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def check_count
@check_count
end
|
#options ⇒ Object
Returns the value of attribute options.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def options
@options
end
|
#pid ⇒ Object
Returns the value of attribute pid.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def pid
@pid
end
|
#process ⇒ Object
Returns the value of attribute process.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def process
@process
end
|
#type ⇒ Object
Returns the value of attribute type.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def type
@type
end
|
#value ⇒ Object
Returns the value of attribute value.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def value
@value
end
|
#values ⇒ Object
Returns the value of attribute values.
13
14
15
|
# File 'lib/eye/checker.rb', line 13
def values
@values
end
|
Class Method Details
.create(pid, options = {}, process = nil) ⇒ Object
36
37
38
|
# File 'lib/eye/checker.rb', line 36
def self.create(pid, options = {}, process = nil)
get_class(options[:type]).new(pid, options, process)
end
|
.get_class(type) ⇒ Object
30
31
32
33
34
|
# File 'lib/eye/checker.rb', line 30
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
20
21
22
23
24
25
26
27
28
|
# File 'lib/eye/checker.rb', line 20
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
158
159
160
161
162
163
|
# File 'lib/eye/checker.rb', line 158
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
40
41
42
|
# File 'lib/eye/checker.rb', line 40
def self.validate!(options)
get_class(options[:type]).validate(options)
end
|
Instance Method Details
#check ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/eye/checker.rb', line 78
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_name ⇒ Object
112
113
114
|
# File 'lib/eye/checker.rb', line 112
def check_name
@check_name ||= @type.to_s
end
|
#defer(&block) ⇒ Object
148
149
150
|
# File 'lib/eye/checker.rb', line 148
def defer(&block)
Celluloid::Future.new(&block).value
end
|
#get_value ⇒ Object
98
99
100
|
# File 'lib/eye/checker.rb', line 98
def get_value
raise 'Realize me'
end
|
#get_value_safe ⇒ Object
94
95
96
|
# File 'lib/eye/checker.rb', line 94
def get_value_safe
get_value
end
|
#good?(value) ⇒ Boolean
true if check ok false if check bad
108
109
110
|
# File 'lib/eye/checker.rb', line 108
def good?(value)
value
end
|
#human_value(value) ⇒ Object
102
103
104
|
# File 'lib/eye/checker.rb', line 102
def human_value(value)
value.to_s
end
|
#inspect ⇒ Object
57
58
59
|
# File 'lib/eye/checker.rb', line 57
def inspect
"<#{self.class} @process='#{@process.full_name}' @options=#{@options} @pid=#{@pid}>"
end
|
#last_human_values ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/eye/checker.rb', line 69
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_tag ⇒ Object
65
66
67
|
# File 'lib/eye/checker.rb', line 65
def logger_sub_tag
"check:#{check_name}"
end
|
#logger_tag ⇒ Object
61
62
63
|
# File 'lib/eye/checker.rb', line 61
def logger_tag
@process.logger.prefix
end
|
#max_tries ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/eye/checker.rb', line 116
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_tries ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/eye/checker.rb', line 128
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_value ⇒ Object
140
141
142
|
# File 'lib/eye/checker.rb', line 140
def previous_value
@values[-1][:value] if @values.present?
end
|
#run_in_process_context(p) ⇒ Object
144
145
146
|
# File 'lib/eye/checker.rb', line 144
def run_in_process_context(p)
process.instance_exec(&p) if process.alive?
end
|