Melika Kheirieh commited on
Commit
f55959d
·
1 Parent(s): 666306b

fix(state): enforce TypedDict types and cleanup stale DB removal logic

Browse files
Files changed (1) hide show
  1. app/state.py +17 -10
app/state.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import time
3
  import logging
4
  from pathlib import Path
5
- from typing import Optional
6
 
7
  log = logging.getLogger(__name__)
8
 
@@ -14,8 +14,14 @@ log = logging.getLogger(__name__)
14
  _DB_UPLOAD_DIR = Path(os.getenv("DB_UPLOAD_DIR", "/tmp/nl2sql_dbs"))
15
  _DB_UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
16
 
 
 
 
 
 
 
17
  # in-memory map: {db_id: {"path": str, "ts": float}}
18
- DB_MAP: dict[str, dict[str, str | float]] = {}
19
 
20
  # cleanup threshold (hours)
21
  DB_TTL_HOURS = 6
@@ -39,10 +45,12 @@ def cleanup_stale_dbs() -> None:
39
  cutoff = DB_TTL_HOURS * 3600
40
  stale_ids = [db_id for db_id, entry in DB_MAP.items() if now - entry["ts"] > cutoff]
41
  for db_id in stale_ids:
42
- path = DB_MAP[db_id]["path"]
 
43
  try:
44
- os.remove(path)
45
- log.info(f"🧹 Deleted stale DB: {path}")
 
46
  except FileNotFoundError:
47
  pass
48
  DB_MAP.pop(db_id, None)
@@ -50,12 +58,12 @@ def cleanup_stale_dbs() -> None:
50
 
51
  def get_db_path(db_id: str) -> Optional[str]:
52
  """Return full path of an uploaded DB (persistent lookup)."""
53
- # ⃣ in-memory lookup
54
  entry = DB_MAP.get(db_id)
55
- if entry and Path(entry["path"]).exists():
56
- return entry["path"]
 
 
57
 
58
- # ⃣ persistent fallback scan
59
  candidates = [
60
  _DB_UPLOAD_DIR / f"{db_id}.sqlite",
61
  _DB_UPLOAD_DIR / f"{db_id}.db",
@@ -67,6 +75,5 @@ def get_db_path(db_id: str) -> Optional[str]:
67
  log.info(f"🔍 Recovered DB path for {db_id}: {p}")
68
  return str(p)
69
 
70
- # ⃣ not found
71
  log.warning(f"⚠️ DB file not found for id={db_id}")
72
  return None
 
2
  import time
3
  import logging
4
  from pathlib import Path
5
+ from typing import Optional, TypedDict
6
 
7
  log = logging.getLogger(__name__)
8
 
 
14
  _DB_UPLOAD_DIR = Path(os.getenv("DB_UPLOAD_DIR", "/tmp/nl2sql_dbs"))
15
  _DB_UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
16
 
17
+
18
+ class DBEntry(TypedDict):
19
+ path: str
20
+ ts: float
21
+
22
+
23
  # in-memory map: {db_id: {"path": str, "ts": float}}
24
+ DB_MAP: dict[str, DBEntry] = {}
25
 
26
  # cleanup threshold (hours)
27
  DB_TTL_HOURS = 6
 
45
  cutoff = DB_TTL_HOURS * 3600
46
  stale_ids = [db_id for db_id, entry in DB_MAP.items() if now - entry["ts"] > cutoff]
47
  for db_id in stale_ids:
48
+ path_str = DB_MAP[db_id]["path"]
49
+ path = Path(path_str)
50
  try:
51
+ if path.exists():
52
+ path.unlink()
53
+ log.info(f"🧹 Deleted stale DB: {path}")
54
  except FileNotFoundError:
55
  pass
56
  DB_MAP.pop(db_id, None)
 
58
 
59
  def get_db_path(db_id: str) -> Optional[str]:
60
  """Return full path of an uploaded DB (persistent lookup)."""
 
61
  entry = DB_MAP.get(db_id)
62
+ if entry:
63
+ path_str = entry["path"]
64
+ if Path(path_str).exists():
65
+ return path_str
66
 
 
67
  candidates = [
68
  _DB_UPLOAD_DIR / f"{db_id}.sqlite",
69
  _DB_UPLOAD_DIR / f"{db_id}.db",
 
75
  log.info(f"🔍 Recovered DB path for {db_id}: {p}")
76
  return str(p)
77
 
 
78
  log.warning(f"⚠️ DB file not found for id={db_id}")
79
  return None