require 'mongo'
def rescue_connection_failure(num_retries = 30)
retries = 0
begin
puts "retries: #{retries}"
yield
rescue Mongo::ConnectionFailure => e
retries += 1
raise if retries > num_retries
puts "Failed to connect to Replica Set. Try to retry: retry number = #{retries}"
sleep 1
retry
end
end
collection = Mongo::ReplSetConnection.new(['localhost', 27017], ['localhost', 27018], ['localhost', 27019]).db('d').collection('c')
i = 0
loop {
rescue_connection_failure {
collection.insert({'num' => i})
}
i += 1
sleep 2
}
Primary down at writing operation.
PRIMARY> db.c.find()
{ "_id" : ObjectId("4f19f72285c102aeeb000001"), "num" : 0 }
{ "_id" : ObjectId("4f19f72485c102aeeb000002"), "num" : 1 }
{ "_id" : ObjectId("4f19f72685c102aeeb000003"), "num" : 2 }
{ "_id" : ObjectId("4f19f72885c102aeeb000004"), "num" : 3 }
{ "_id" : ObjectId("4f19f72d85c102aeeb000007"), "num" : 5 }
{ "_id" : ObjectId("4f19f72f85c102aeeb000008"), "num" : 6 }
{ "_id" : ObjectId("4f19f73185c102aeeb000009"), "num" : 7 }
4 number object lossed...
mongo-ruby-driver: 1.5.2
mongod: osx-x86_64-2.0.2