matsuap commited on
Commit
e768b43
·
verified ·
1 Parent(s): 76358c9

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. api/websocket_routes.py +0 -37
  2. core/database.py +5 -6
api/websocket_routes.py CHANGED
@@ -1,7 +1,6 @@
1
  import logging
2
  import asyncio
3
  from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Depends
4
- from sqlalchemy import text
5
  from sqlalchemy.orm import Session
6
  from datetime import datetime
7
  from typing import Dict, Any
@@ -220,12 +219,6 @@ async def handle_podcast_task(connection_id: str, data: Dict, current_user: db_m
220
 
221
  public_url = s3_service.get_public_url(s3_key)
222
 
223
- # Ensure DB connection is alive before final update
224
- try:
225
- db.execute(text("SELECT 1"))
226
- except Exception:
227
- db.rollback()
228
-
229
  db_podcast.s3_key = s3_key
230
  db_podcast.s3_url = public_url
231
  db_podcast.script = script
@@ -312,12 +305,6 @@ async def handle_flashcards_task(connection_id: str, data: Dict, current_user: d
312
  )
313
  db.add(db_card)
314
 
315
- # Ensure DB connection is alive before final update
316
- try:
317
- db.execute(text("SELECT 1"))
318
- except Exception:
319
- db.rollback()
320
-
321
  db_set.status = "completed"
322
  db.commit()
323
 
@@ -397,12 +384,6 @@ async def handle_quiz_task(connection_id: str, data: Dict, current_user: db_mode
397
  )
398
  db.add(db_question)
399
 
400
- # Ensure DB connection is alive before final update
401
- try:
402
- db.execute(text("SELECT 1"))
403
- except Exception:
404
- db.rollback()
405
-
406
  db_set.status = "completed"
407
  db.commit()
408
  await manager.send_result(connection_id, {"id": db_set.id, "title": db_set.title, "status": "completed"})
@@ -454,12 +435,6 @@ async def handle_video_task(connection_id: str, data: Dict, current_user: db_mod
454
  voice_name=data.get("voice_name", "Kore")
455
  )
456
 
457
- # Ensure DB connection is alive before final update
458
- try:
459
- db.execute(text("SELECT 1"))
460
- except Exception:
461
- db.rollback()
462
-
463
  db_summary.title = result["title"]
464
  db_summary.s3_key = result["s3_key"]
465
  db_summary.s3_url = result["s3_url"]
@@ -522,12 +497,6 @@ async def handle_report_task(connection_id: str, data: Dict, current_user: db_mo
522
  if not content:
523
  raise Exception("AI failed to generate report content")
524
 
525
- # Ensure DB connection is alive before final update
526
- try:
527
- db.execute(text("SELECT 1"))
528
- except Exception:
529
- db.rollback()
530
-
531
  if not db_report.title or "Report-" not in db_report.title:
532
  title = content.split('\n')[0].replace('#', '').strip()
533
  if not title or len(title) < 3:
@@ -594,12 +563,6 @@ async def handle_mindmap_task(connection_id: str, data: Dict, current_user: db_m
594
  if not mermaid_code:
595
  raise Exception("AI failed to generate mind map code")
596
 
597
- # Ensure DB connection is alive before final update
598
- try:
599
- db.execute(text("SELECT 1"))
600
- except Exception:
601
- db.rollback()
602
-
603
  db_mindmap.mermaid_code = mermaid_code
604
  db_mindmap.status = "completed"
605
  db.commit()
 
1
  import logging
2
  import asyncio
3
  from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Depends
 
4
  from sqlalchemy.orm import Session
5
  from datetime import datetime
6
  from typing import Dict, Any
 
219
 
220
  public_url = s3_service.get_public_url(s3_key)
221
 
 
 
 
 
 
 
222
  db_podcast.s3_key = s3_key
223
  db_podcast.s3_url = public_url
224
  db_podcast.script = script
 
305
  )
306
  db.add(db_card)
307
 
 
 
 
 
 
 
308
  db_set.status = "completed"
309
  db.commit()
310
 
 
384
  )
385
  db.add(db_question)
386
 
 
 
 
 
 
 
387
  db_set.status = "completed"
388
  db.commit()
389
  await manager.send_result(connection_id, {"id": db_set.id, "title": db_set.title, "status": "completed"})
 
435
  voice_name=data.get("voice_name", "Kore")
436
  )
437
 
 
 
 
 
 
 
438
  db_summary.title = result["title"]
439
  db_summary.s3_key = result["s3_key"]
440
  db_summary.s3_url = result["s3_url"]
 
497
  if not content:
498
  raise Exception("AI failed to generate report content")
499
 
 
 
 
 
 
 
500
  if not db_report.title or "Report-" not in db_report.title:
501
  title = content.split('\n')[0].replace('#', '').strip()
502
  if not title or len(title) < 3:
 
563
  if not mermaid_code:
564
  raise Exception("AI failed to generate mind map code")
565
 
 
 
 
 
 
 
566
  db_mindmap.mermaid_code = mermaid_code
567
  db_mindmap.status = "completed"
568
  db.commit()
core/database.py CHANGED
@@ -11,13 +11,12 @@ SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL or "sqlite:///./temp.db"
11
  engine = create_engine(
12
  SQLALCHEMY_DATABASE_URL,
13
  pool_pre_ping=True, # Check connection health before every query
14
- pool_recycle=300, # Refresh connections every 5 minutes (prevents firewall drops)
15
- pool_timeout=60, # Wait up to 60 seconds for a connection
16
- pool_size=20, # Maintain a healthy pool
17
- max_overflow=30, # Allow more overflow if busy
18
  connect_args={
19
- "timeout": 60, # Command timeout
20
- "connect_timeout": 60 # Connection establishment timeout
21
  }
22
  )
23
 
 
11
  engine = create_engine(
12
  SQLALCHEMY_DATABASE_URL,
13
  pool_pre_ping=True, # Check connection health before every query
14
+ pool_recycle=1800, # 30 minutes (best practice)
15
+ pool_timeout=30, # Wait up to 30 seconds for a connection
16
+ pool_size=15, # Maintain a slightly larger pool
17
+ max_overflow=25, # Allow more overflow if busy
18
  connect_args={
19
+ "timeout": 30 # 30 second timeout for queries/connections
 
20
  }
21
  )
22