From 78ac2ccb553c83172f1cfbcfcb6cbfdc0198a1ea Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Fri, 4 Dec 2015 13:23:43 -0500 Subject: [PATCH] more robust check added to mongo db connection Calling mongoengine (or pymongo) connect() after already calling connect() resulting in getting the first connection (connection pooling). This is bad if we are relying on connect() to determine if mongo is up. Added executing a ping/pong command after the connect() command to ensure mongo is still/really up. --- awx/fact/utils/connection.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/awx/fact/utils/connection.py b/awx/fact/utils/connection.py index 75fe897b3b..4ea5f23bd1 100644 --- a/awx/fact/utils/connection.py +++ b/awx/fact/utils/connection.py @@ -4,6 +4,7 @@ from django.conf import settings from mongoengine import connect from mongoengine.connection import ConnectionError +from pymongo.errors import AutoReconnect def test_mongo_connection(): # Connect to Mongo @@ -14,13 +15,14 @@ def test_mongo_connection(): raise ConnectionError # Attempt to connect to the MongoDB database. - connect(settings.MONGO_DB, - host=settings.MONGO_HOST, - port=int(settings.MONGO_PORT), - username=settings.MONGO_USERNAME, - password=settings.MONGO_PASSWORD, - tz_aware=settings.USE_TZ) + db = connect(settings.MONGO_DB, + host=settings.MONGO_HOST, + port=int(settings.MONGO_PORT), + username=settings.MONGO_USERNAME, + password=settings.MONGO_PASSWORD, + tz_aware=settings.USE_TZ) + db[settings.MONGO_DB].command('ping') return True - except ConnectionError: + except ConnectionError, AutoReconnect: return False