Ubuntu commited on
Commit
e136d4a
·
1 Parent(s): f2da7fe

mongo fixes

Browse files
Files changed (5) hide show
  1. .qwen/settings.json +2 -1
  2. Dockerfile +12 -15
  3. main.py +8 -2
  4. mongo_client.py +17 -7
  5. start.sh +17 -0
.qwen/settings.json CHANGED
@@ -3,7 +3,8 @@
3
  "allow": [
4
  "Bash(curl *)",
5
  "Bash(python3 *)",
6
- "Bash(sleep *)"
 
7
  ]
8
  },
9
  "$version": 3
 
3
  "allow": [
4
  "Bash(curl *)",
5
  "Bash(python3 *)",
6
+ "Bash(sleep *)",
7
+ "Bash(sudo *)"
8
  ]
9
  },
10
  "$version": 3
Dockerfile CHANGED
@@ -4,17 +4,15 @@
4
  FROM python:3.12-slim
5
 
6
  # Install dependencies
7
- RUN apt-get update && apt-get install -y redis-server gnupg wget && \
8
  rm -rf /var/lib/apt/lists/*
9
 
10
- # Install MongoDB
11
- RUN apt-get update && \
12
- GNUPGHOME=/tmp gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 9B3B7D436F8E4A3426B64D3D610C8B9831C4F6FC && \
13
- GNUPGHOME=/tmp gpg --export 9B3B7D436F8E4A3426B64D3D610C8B9831C4F6FC > /usr/share/keyrings/mongodb-archive-keyring.gpg && \
14
- echo "deb [ signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list && \
15
  apt-get update && \
16
- apt-get install -y --allow-unauthenticated mongodb-org || \
17
- echo "MongoDB install attempted"
18
 
19
  # Install MinIO
20
  RUN wget -q https://dl.min.io/server/minio/release/linux-amd64/minio -O /usr/local/bin/minio && \
@@ -61,12 +59,11 @@ RUN mkdir -p /app/chroma_db /app/data /app/redis_data /app/mongo_data /app/minio
61
  # Seed the database (consistent data in every container)
62
  RUN uv run python seed_db.py
63
 
 
 
 
 
64
  EXPOSE 7860 6379 27017 9000 9001
65
 
66
- # Start Redis, MongoDB, MinIO and the app
67
- CMD ["sh", "-c", "\
68
- redis-server --daemonize yes --dir /app/redis_data --appendonly yes && \
69
- (mongod --dbpath /app/mongo_data --bind_ip 127.0.0.1 --fork --logpath /app/mongo_data/mongod.log 2>/dev/null || echo 'MongoDB not available') && \
70
- (minio server /app/minio_data --address 127.0.0.1:9000 --console-address 127.0.0.1:9001 --quiet &>/dev/null &) && \
71
- sleep 2 && \
72
- uvicorn main:app --host 0.0.0.0 --port 7860"]
 
4
  FROM python:3.12-slim
5
 
6
  # Install dependencies
7
+ RUN apt-get update && apt-get install -y redis-server gnupg wget curl && \
8
  rm -rf /var/lib/apt/lists/*
9
 
10
+ # Install MongoDB (using apt-get with insecure repo for bookworm compatibility)
11
+ RUN curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor && \
12
+ echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg trusted=yes ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list && \
 
 
13
  apt-get update && \
14
+ apt-get install -y mongodb-org && \
15
+ rm -rf /var/lib/apt/lists/*
16
 
17
  # Install MinIO
18
  RUN wget -q https://dl.min.io/server/minio/release/linux-amd64/minio -O /usr/local/bin/minio && \
 
59
  # Seed the database (consistent data in every container)
60
  RUN uv run python seed_db.py
61
 
62
+ # Copy startup script
63
+ COPY --chown=user start.sh /app/start.sh
64
+ RUN chmod +x /app/start.sh
65
+
66
  EXPOSE 7860 6379 27017 9000 9001
67
 
68
+ # Start Redis, MongoDB, MinIO and the app using startup script
69
+ CMD ["/app/start.sh"]
 
 
 
 
 
main.py CHANGED
@@ -102,6 +102,7 @@ async def health_check():
102
  from redis_client import get_redis_client
103
  redis_status = "disconnected"
104
  mongo_status = "disconnected"
 
105
  minio_status = "disconnected"
106
 
107
  try:
@@ -114,11 +115,15 @@ async def health_check():
114
 
115
  try:
116
  mongo_db = get_mongo_db()
117
- if mongo_db:
118
  await mongo_db.command('ping')
119
  mongo_status = "connected"
120
- except Exception:
 
 
 
121
  mongo_status = "error"
 
122
 
123
  # MinIO health check
124
  minio_health = check_minio_health()
@@ -128,6 +133,7 @@ async def health_check():
128
  "status": "healthy",
129
  "redis": redis_status,
130
  "mongodb": mongo_status,
 
131
  "minio": minio_status,
132
  "database": "sqlite"
133
  }
 
102
  from redis_client import get_redis_client
103
  redis_status = "disconnected"
104
  mongo_status = "disconnected"
105
+ mongo_details = {}
106
  minio_status = "disconnected"
107
 
108
  try:
 
115
 
116
  try:
117
  mongo_db = get_mongo_db()
118
+ if mongo_db is not None:
119
  await mongo_db.command('ping')
120
  mongo_status = "connected"
121
+ # Get collection stats
122
+ collections = await mongo_db.list_collection_names()
123
+ mongo_details["collections"] = collections
124
+ except Exception as e:
125
  mongo_status = "error"
126
+ mongo_details["error"] = str(e)
127
 
128
  # MinIO health check
129
  minio_health = check_minio_health()
 
133
  "status": "healthy",
134
  "redis": redis_status,
135
  "mongodb": mongo_status,
136
+ "mongodb_details": mongo_details,
137
  "minio": minio_status,
138
  "database": "sqlite"
139
  }
mongo_client.py CHANGED
@@ -30,27 +30,37 @@ async def init_mongodb() -> None:
30
  """Initialize MongoDB connection."""
31
  global _mongo_client, _mongo_db
32
  try:
33
- _mongo_client = AsyncIOMotorClient(MONGO_URI)
 
 
 
 
 
 
34
  _mongo_db = _mongo_client[MONGO_DB_NAME]
35
-
36
- # Test connection
37
  await _mongo_client.admin.command('ping')
38
  print(f"✓ Connected to MongoDB at {MONGO_URI}")
39
-
40
  # Create indexes for play_history collection
41
  await _mongo_db.play_history.create_index([("user_id", ASCENDING), ("played_at", DESCENDING)])
42
  await _mongo_db.play_history.create_index([("song_id", ASCENDING)])
43
  await _mongo_db.play_history.create_index([("played_at", DESCENDING)])
44
-
45
  # Create indexes for activity_logs collection
46
  await _mongo_db.activity_logs.create_index([("user_id", ASCENDING), ("timestamp", DESCENDING)])
47
  await _mongo_db.activity_logs.create_index([("action_type", ASCENDING)])
48
  await _mongo_db.activity_logs.create_index([("timestamp", DESCENDING)])
49
-
50
  # Create indexes for analytics collection
51
  await _mongo_db.analytics.create_index([("event_type", ASCENDING), ("timestamp", DESCENDING)])
52
  await _mongo_db.analytics.create_index([("timestamp", DESCENDING)])
53
-
 
 
 
 
54
  print("✓ MongoDB indexes created")
55
  except Exception as e:
56
  print(f"⚠ MongoDB connection failed: {e}")
 
30
  """Initialize MongoDB connection."""
31
  global _mongo_client, _mongo_db
32
  try:
33
+ # Use serverSelectionTimeoutMS to fail fast if MongoDB is not available
34
+ _mongo_client = AsyncIOMotorClient(
35
+ MONGO_URI,
36
+ serverSelectionTimeoutMS=3000,
37
+ connectTimeoutMS=5000,
38
+ socketTimeoutMS=5000
39
+ )
40
  _mongo_db = _mongo_client[MONGO_DB_NAME]
41
+
42
+ # Test connection with ping
43
  await _mongo_client.admin.command('ping')
44
  print(f"✓ Connected to MongoDB at {MONGO_URI}")
45
+
46
  # Create indexes for play_history collection
47
  await _mongo_db.play_history.create_index([("user_id", ASCENDING), ("played_at", DESCENDING)])
48
  await _mongo_db.play_history.create_index([("song_id", ASCENDING)])
49
  await _mongo_db.play_history.create_index([("played_at", DESCENDING)])
50
+
51
  # Create indexes for activity_logs collection
52
  await _mongo_db.activity_logs.create_index([("user_id", ASCENDING), ("timestamp", DESCENDING)])
53
  await _mongo_db.activity_logs.create_index([("action_type", ASCENDING)])
54
  await _mongo_db.activity_logs.create_index([("timestamp", DESCENDING)])
55
+
56
  # Create indexes for analytics collection
57
  await _mongo_db.analytics.create_index([("event_type", ASCENDING), ("timestamp", DESCENDING)])
58
  await _mongo_db.analytics.create_index([("timestamp", DESCENDING)])
59
+
60
+ # Create indexes for search_history collection
61
+ await _mongo_db.search_history.create_index([("query", ASCENDING)])
62
+ await _mongo_db.search_history.create_index([("timestamp", DESCENDING)])
63
+
64
  print("✓ MongoDB indexes created")
65
  except Exception as e:
66
  print(f"⚠ MongoDB connection failed: {e}")
start.sh ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo "Starting Redis..."
5
+ redis-server --daemonize yes --dir /app/redis_data --appendonly yes
6
+ sleep 2
7
+
8
+ echo "Starting MongoDB..."
9
+ /usr/bin/mongod --dbpath /app/mongo_data --bind_ip 127.0.0.1 --fork --logpath /app/mongo_data/mongod.log
10
+ sleep 2
11
+
12
+ echo "Starting MinIO..."
13
+ /usr/local/bin/minio server /app/minio_data --address 127.0.0.1:9000 --console-address 127.0.0.1:9001 &
14
+ sleep 3
15
+
16
+ echo "Starting FastAPI application..."
17
+ exec uvicorn main:app --host 0.0.0.0 --port 7860