Skip to content

Commit 1011453

Browse files
fix: add fallback for connection strings
fix: add fallback for connection strings
2 parents fcec7c3 + 4e6296a commit 1011453

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
### Changed
4+
- add fallback for MONGODB and REDIS connection strings, fix logging
45
- create MONGODB connection string from environment variables instead of full MONGO_URI variable
56
- create REDIS connection string from environment variables instead of full REDIS_URL variable
67

backend/SC4SNMP_UI_backend/__init__.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
__version__ = "1.1.2-beta.2"
1616

1717
MONGO_URI = os.getenv("MONGO_URI")
18+
log = logging.getLogger('gunicorn.error')
19+
log.setLevel(logging.INFO)
1820

19-
20-
21-
def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
21+
def wait_for_mongodb_replicaset(logger, mongo_uri, max_retries=120, retry_interval=5):
2222
"""
2323
Wait for MongoDB to be ready before starting the application.
2424
For replica sets, waits for PRIMARY to be elected.
@@ -28,15 +28,15 @@ def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
2828
logger.info("MongoDB is in standalone mode, skipping ReplicaSet wait")
2929
return
3030

31-
mongo_uri = os.getenv("MONGO_URI")
32-
3331
if not mongo_uri:
3432
logger.warning("MONGO_URI not set, exiting application")
3533
sys.exit(1)
3634

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

3937
for attempt in range(1, max_retries + 1):
38+
if attempt != 1:
39+
time.sleep(retry_interval)
4040
try:
4141
# Try to connect
4242
client = MongoClient(
@@ -49,7 +49,7 @@ def wait_for_mongodb_replicaset(logger, max_retries=120, retry_interval=5):
4949
# For replica sets, verify PRIMARY exists
5050
if "replicaSet=" in mongo_uri:
5151
if client.primary is None:
52-
raise Exception("No PRIMARY elected yet")
52+
continue
5353
logger.info(f"PRIMARY found: {client.primary}")
5454

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

5959
except (ServerSelectionTimeoutError, ConnectionFailure, Exception) as e:
6060
if attempt >= max_retries:
61-
logger.info(
62-
f"MongoDB not ready after {max_retries * retry_interval}s"
63-
)
61+
logger.info(f"MongoDB not ready after {max_retries * retry_interval}s")
6462
logger.info(f" Error: {e}")
6563
sys.exit(1)
6664

67-
if attempt % 6 == 0: # Print every 30 seconds
68-
logger.info(
69-
f" Still waiting... ({attempt}/{max_retries}) - {e.__class__.__name__}"
70-
)
71-
72-
time.sleep(retry_interval)
65+
logger.info(f" Still waiting... ({attempt}/{max_retries})")
7366

74-
wait_for_mongodb_replicaset(logging.getLogger())
67+
wait_for_mongodb_replicaset(log, MONGO_URI)
7568
mongo_client = MongoClient(MONGO_URI)
7669

7770
VALUES_DIRECTORY = os.getenv("VALUES_DIRECTORY", "")

backend/construct-connection-strings.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
5050
: "${REDIS_URL:=$BASE/${REDIS_DB:-1}}"
5151
: "${CELERY_BROKER_URL:=$BASE/${CELERY_DB:-0}}"
5252
fi
53+
export REDIS_URL
54+
export CELERY_BROKER_URL
55+
export REDIS_MODE
56+
fi
5357

58+
# Only construct if MONGO_URI not already set
59+
if [ -z "$MONGO_URI" ]; then
5460
# Build MongoDB URI from environment variables
5561
if [ -n "$MONGODB_PASSWORD" ]; then
5662
# With authentication
@@ -59,20 +65,17 @@ if [ -z "$REDIS_URL" ] || [ -z "$CELERY_BROKER_URL" ]; then
5965
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=${MONGODB_AUTH_SOURCE:-admin}&readPreference=primary"
6066
else
6167
# Standalone
62-
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
68+
export MONGO_URI="mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=${MONGODB_AUTH_SOURCE:-admin}"
6369
fi
6470
else
6571
# Without authentication
6672
if [ -n "$MONGODB_REPLICA_SET" ]; then
67-
export MONGO_URI="mongodb://${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=admin&retryWrites=false"
73+
export MONGO_URI="mongodb://${MONGODB_HOST}/${MONGODB_DATABASE}?replicaSet=${MONGODB_REPLICA_SET}&authSource=${MONGODB_AUTH_SOURCE:-admin}&retryWrites=false&readPreference=primary"
6874
else
69-
export MONGO_URI="mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin"
75+
export MONGO_URI="mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=${MONGODB_AUTH_SOURCE:-admin}"
7076
fi
7177
fi
7278

7379
export MONGO_URI
74-
export REDIS_URL
75-
export CELERY_BROKER_URL
76-
export REDIS_MODE
7780

7881
fi

0 commit comments

Comments
 (0)