Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_reconnect_in_case_connection_closed_by_mongo(self):
cx = self.asyncio_client(maxPoolSize=1, retryReads=False)
yield from cx.admin.command('ping')
# close motor_socket, we imitate that connection to mongo server
# lost, as result we should have AutoReconnect instead of
# IncompleteReadError
pool = get_primary_pool(cx)
socket = pool.sockets.pop()
socket.sock.close()
pool.sockets.appendleft(socket)
with self.assertRaises(pymongo.errors.AutoReconnect):
yield from cx.motor_test.test_collection.find_one()
text_type(db_user),
text_type(db_password),
{},
'admin')
c.delegate._cache_credentials('test', credentials, connect=False)
# Cause a network error on the actual socket.
pool = get_primary_pool(c)
socket_info = one(pool.sockets)
socket_info.sock.close()
# In __check_auth, the client authenticates its socket with the
# new credential, but gets a socket.error. Should be reraised as
# AutoReconnect.
with self.assertRaises(pymongo.errors.AutoReconnect):
yield c.test.collection.find_one()
# No semaphore leak, the pool is allowed to make a new socket.
yield c.test.collection.find_one()
Job.ATTR_TS: {
"$lte": datetime.datetime.now() + self.read_ahead_interval
},
Job.ATTR_STATUS: Job.S_WAIT
})).limit(limit).sort(Job.ATTR_TS)
try:
for job in qs:
try:
jcls = get_handler(job[Job.ATTR_CLASS])
yield jcls(self, job)
except ImportError as e:
self.logger.error("Invalid job class %s",
job[Job.ATTR_CLASS])
self.logger.error("Error: %s", e)
self.remove_job_by_id(job[Job.ATTR_ID])
except pymongo.errors.CursorNotFound:
self.logger.info("Server cursor timed out. Waiting for next cycle")
except pymongo.errors.OperationFailure as e:
self.logger.error("Operation failure: %s", e)
self.logger.error("Trying to recover")
except pymongo.errors.AutoReconnect:
self.logger.error("Auto-reconnect detected. Waiting for next cycle")
client = pymongo.MongoClient( serverSelectionTimeoutMS=timeout )
database = client[ database_name ]
collection = database[ collection_name ]
#
# Make sure we have a collection
print( "Connecting to database server...")
try:
_ = database.command("ismaster")
print( "connected" )
print( "collection '%s.%s' has %i documents" % ( database_name,
collection_name,
collection.count()))
return collection
except pymongo.errors.ServerSelectionTimeoutError, e:
print( "connection failed: %s" % e )
print( "No mongod found on port 27017")
sys.exit( 2 )
super(MongoRepo, self).__init__()
self.dbhost = host
if user and passw:
uri = "mongodb://{}:{}@{}:{}/test".format(user, passw, host, port)
client = pymongo.MongoClient(uri)
else:
client = pymongo.MongoClient(host, port)
if client is None:
s = "cannot connect to mongo at host: [{}], port [{}], username: [{}], password: [{}]".format(host,
port,
user,
passw)
raise Exception(s)
try:
dbnames = client.database_names()
except pymongo.errors.ServerSelectionTimeoutError:
raise Exception("cannot get database from mongo at host: {}, port {}, connection timed out".format(host,
port))
if "reporting_db" not in dbnames:
raise Exception("no database for results found")
self.db = client.reporting_db
def update_item(self, req: falcon.Request, object_id: str) -> Dict:
try:
result = self._contacts.find_one_and_update(
{'_id': self._make_objectid(object_id)},
{'$set': req.context['body_json']},
return_document=ReturnDocument.AFTER)
if result is None:
self._handle_not_found(object_id)
result['_id'] = str(result['_id'])
return result
except (pymongoErrors.AutoReconnect,
pymongoErrors.ConnectionFailure,
pymongoErrors.NetworkTimeout):
self._handle_service_unavailable()
def _decorator(self, *args, **kwargs):
try:
rval = method(self, *args, **kwargs)
except pymongo.errors.DuplicateKeyError as e:
raise DuplicateKeyError(str(e)) from e
except pymongo.errors.BulkWriteError as e:
for error in e.details['writeErrors']:
if any(m in error["errmsg"] for m in DUPLICATE_KEY_MESSAGES):
raise DuplicateKeyError(error["errmsg"]) from e
raise
except pymongo.errors.ConnectionFailure as e:
raise DatabaseError("Connection Failure: database not found on "
"specified uri") from e
except pymongo.errors.OperationFailure as e:
if any(m in str(e) for m in AUTH_FAILED_MESSAGES):
raise DatabaseError("Authentication Failure: bad credentials") from e
elif any(m in str(e) for m in INDEX_OP_ERROR_MESSAGES):
raise DatabaseError(str(e)) from e
raise
return rval
def __execute(self, procedure, *args, **kwargs):
"""Try 10 times to execute the procedure."""
for tryCount in range(10):
try:
return procedure(*args, **kwargs)
except pymongo.errors.AutoReconnect as error:
self.__lastError = error
time.sleep(0.1)
except pymongo.errors.OperationFailure as error:
self.__lastError = error
break
def connect_db(dataset, use_shared_data=False):
"""
Connects to a specific database based on the values stored in settings for
a given dataset, and whether to use shared data or not.
"""
database = 'shared' if use_shared_data else 'db'
try:
client = pymongo.MongoClient(connect=False, host=settings.mongo_host, port=settings.mongo_port)
auth_db = client['exac-user']
auth_db.authenticate(settings.mongo_user, settings.mongo_password)
db = client[settings.mongo_databases[dataset][database]]
return db
except pymongo.errors.ServerSelectionTimeoutError:
logging.error("Failed to connect to database '{}' for dataset '{}'".format(database, dataset))
return None
'statuses_count': tweet.author.statuses_count,
'time_zone': tweet.author.time_zone,
'url': tweet.author.url,
'utc_offset': tweet.author.utc_offset,
'verified': tweet.author.verified,
'_updated': datetime.datetime.now(),
}
authors.append(u)
ts.append(t)
self.update_authors(authors)
# insert into db
try:
self.db[self.DB_NAME].insert(ts)
except pymongo.errors.InvalidOperation: # no tweets?
pass
if self.debug:
print "added %s tweets to the db" % (len(ts))