|
1 | 1 | from pymongo import MongoClient
|
2 | 2 | import os
|
3 | 3 | from dotenv import load_dotenv
|
4 |
| -import pymongo |
| 4 | +import threading |
| 5 | +import time |
5 | 6 |
|
6 | 7 | load_dotenv()
|
7 | 8 |
|
|
13 | 14 | file_name = "/etc/ssl/ca-certificate.crt"
|
14 | 15 | use_tls = True
|
15 | 16 |
|
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 |
22 | 18 | if use_tls:
|
23 | 19 | client = MongoClient(
|
24 | 20 | os.getenv("MONGO_URI"),
|
25 | 21 | tls=True,
|
26 | 22 | 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 |
33 | 23 | )
|
34 | 24 | 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")) |
43 | 26 |
|
44 | 27 | # Force connection on startup
|
45 | 28 | try:
|
|
49 | 32 | print(f"❌ MongoDB connection failed on startup: {e}")
|
50 | 33 | raise e
|
51 | 34 |
|
| 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 | + |
52 | 49 | # Access the database
|
53 | 50 | db = client[os.getenv("MONGO_DB", "score_db")]
|
54 | 51 |
|
55 | 52 |
|
56 |
| -# Setup indexes only if they don't exist |
57 | 53 | def setup_database_indexes():
|
58 | 54 | """Set up MongoDB indexes for optimal query performance"""
|
59 | 55 | try:
|
60 | 56 | game_collection = db["game"]
|
61 | 57 |
|
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") |
89 | 70 | 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 |
92 | 73 |
|
93 | 74 |
|
94 |
| -# Setup indexes |
95 | 75 | setup_database_indexes()
|
0 commit comments