Class: AppStatus::CheckCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/app_status/check_collection.rb

Constant Summary collapse

@@check_descriptions =
HashWithIndifferentAccess.new
@@checks =
HashWithIndifferentAccess.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCheckCollection

Returns a new instance of CheckCollection.



94
95
96
# File 'app/models/app_status/check_collection.rb', line 94

def initialize
  reset
end

Instance Attribute Details

#finishedObject (readonly)

Returns the value of attribute finished.



91
92
93
# File 'app/models/app_status/check_collection.rb', line 91

def finished
  @finished
end

#msObject (readonly)

Returns the value of attribute ms.



91
92
93
# File 'app/models/app_status/check_collection.rb', line 91

def ms
  @ms
end

#statusObject (readonly)

Returns the value of attribute status.



91
92
93
# File 'app/models/app_status/check_collection.rb', line 91

def status
  @status
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



91
92
93
# File 'app/models/app_status/check_collection.rb', line 91

def status_code
  @status_code
end

Class Method Details

.add_check(name, &block) ⇒ Object

add the results of a check to the collection. this should describe the health of some portion of your application The check block you supply should return a status value, or a

:status, ‘details’

array.

example:

AppStatus::CheckCollection.add_check('some_service') do
  [:ok, 'these are optional details']
end

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/app_status/check_collection.rb', line 56

def self.add_check(name, &block)
  raise ArgumentError, ":name option is required." if ! name

  name = name.to_sym
  raise ArgumentError, "Check name '#{name}' has already been added." if @@checks.keys.include?(name.to_s)
  raise ArgumentError, "No check defined for '#{name}'." if ! block_given?

  item = CheckItem.new(name)
  item.proc = block
  @@checks[name] = item
end

.add_description(name, markdown) ⇒ Object

add a long-form description of a check service must have already been added via add_check.

example:

AppStatus::CheckCollection.configure do |c|
  c.add_check('some_service') do
    [:ok, 'these are optional details']
  end
  c.add_decription 'some_service', <<-EOF

some_service is pretty easy to understand. it always works, no matter what. but if it were **harder to comprehend** you could add markdown here to explain what it is and what to do if it starts failing.

EOF

Raises:

  • (ArgumentError)


84
85
86
87
# File 'app/models/app_status/check_collection.rb', line 84

def self.add_description(name, markdown)
  raise ArgumentError, "Check '#{name}' is not defined." if ! @@checks[name]
  @@checks[name].description = markdown
end

.clear_checks!Object



33
34
35
36
# File 'app/models/app_status/check_collection.rb', line 33

def self.clear_checks!
  @@check_descriptions = HashWithIndifferentAccess.new
  @@checks = HashWithIndifferentAccess.new
end

.configure {|_self| ... } ⇒ Object

Add checks here.

These checks are re-run whenever evaluate! is called on an instance. They aren’t run at configure time.

The block recieves an instance of AppStatus::CheckCollection as an argument.

example (put this in config/initalizers/app_status.rb):

AppStatus::CheckCollection.configure do |c|

  value = some_service_check
  status = value > 100 ? :ok : :critical
  c.add(:name => 'some_service', :status => status, :details => value)

end

Yields:

  • (_self)

Yield Parameters:



29
30
31
# File 'app/models/app_status/check_collection.rb', line 29

def self.configure(&block)
  yield self
end

.valid_status_mapObject



38
39
40
41
42
43
44
45
# File 'app/models/app_status/check_collection.rb', line 38

def self.valid_status_map
  {
          ok: 0,
     warning: 1,
    critical: 2,
     unknown: 3
  }
end

Instance Method Details

#as_hashObject



136
137
138
139
140
141
142
143
144
# File 'app/models/app_status/check_collection.rb', line 136

def as_hash
  HashWithIndifferentAccess.new({
    status: @status,
    status_code: @status_code,
    ms: @ms.to_i,
    finished: @finished.iso8601,
    checks: @@checks.inject({}) {|memo,(name,check)| memo[name] = check.as_hash; memo}
  })
end

#as_jsonObject



146
147
148
# File 'app/models/app_status/check_collection.rb', line 146

def as_json
  as_hash
end

#eachObject



106
107
108
# File 'app/models/app_status/check_collection.rb', line 106

def each
  @@checks.each {|name,check| yield check }
end

#evaluate!Object

run the checks added via configure results of the checks are available via as_json



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/app_status/check_collection.rb', line 116

def evaluate!
  eval_start = Time.now

  reset

  @@checks.each do |name,check|
    @status_code = [check.evaluate!, @status_code].max
  end

  @finished = Time.now.utc
  @ms = (Time.now - eval_start) * 1000

  if @@checks.size == 0
    @status = :unknown
    @status_code = self.class.valid_status_map[@status]
  else
    @status = self.class.valid_status_map.invert[@status_code]
  end
end

#resetObject



98
99
100
101
102
103
104
# File 'app/models/app_status/check_collection.rb', line 98

def reset
  @finished = nil
  @ms = 0
  @status = :ok
  @status_code = self.class.valid_status_map[@status]
  @@checks.each {|key,check| check.reset }
end

#sizeObject



110
111
112
# File 'app/models/app_status/check_collection.rb', line 110

def size
  @@checks.size
end