lib/yogi_berra/catcher.rb in yogi_berra-0.0.14 vs lib/yogi_berra/catcher.rb in yogi_berra-0.0.15

- old
+ new

@@ -1,8 +1,9 @@ require 'mongo' require 'facets' require 'yaml' +require 'timeout' module YogiBerra class Catcher extend Facets cattr_accessor :settings, :mongo_client, :connection @@ -42,15 +43,19 @@ end def db_client(host, port, replica_set = nil) # :w => 0 set the default write concern to 0, this allows writes to be non-blocking # by not waiting for a response from mongodb + # :connect_timeout set to 5 will only wait 5 seconds failing to connect if replica_set - @@mongo_client = Mongo::MongoReplicaSetClient.new(replica_set, :w => 0) + @@mongo_client = Mongo::MongoReplicaSetClient.new(replica_set, :w => 0, :connect_timeout => 5) else - @@mongo_client = Mongo::MongoClient.new(host, port, :w => 0) + @@mongo_client = Mongo::MongoClient.new(host, port, :w => 0, :connect_timeout => 5) end - rescue + rescue Timeout::Error => error + YogiBerra::Logger.log("Couldn't connect to the mongo database timeout on host: #{host} port: #{port}.\n #{error}", :error) + nil + rescue => error YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.", :error) nil end def quick_connection(load_settings = false) \ No newline at end of file