Ruby:如何处理Mongo副本集Mongo :: ConnectionFailure异常?

时间:2014-09-19 13:48:19

标签: ruby mongodb exception-handling replication

如何在Ruby中处理Mogo副本集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

没有人可以处理救援区的异常消息

1 个答案:

答案 0 :(得分: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