This commit is contained in:
2026-02-01 19:01:47 -08:00
parent 95748698fa
commit f0a82c2403
7 changed files with 113 additions and 50 deletions

View File

@@ -50,6 +50,7 @@ def _resolve_root_dir() -> Path:
ROOT_DIR = _resolve_root_dir()
DB_PATH = (ROOT_DIR / "medios.db").resolve()
LOG_DB_PATH = (ROOT_DIR / "logs.db").resolve()
class Database:
_instance: Optional[Database] = None
@@ -286,6 +287,38 @@ _LOG_THREAD_STARTED = False
_LOG_THREAD_LOCK = threading.Lock()
def _ensure_log_db_schema() -> None:
try:
conn = sqlite3.connect(
str(LOG_DB_PATH),
timeout=30.0,
check_same_thread=False,
)
try:
conn.execute("PRAGMA busy_timeout = 30000")
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
conn.execute(
"""
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
level TEXT,
module TEXT,
message TEXT
)
"""
)
conn.commit()
finally:
conn.close()
except Exception:
pass
_ensure_log_db_schema()
def _log_worker_loop() -> None:
"""Background log writer using a temporary per-write connection with
small retry/backoff and a file fallback when writes fail repeatedly.
@@ -297,9 +330,13 @@ def _log_worker_loop() -> None:
written = False
while attempts < 3 and not written:
try:
# Create a short-lived connection for the logging write so the
# logging thread does not contend with the main connection lock.
conn = sqlite3.connect(str(db.db_path), timeout=5.0)
conn = sqlite3.connect(str(LOG_DB_PATH), timeout=30.0)
try:
conn.execute("PRAGMA busy_timeout = 30000")
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
except sqlite3.Error:
pass
cur = conn.cursor()
cur.execute("INSERT INTO logs (level, module, message) VALUES (?, ?, ?)", (level, module, message))
conn.commit()