Skip to content

Commit

Permalink
Added slave_lag method to Mongo::Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan committed Jul 2, 2010
1 parent 0fd941c commit 20afca0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions 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"
Expand Down
14 changes: 8 additions & 6 deletions 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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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?

18 changes: 18 additions & 0 deletions lib/cubicle/support.rb
Expand Up @@ -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

0 comments on commit 20afca0

Please sign in to comment.