diff --git a/lib/cubicle.rb b/lib/cubicle.rb index a0344a0..eed028f 100644 --- a/lib/cubicle.rb +++ b/lib/cubicle.rb @@ -1,5 +1,6 @@ require "rubygems" require "active_support" +require "active_support/hash_with_indifferent_access" unless defined?(HashWithIndifferentAccess) require "mongo" require "logger" require "mustache" diff --git a/lib/cubicle/mongo_environment.rb b/lib/cubicle/mongo_environment.rb index 7461f16..d5033ec 100644 --- a/lib/cubicle/mongo_environment.rb +++ b/lib/cubicle/mongo_environment.rb @@ -1,3 +1,9 @@ +#This class represents MongoDB. It is lifted line for line from MongoMapper +#http://github.com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper.rb +#Actually, if the MongoMapper gem is loaded, Cubicle will simply use it for +#providing the MongoEnvironment. However, if MongoMapper isn't loaded, +#this stuff is still required, so why reinvent the wheel? + module Cubicle class MongoEnvironment @@ -25,7 +31,7 @@ def self.database=(name) # @api public def self.database if @@database_name.blank? - raise 'You forgot to set the default database name: MongoMapper.database = "foobar"' + raise 'You forgot to set the default database name: Cubicle.mongo.database = "foobar"' end @@database ||= connection.db(@@database_name) @@ -95,8 +101,4 @@ def self.normalize_object_id(value) end end end -#This class represents MongoDB. It is lifted line for line from MongoMapper -#http://github.com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper.rb -#Actually, if the MongoMapper gem is loaded, Cubicle will simply use it for -#providing the MongoEnvironment. However, if MongoMapper isn't loaded, -#this stuff is still required, so why reinvent the wheel? + diff --git a/lib/cubicle/support.rb b/lib/cubicle/support.rb index 990296c..77b47fb 100644 --- a/lib/cubicle/support.rb +++ b/lib/cubicle/support.rb @@ -53,4 +53,22 @@ def by!(step_by) first.step_by = step_by if first.respond_to?(:step_by=) self end +end + +module Mongo + class Connection + def slave_lag + args = BSON::OrderedHash.new + args["serverStatus"] = 1 + args["repl"] = 1 + result = self["admin"].command(args) + local_time = result["localTime"] + sources = {} + result["repl"]["sources"].each do |source| + sync_time = source["syncedTo"]["time"] + sources[source["host"]] = local_time - sync_time + end + sources + end + end end \ No newline at end of file