LogicGoInfotechSpaces commited on
Commit
de765fa
·
verified ·
1 Parent(s): fc9e298

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -1
app.py CHANGED
@@ -17,6 +17,7 @@ from huggingface_hub import hf_hub_download
17
  from fastapi import FastAPI, UploadFile, File, HTTPException, Response, Depends, Security, Query
18
  from fastapi.responses import RedirectResponse
19
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
 
20
  from pydantic import BaseModel
21
  from motor.motor_asyncio import AsyncIOMotorClient
22
 
@@ -235,9 +236,62 @@ def download_from_spaces(key):
235
  def root():
236
  return RedirectResponse("/gradio")
237
 
 
 
 
238
  @fastapi_app.get("/health")
239
  async def health():
240
- return {"status": "healthy"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
  from fastapi import Form
243
  import requests
 
17
  from fastapi import FastAPI, UploadFile, File, HTTPException, Response, Depends, Security, Query
18
  from fastapi.responses import RedirectResponse
19
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
20
+ from fastapi.concurrency import run_in_threadpool
21
  from pydantic import BaseModel
22
  from motor.motor_asyncio import AsyncIOMotorClient
23
 
 
236
  def root():
237
  return RedirectResponse("/gradio")
238
 
239
+ # @fastapi_app.get("/health")
240
+ # async def health():
241
+ # return {"status": "healthy
242
  @fastapi_app.get("/health")
243
  async def health():
244
+ mongo_ok = False
245
+ spaces_ok = False
246
+ mongo_error = None
247
+ spaces_error = None
248
+
249
+ # ---- MongoDB Check (ASYNC SAFE) ----
250
+ try:
251
+ if database is None:
252
+ raise Exception("MongoDB client not initialized")
253
+
254
+ await database.command("ping")
255
+ mongo_ok = True
256
+
257
+ except Exception as e:
258
+ mongo_error = str(e)
259
+
260
+ # ---- DigitalOcean Spaces Check (RUN IN THREADPOOL) ----
261
+ try:
262
+ def check_spaces():
263
+ client = get_spaces_client()
264
+ client.list_objects_v2(
265
+ Bucket=DO_SPACES_BUCKET,
266
+ MaxKeys=1
267
+ )
268
+
269
+ await run_in_threadpool(check_spaces)
270
+ spaces_ok = True
271
+
272
+ except Exception as e:
273
+ spaces_error = str(e)
274
+
275
+ # ---- FINAL RESPONSE ----
276
+ if mongo_ok and spaces_ok:
277
+ return {
278
+ "status": "healthy",
279
+ "mongo": "connected",
280
+ "spaces": "connected",
281
+ "timestamp": datetime.utcnow().isoformat()
282
+ }
283
+
284
+ raise HTTPException(
285
+ status_code=503,
286
+ detail={
287
+ "status": "unhealthy",
288
+ "mongo": "connected" if mongo_ok else "failed",
289
+ "spaces": "connected" if spaces_ok else "failed",
290
+ "mongo_error": mongo_error,
291
+ "spaces_error": spaces_error,
292
+ "timestamp": datetime.utcnow().isoformat()
293
+ }
294
+ )
295
 
296
  from fastapi import Form
297
  import requests