Class: AppStatus::CheckCollection
- Inherits:
-
Object
- Object
- AppStatus::CheckCollection
- Defined in:
- app/models/app_status/check_collection.rb
Constant Summary collapse
- @@config_proc =
nil
Class Method Summary collapse
- .clear_checks! ⇒ Object
-
.configure(&block) ⇒ Object
Add checks here.
Instance Method Summary collapse
-
#add(options = {}) ⇒ Object
add the results of a check to the collection.
- #as_json ⇒ Object
-
#evaluate! ⇒ Object
run the checks added via configure results of the checks are available via as_json.
-
#initialize ⇒ CheckCollection
constructor
A new instance of CheckCollection.
- #valid_status?(status) ⇒ Boolean
Constructor Details
#initialize ⇒ CheckCollection
Returns a new instance of CheckCollection.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/app_status/check_collection.rb', line 34 def initialize @valid_status = { ok: 0, warning: 1, critical: 2, unknown: 3 }.freeze @checks = HashWithIndifferentAccess.new @eval_finished = nil @eval_time = 0 end |
Class Method Details
.clear_checks! ⇒ Object
30 31 32 |
# File 'app/models/app_status/check_collection.rb', line 30 def self.clear_checks! @@config_proc = nil end |
.configure(&block) ⇒ 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
26 27 28 |
# File 'app/models/app_status/check_collection.rb', line 26 def self.configure(&block) @@config_proc = block end |
Instance Method Details
#add(options = {}) ⇒ Object
add the results of a check to the collection. this should describe the health of some portion of your application
example:
value = some_service_check
c.add(:name => 'some_service', :status => :ok, :details => value)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/app_status/check_collection.rb', line 53 def add(={}) raise ArgumentError, ":name option is required." if ! [:name] raise ArgumentError, ":status option is required." if ! [:status] name = [:name].to_sym status = [:status].to_sym details = [:details].to_s # blow up if someone sends us options we don't understand. = .keys - [:name, :status, :details] if .size > 0 raise ArgumentError, "Unrecognized option(s) for '#{name}' check: #{.join(',')}" end raise ArgumentError, "'#{status}' is not a valid status for check '#{name}'." if ! valid_status?(status) raise ArgumentError, "Check name '#{name}' has already been added." if @checks.keys.include?(name) @checks[name] = {status: status, status_code: @valid_status[status], details: details} end |
#as_json ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/app_status/check_collection.rb', line 87 def as_json if @checks.size == 0 max_status = :unknown max_int = @valid_status[max_status] else max_int = @checks.inject([]){ |memo,val| memo << val[1][:status_code]; memo}.max max_status = @valid_status.invert[max_int] end HashWithIndifferentAccess.new({ status: max_status, status_code: max_int, run_time_ms: @eval_time.to_i, finished: @eval_finished.iso8601, checks: @checks }) end |
#evaluate! ⇒ Object
run the checks added via configure results of the checks are available via as_json
79 80 81 82 83 84 85 |
# File 'app/models/app_status/check_collection.rb', line 79 def evaluate! start = Time.now @checks = {} @@config_proc.call(self) if @@config_proc @eval_finished = Time.now.utc @eval_time = (Time.now - start) * 1000 end |
#valid_status?(status) ⇒ Boolean
73 74 75 |
# File 'app/models/app_status/check_collection.rb', line 73 def valid_status?(status) @valid_status.keys.include?(status) end |