2014-09-19 67 views
0

如何在Ruby中處理Mogo副本集Mongo :: ConnectionFailure異常?Ruby:如何處理Mongo副本集Mongo :: ConnectionFailure異常?

嘗試:

begin 
    $mongo = Mongo::MongoReplicaSetClient.new(['seed1:27017', 'seed2:27017', 'seed3:27017'], :read => :primary, :rs_name => 'rs0', :connect_timeout => 30, :op_timeout => 30) 
    mongo = $mongo.db('db_name') 
    rescue Exception => ex 
    puts ex.message 
    rescue Mongo::ConnectionFailure => ex 
    puts ex.message 
    rescue RuntimeError => ex 
    puts ex.message 
    rescue => ex 
    puts ex.message 
    end 

沒有人能處理救援塊的異常消息

回答

0

解決了:

def rescue_mongo_failures(max_retries=60) 
    retries = 0 

    begin 
    yield 
    rescue Mongo::ConnectionFailure => ex 
    retries += 1 
    # here you can get ex.message 
    raise ex if retries > max_retries 
    retry 
    end 
end 

def rescue_mongo_failures do 
    $mongo = Mongo::MongoReplicaSetClient.new(['seed1:27017', 'seed2:27017', 'seed3:27017'], :read => :primary, :rs_name => 'rs0', :connect_timeout => 30, :op_timeout => 30) 
    mongo = $mongo.db('db_name') 
    $mongo.db.stats 
end