Skip to content

Instantly share code, notes, and snippets.

@justindossey
Created March 17, 2012 17:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justindossey/2063267 to your computer and use it in GitHub Desktop.
Save justindossey/2063267 to your computer and use it in GitHub Desktop.
MongoMapper-- check that replicas are within a minute of primary
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
@jnunemaker
Copy link

Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment