Skip to content

Commit 3dc57b7

Browse files
1 workers
1 parent 915e84d commit 3dc57b7

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ WORKDIR /usr/src/app
44
COPY . .
55
RUN pip3 install --upgrade pip
66
RUN pip install -r requirements.txt
7-
CMD gunicorn app:app -b 0.0.0.0:8000 --workers 2 --timeout 60 --max-requests 1000 --max-requests-jitter 200
7+
CMD gunicorn app:app -b 0.0.0.0:8000 --workers 1 --timeout 60 --max-requests 1000 --max-requests-jitter 200

src/database.py

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from pymongo import MongoClient
22
import os
33
from dotenv import load_dotenv
4-
import pymongo
4+
import threading
5+
import time
56

67
load_dotenv()
78

@@ -13,33 +14,15 @@
1314
file_name = "/etc/ssl/ca-certificate.crt"
1415
use_tls = True
1516

16-
# Connection pooling settings
17-
max_pool_size = 10 # Maximum number of connections in the pool
18-
min_pool_size = 3 # Minimum number of connections to maintain
19-
max_idle_time_ms = 60000 # 1 minute
20-
21-
# Initialize MongoDB client with optimized settings
17+
# Initialize MongoDB client
2218
if use_tls:
2319
client = MongoClient(
2420
os.getenv("MONGO_URI"),
2521
tls=True,
2622
tlsCAFile=file_name,
27-
maxPoolSize=max_pool_size,
28-
minPoolSize=min_pool_size,
29-
maxIdleTimeMS=max_idle_time_ms,
30-
waitQueueTimeoutMS=2000, # Wait 2 seconds for connection before timeout
31-
retryWrites=True,
32-
# Remove serverSelectionTimeoutMS to use MongoDB default
3323
)
3424
else:
35-
client = MongoClient(
36-
os.getenv("MONGO_URI"),
37-
maxPoolSize=max_pool_size,
38-
minPoolSize=min_pool_size,
39-
maxIdleTimeMS=max_idle_time_ms,
40-
waitQueueTimeoutMS=2000,
41-
retryWrites=True,
42-
)
25+
client = MongoClient(os.getenv("MONGO_URI"))
4326

4427
# Force connection on startup
4528
try:
@@ -49,47 +32,44 @@
4932
print(f"❌ MongoDB connection failed on startup: {e}")
5033
raise e
5134

35+
36+
# Start a keep-alive thread
37+
def keep_connection_alive():
38+
while True:
39+
try:
40+
client.admin.command("ping")
41+
print("🔁 MongoDB keep-alive ping successful")
42+
except Exception as e:
43+
print(f"⚠️ MongoDB keep-alive ping failed: {e}")
44+
time.sleep(300) # ping every 5 minutes
45+
46+
47+
threading.Thread(target=keep_connection_alive, daemon=True).start()
48+
5249
# Access the database
5350
db = client[os.getenv("MONGO_DB", "score_db")]
5451

5552

56-
# Setup indexes only if they don't exist
5753
def setup_database_indexes():
5854
"""Set up MongoDB indexes for optimal query performance"""
5955
try:
6056
game_collection = db["game"]
6157

62-
# Get existing indexes
63-
existing_indexes = game_collection.index_information()
64-
65-
# Define indexes we want
66-
indexes = [
67-
("sport", pymongo.ASCENDING),
68-
("date", pymongo.ASCENDING),
69-
("gender", pymongo.ASCENDING),
70-
[("sport", pymongo.ASCENDING), ("gender", pymongo.ASCENDING)],
71-
("date", pymongo.DESCENDING),
72-
]
73-
74-
# Create only missing indexes
75-
for index in indexes:
76-
# Convert single field index to list format for consistency
77-
index_list = [index] if not isinstance(index, list) else index
78-
79-
# Generate index name
80-
index_name = "_".join(
81-
[f"{field}_{direction}" for field, direction in index_list]
82-
)
83-
84-
if index_name not in existing_indexes:
85-
game_collection.create_index(index_list, background=True)
86-
print(f"Created index: {index_name}")
87-
88-
print("✅ MongoDB indexes verified")
58+
# Create single field indexes for commonly queried fields
59+
game_collection.create_index([("sport", 1)], background=True)
60+
game_collection.create_index([("date", 1)], background=True)
61+
game_collection.create_index([("gender", 1)], background=True)
62+
63+
# Create compound indexes for common query combinations
64+
game_collection.create_index([("sport", 1), ("gender", 1)], background=True)
65+
66+
# Index for sorting operations
67+
game_collection.create_index([("date", -1)], background=True)
68+
69+
print("✅ MongoDB indexes created successfully")
8970
except Exception as e:
90-
print(f"❌ Failed to verify MongoDB indexes: {e}")
91-
# Don't raise exception here, just log the error
71+
print(f"❌ Failed to create MongoDB indexes: {e}")
72+
raise e
9273

9374

94-
# Setup indexes
9575
setup_database_indexes()

0 commit comments

Comments
 (0)