Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions backend/SC4SNMP_UI_backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
__version__ = "1.1.2-beta.2"

MONGO_URI = os.getenv("MONGO_URI")
logging.basicConfig(level=logging.INFO)



def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
def wait_for_mongodb_replicaset(logger, mongo_uri, max_retries=120, retry_interval=5):
"""
Wait for MongoDB to be ready before starting the application.
For replica sets, waits for PRIMARY to be elected.
Expand All @@ -28,15 +27,15 @@ def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
logger.info("MongoDB is in standalone mode, skipping ReplicaSet wait")
return

mongo_uri = os.getenv("MONGO_URI")

if not mongo_uri:
logger.warning("MONGO_URI not set, exiting application")
sys.exit(1)

logger.info(f"Waiting for MongoDB ReplicaSet to be ready and elect the primary...")

for attempt in range(1, max_retries + 1):
if attempt != 1:
time.sleep(retry_interval)
try:
# Try to connect
client = MongoClient(
Expand All @@ -49,7 +48,7 @@ def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
# For replica sets, verify PRIMARY exists
if "replicaSet=" in mongo_uri:
if client.primary is None:
raise Exception("No PRIMARY elected yet")
continue
logger.info(f"PRIMARY found: {client.primary}")

client.close()
Expand All @@ -58,20 +57,13 @@ def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):

except (ServerSelectionTimeoutError, ConnectionFailure, Exception) as e:
if attempt >= max_retries:
logger.info(
f"MongoDB not ready after {max_retries * retry_interval}s"
)
logger.info(f"MongoDB not ready after {max_retries * retry_interval}s")
logger.info(f" Error: {e}")
sys.exit(1)

if attempt % 6 == 0: # Print every 30 seconds
logger.info(
f" Still waiting... ({attempt}/{max_retries}) - {e.__class__.__name__}"
)

time.sleep(retry_interval)
logger.info(f" Still waiting... ({attempt}/{max_retries})")

wait_for_mongodb_replicaset(logging.getLogger())
wait_for_mongodb_replicaset(logging.getLogger(), MONGO_URI)
mongo_client = MongoClient(MONGO_URI)

VALUES_DIRECTORY = os.getenv("VALUES_DIRECTORY", "")
Expand Down
15 changes: 9 additions & 6 deletions backend/construct-connection-strings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
: "${REDIS_URL:=$BASE/${REDIS_DB:-1}}"
: "${CELERY_BROKER_URL:=$BASE/${CELERY_DB:-0}}"
fi
export REDIS_URL
export CELERY_BROKER_URL
export REDIS_MODE
fi

# Only construct if MONGO_URI not already set
if [ -z "$MONGO_URI" ]; then
# Build MongoDB URI from environment variables
if [ -n "$MONGODB_PASSWORD" ]; then
# With authentication
Expand All @@ -59,20 +65,17 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=${MONGODB_AUTH_SOURCE:-admin}&readPreference=primary"
else
# Standalone
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=${MONGODB_AUTH_SOURCE:-admin}"
fi
else
# Without authentication
if [ -n "$MONGODB_REPLICA_SET" ]; then
export MONGO_URI="mongodb://${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=admin&retryWrites=false"
export MONGO_URI="mongodb://${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=${MONGODB_AUTH_SOURCE:-admin}&retryWrites=false&readPreference=primary"
else
export MONGO_URI="mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
export MONGO_URI="mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=${MONGODB_AUTH_SOURCE:-admin}"
fi
fi

export MONGO_URI
export REDIS_URL
export CELERY_BROKER_URL
export REDIS_MODE

fi
Loading