Created
March 17, 2012 17:38
-
-
Save justindossey/2063267 to your computer and use it in GitHub Desktop.
MongoMapper-- check that replicas are within a minute of primary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def all_replicas_in_sync?(verbose=false) | |
in_sync = true | |
# connect to the primary | |
mongo = Mongo::Connection.new(*MongoMapper.connection.primary) | |
db = mongo.db('local') | |
last_op=db.collection('oplog.rs').find.sort([['$natural',-1]]).limit(1).to_a[0] | |
db.collection('slaves').find().to_a.each do |slave| | |
opdiff = last_op['ts'].increment - slave['syncedTo'].increment | |
diff = (last_op['ts'].seconds - slave['syncedTo'].seconds)/1000.0 | |
in_sync = false if diff > 60 | |
if verbose | |
puts "#{slave['host']} is #{diff} seconds and #{opdiff} ops behind master (#{in_sync ? 'good' : 'bad'})" | |
end | |
end | |
mongo.close | |
return in_sync | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing.