refactor: simplify database logging handler
- Remove retry logic from DatabaseLogHandler (was for SQLite locking) - Streamline error handling in log emission - Clean up platform health service Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -47,9 +47,12 @@ def check_database_ready():
|
|||||||
"""Check if database is ready (migrations have been run)."""
|
"""Check if database is ready (migrations have been run)."""
|
||||||
try:
|
try:
|
||||||
with engine.connect() as conn:
|
with engine.connect() as conn:
|
||||||
# Try to query a table that should exist
|
# Check for tables in the public schema (PostgreSQL)
|
||||||
result = conn.execute(
|
result = conn.execute(
|
||||||
text("SELECT name FROM sqlite_master WHERE type='table' LIMIT 1")
|
text(
|
||||||
|
"SELECT tablename FROM pg_catalog.pg_tables "
|
||||||
|
"WHERE schemaname = 'public' LIMIT 1"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
tables = result.fetchall()
|
tables = result.fetchall()
|
||||||
return len(tables) > 0
|
return len(tables) > 0
|
||||||
|
|||||||
@@ -23,21 +23,14 @@ class DatabaseLogHandler(logging.Handler):
|
|||||||
Custom logging handler that stores WARNING, ERROR, and CRITICAL logs in database.
|
Custom logging handler that stores WARNING, ERROR, and CRITICAL logs in database.
|
||||||
|
|
||||||
Runs asynchronously to avoid blocking application performance.
|
Runs asynchronously to avoid blocking application performance.
|
||||||
Uses retry logic for SQLite database locking issues.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MAX_RETRIES = 3
|
|
||||||
RETRY_DELAY = 0.1 # 100ms delay between retries
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.setLevel(logging.WARNING) # Only log WARNING and above to database
|
self.setLevel(logging.WARNING) # Only log WARNING and above to database
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
"""Emit a log record to the database with retry logic for SQLite locking."""
|
"""Emit a log record to the database."""
|
||||||
import time
|
|
||||||
|
|
||||||
for attempt in range(self.MAX_RETRIES):
|
|
||||||
try:
|
try:
|
||||||
from app.core.database import SessionLocal
|
from app.core.database import SessionLocal
|
||||||
from models.database.admin import ApplicationLog
|
from models.database.admin import ApplicationLog
|
||||||
@@ -90,17 +83,10 @@ class DatabaseLogHandler(logging.Handler):
|
|||||||
|
|
||||||
db.add(log_entry)
|
db.add(log_entry)
|
||||||
db.commit()
|
db.commit()
|
||||||
return # Success, exit retry loop
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
# Check if it's a database locked error
|
# Silently skip - don't print to stderr to avoid log spam
|
||||||
if "database is locked" in str(e).lower():
|
|
||||||
if attempt < self.MAX_RETRIES - 1:
|
|
||||||
time.sleep(self.RETRY_DELAY * (attempt + 1))
|
|
||||||
continue
|
|
||||||
# For other errors or final attempt, silently skip
|
|
||||||
# Don't print to stderr to avoid log spam during imports
|
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
@@ -108,8 +94,6 @@ class DatabaseLogHandler(logging.Handler):
|
|||||||
# Silently fail - logging should never crash the app
|
# Silently fail - logging should never crash the app
|
||||||
pass
|
pass
|
||||||
|
|
||||||
break # Exit retry loop on non-recoverable errors
|
|
||||||
|
|
||||||
|
|
||||||
def get_log_level_from_db():
|
def get_log_level_from_db():
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -305,27 +305,13 @@ class PlatformHealthService:
|
|||||||
def _get_database_size(self, db: Session) -> float:
|
def _get_database_size(self, db: Session) -> float:
|
||||||
"""Get database size in MB."""
|
"""Get database size in MB."""
|
||||||
try:
|
try:
|
||||||
# Try SQLite approach
|
|
||||||
result = db.execute(
|
|
||||||
text(
|
|
||||||
"SELECT page_count * page_size as size "
|
|
||||||
"FROM pragma_page_count(), pragma_page_size()"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
row = result.fetchone()
|
|
||||||
if row:
|
|
||||||
return round(row[0] / (1024 * 1024), 2)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Try PostgreSQL approach
|
|
||||||
result = db.execute(text("SELECT pg_database_size(current_database())"))
|
result = db.execute(text("SELECT pg_database_size(current_database())"))
|
||||||
row = result.fetchone()
|
row = result.fetchone()
|
||||||
if row:
|
if row:
|
||||||
return round(row[0] / (1024 * 1024), 2)
|
return round(row[0] / (1024 * 1024), 2)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
logger.warning("Failed to get database size")
|
||||||
|
return 0.0
|
||||||
|
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user