Class: Eye::Checker

Inherits:
Object show all
Includes:
Dsl::Validation
Defined in:
lib/eye/checker.rb

Direct Known Subclasses

CustomCell, Defer, FileCTime, FileTouched, Measure, Nop

Defined Under Namespace

Classes: Cpu, Cputime, Custom, CustomCell, CustomDefer, Defer, FileCTime, FileSize, FileTouched, Http, Measure, Memory, Nop, Runtime, Socket

Constant Summary collapse

TYPES =
{:memory => 'Memory', :cpu => 'Cpu', :http => 'Http',
:ctime => 'FileCTime', :fsize => 'FileSize', :file_touched => 'FileTouched',
:socket => 'Socket', :nop => 'Nop', :runtime => 'Runtime', :cputime => 'Cputime' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

included

Constructor Details

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

Returns a new instance of Checker.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eye/checker.rb', line 58

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

  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.



19
20
21
# File 'lib/eye/checker.rb', line 19

def check_count
  @check_count
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/eye/checker.rb', line 19

def options
  @options
end

#pidObject

Returns the value of attribute pid.



19
20
21
# File 'lib/eye/checker.rb', line 19

def pid
  @pid
end

#processObject

Returns the value of attribute process.



19
20
21
# File 'lib/eye/checker.rb', line 19

def process
  @process
end

#typeObject

Returns the value of attribute type.



19
20
21
# File 'lib/eye/checker.rb', line 19

def type
  @type
end

#valueObject

Returns the value of attribute value.



19
20
21
# File 'lib/eye/checker.rb', line 19

def value
  @value
end

#valuesObject

Returns the value of attribute values.



19
20
21
# File 'lib/eye/checker.rb', line 19

def values
  @values
end

Class Method Details

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



46
47
48
49
50
51
52
# File 'lib/eye/checker.rb', line 46

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

.depends_onObject



200
201
# File 'lib/eye/checker.rb', line 200

def self.depends_on
end

.get_class(type) ⇒ Object



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

def self.get_class(type)
  klass = eval("Eye::Checker::#{TYPES[type]}") rescue nil
  raise "Unknown checker #{type}" unless klass
  if deps = klass.depends_on
    Array(deps).each { |d| require d }
  end
  klass
end

.name_and_class(type) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/eye/checker.rb', line 27

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



193
194
195
196
197
198
# File 'lib/eye/checker.rb', line 193

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



54
55
56
# File 'lib/eye/checker.rb', line 54

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

Instance Method Details

#checkObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/eye/checker.rb', line 94

def check
  if initial_grace && (Time.now - @initialized_at < initial_grace)
    debug 'skipped initial grace'
    return true
  else
    @options[:initial_grace] = nil
  end

  @value = get_value_safe
  @good_value = good?(value)
  @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

  if skip_initial_fails
    if @good_value
      @options[:skip_initial_fails] = nil
    else
      result = true
    end
  end

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

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

#check_nameObject



147
148
149
# File 'lib/eye/checker.rb', line 147

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

#defer(&block) ⇒ Object



183
184
185
# File 'lib/eye/checker.rb', line 183

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

#get_valueObject

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/eye/checker.rb', line 133

def get_value
  raise NotImplementedError
end

#get_value_safeObject



129
130
131
# File 'lib/eye/checker.rb', line 129

def get_value_safe
  get_value
end

#good?(value) ⇒ Boolean

true if check ok false if check bad

Returns:

  • (Boolean)


143
144
145
# File 'lib/eye/checker.rb', line 143

def good?(value)
  value
end

#human_value(value) ⇒ Object



137
138
139
# File 'lib/eye/checker.rb', line 137

def human_value(value)
  value.to_s
end

#inspectObject



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

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

#last_human_valuesObject



85
86
87
88
89
90
91
92
# File 'lib/eye/checker.rb', line 85

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



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

def logger_sub_tag
  "check:#{check_name}"
end

#logger_tagObject



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

def logger_tag
  @process.logger.prefix
end

#max_triesObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/eye/checker.rb', line 151

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



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/eye/checker.rb', line 163

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



175
176
177
# File 'lib/eye/checker.rb', line 175

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

#run_in_process_context(p) ⇒ Object



179
180
181
# File 'lib/eye/checker.rb', line 179

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