Module: FailFast::CheckMongoDB

Defined in:
lib/fail_fast/extensions/check_mongo_db.rb

Instance Method Summary collapse

Instance Method Details

#has_mongoDB(host, *params) ⇒ Object

Ensure the mongoDB server can be reached, and the db could be opened :

Usage :

has_mongoDB 'localhost'
has_mongoDB 'localhost', :message => 'custom message'
has_mongoDB 'localhost', 'my_db', :port => 1234, :timeout => 2
has_mongoDB 'localhost', :port => 1234, :timeout => 2, :message => 'custom message'


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fail_fast/extensions/check_mongo_db.rb', line 12

def has_mongoDB(host, *params)
  options = params.last.is_a?(Hash) ? params.pop : {}
  db      = params.first

  begin
    port = options.delete(:port)
    @conn = Mongo::Connection.new(host, port, options)
  rescue Mongo::ConnectionFailure
    add_error ErrorDetails.new(nil, :mongoDB_server_not_found, host, options[:message])
    return
  end

  if db && !@conn.database_names.include?(db)
    add_error ErrorDetails.new(nil, :mongoDB_db_not_found, db, options[:message])
  end
end

#has_mongoDB_for(key, *params) ⇒ Object

Ensure the mongoDB server can be reached, and the db could be opened :

Usage :

has_mongoDB_for 'test/mongoDB'
has_mongoDB_for 'test/unknown_mongoDB_db', :check_database => false


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fail_fast/extensions/check_mongo_db.rb', line 35

def has_mongoDB_for(key, *params)
  p = key_value_regexp_options(key, params)
  key, options = p.key, p.options
  return unless has_value_for key              , :message => options[:message]
  return unless has_value_for "#{key}/host"    , :message => options[:message]
  return unless has_value_for "#{key}/database", :message => options[:message]


  value = value_for_deep_key(key)
  host, port, db = value['host'], value['port'], value['database']

  begin
    @conn = Mongo::Connection.new(host, port)
  rescue Mongo::ConnectionFailure
    add_error ErrorDetails.new(key, :mongoDB_server_not_found, host, options[:message])
    return
  end

  must_check_db = !(false == options[:check_database])
  if must_check_db && !@conn.database_names.include?(db)
    add_error ErrorDetails.new(key, :mongoDB_db_not_found, db, options[:message])
  end
end