Class: OkComputer::MongoidCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/mongoid_check.rb

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name

Instance Method Summary collapse

Methods inherited from Check

#clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text

Constructor Details

#initialize(session = :default) ⇒ MongoidCheck

Public: Initialize a check for a Mongoid replica set

session - The name of the Mongoid session to use. Defaults to the default session.



9
10
11
12
13
# File 'lib/ok_computer/built_in_checks/mongoid_check.rb', line 9

def initialize(session = :default)
  if Mongoid.respond_to?(:sessions)
    self.session = Mongoid::Sessions.with_name(session)
  end
end

Instance Attribute Details

#session ⇒ Object

Returns the value of attribute session.



3
4
5
# File 'lib/ok_computer/built_in_checks/mongoid_check.rb', line 3

def session
  @session
end

Instance Method Details

#check ⇒ Object

Public: Return the status of the mongodb



16
17
18
19
20
21
# File 'lib/ok_computer/built_in_checks/mongoid_check.rb', line 16

def check
  mark_message "Connected to mongodb #{mongodb_name}"
rescue ConnectionFailed => e
  mark_failure
  mark_message "Error: '#{e}'"
end

#mongodb_name ⇒ Object

Public: The name of the app's mongodb

Returns a string with the mongdb name



39
40
41
# File 'lib/ok_computer/built_in_checks/mongoid_check.rb', line 39

def mongodb_name
  mongodb_stats["db"]
end

#mongodb_stats ⇒ Object

Public: The stats for the app's mongodb

Returns a hash with the status of the db



26
27
28
29
30
31
32
33
34
# File 'lib/ok_computer/built_in_checks/mongoid_check.rb', line 26

def mongodb_stats
  if session
    session.command(dbStats: 1) # Mongoid 3+
  else
    Mongoid.database.stats # Mongoid 2
  end
rescue => e
  raise ConnectionFailed, e
end