This commit is contained in:
2026-01-23 17:12:15 -08:00
parent b3a4ba14e5
commit 883ac2a283
4 changed files with 21 additions and 53 deletions

2
CLI.py
View File

@@ -571,7 +571,7 @@ class ConfigLoader:
def load(self) -> Dict[str, Any]: def load(self) -> Dict[str, Any]:
try: try:
return deepcopy(load_config(config_dir=self._root)) return deepcopy(load_config())
except Exception: except Exception:
return {} return {}

View File

@@ -275,7 +275,7 @@ def _run_op(op: str, data: Any) -> Dict[str, Any]:
from Store import Store # noqa: WPS433 from Store import Store # noqa: WPS433
config_root = _runtime_config_root() config_root = _runtime_config_root()
cfg = reload_config(config_dir=config_root) cfg = reload_config()
storage = Store(config=cfg, suppress_debug=True) storage = Store(config=cfg, suppress_debug=True)
backends = storage.list_backends() or [] backends = storage.list_backends() or []

View File

@@ -13,10 +13,9 @@ from SYS.logger import log
from SYS.utils import expand_path from SYS.utils import expand_path
from SYS.database import db, get_config_all, save_config_value from SYS.database import db, get_config_all, save_config_value
DEFAULT_CONFIG_FILENAME = "config.conf"
SCRIPT_DIR = Path(__file__).resolve().parent SCRIPT_DIR = Path(__file__).resolve().parent
_CONFIG_CACHE: Dict[str, Dict[str, Any]] = {} _CONFIG_CACHE: Dict[str, Any] = {}
_CONFIG_SAVE_MAX_RETRIES = 5 _CONFIG_SAVE_MAX_RETRIES = 5
_CONFIG_SAVE_RETRY_DELAY = 0.15 _CONFIG_SAVE_RETRY_DELAY = 0.15
@@ -41,14 +40,8 @@ def global_config() -> List[Dict[str, Any]]:
def clear_config_cache() -> None: def clear_config_cache() -> None:
"""Clear the configuration cache.""" """Clear the configuration cache."""
_CONFIG_CACHE.clear() global _CONFIG_CACHE
_CONFIG_CACHE = {}
def _make_cache_key(config_dir: Optional[Path], filename: str, actual_path: Optional[Path]) -> str:
if actual_path:
return str(actual_path.resolve())
base_dir = config_dir or SCRIPT_DIR
return str((base_dir / filename).resolve())
def get_hydrus_instance( def get_hydrus_instance(
@@ -341,44 +334,26 @@ def resolve_debug_log(config: Dict[str, Any]) -> Optional[Path]:
return path return path
def load_config( def load_config() -> Dict[str, Any]:
config_dir: Optional[Path] = None, filename: str = DEFAULT_CONFIG_FILENAME global _CONFIG_CACHE
) -> Dict[str, Any]: if _CONFIG_CACHE:
base_dir = config_dir or SCRIPT_DIR return _CONFIG_CACHE
config_path = base_dir / filename
cache_key = _make_cache_key(config_dir, filename, config_path)
if cache_key in _CONFIG_CACHE:
return _CONFIG_CACHE[cache_key]
# Load from database # Load strictly from database
db_config = get_config_all() db_config = get_config_all()
if db_config: if db_config:
_CONFIG_CACHE[cache_key] = db_config _CONFIG_CACHE = db_config
return db_config return db_config
return {} return {}
def reload_config( def reload_config() -> Dict[str, Any]:
config_dir: Optional[Path] = None, filename: str = DEFAULT_CONFIG_FILENAME clear_config_cache()
) -> Dict[str, Any]: return load_config()
base_dir = config_dir or SCRIPT_DIR
config_path = base_dir / filename
cache_key = _make_cache_key(config_dir, filename, config_path)
_CONFIG_CACHE.pop(cache_key, None)
return load_config(config_dir=config_dir, filename=filename)
def save_config(config: Dict[str, Any]) -> int:
def save_config(
config: Dict[str, Any],
config_dir: Optional[Path] = None,
filename: str = DEFAULT_CONFIG_FILENAME,
) -> int:
base_dir = config_dir or SCRIPT_DIR
config_path = base_dir / filename
def _write_entries() -> int: def _write_entries() -> int:
count = 0 count = 0
with db.transaction(): with db.transaction():
@@ -409,26 +384,24 @@ def save_config(
while True: while True:
try: try:
saved_entries = _write_entries() saved_entries = _write_entries()
log(f"Saved {saved_entries} configuration entries to database.") log(f"Synced {saved_entries} entries to {db.db_path}")
break break
except sqlite3.OperationalError as exc: except sqlite3.OperationalError as exc:
attempts += 1 attempts += 1
locked_error = "locked" in str(exc).lower() locked_error = "locked" in str(exc).lower()
if not locked_error or attempts >= _CONFIG_SAVE_MAX_RETRIES: if not locked_error or attempts >= _CONFIG_SAVE_MAX_RETRIES:
log(f"CRITICAL: Failed to save config to database: {exc}") log(f"CRITICAL: Database write failed: {exc}")
raise raise
delay = _CONFIG_SAVE_RETRY_DELAY * attempts delay = _CONFIG_SAVE_RETRY_DELAY * attempts
log( log(f"Database locked; retry {attempts}/{_CONFIG_SAVE_MAX_RETRIES} in {delay:.2f}s")
f"Database busy locking medios.db (attempt {attempts}/{_CONFIG_SAVE_MAX_RETRIES}); retrying in {delay:.2f}s."
)
time.sleep(delay) time.sleep(delay)
except Exception as exc: except Exception as exc:
log(f"CRITICAL: Failed to save config to database: {exc}") log(f"CRITICAL: Configuration save failed: {exc}")
raise raise
cache_key = _make_cache_key(config_dir, filename, config_path)
clear_config_cache() clear_config_cache()
_CONFIG_CACHE[cache_key] = config global _CONFIG_CACHE
_CONFIG_CACHE = config
return saved_entries return saved_entries

View File

@@ -263,11 +263,6 @@ def save_config_value(category: str, subtype: str, item_name: str, key: str, val
def get_config_all() -> Dict[str, Any]: def get_config_all() -> Dict[str, Any]:
"""Retrieve all configuration from the database in the legacy dict format.""" """Retrieve all configuration from the database in the legacy dict format."""
try:
db.execute("DELETE FROM config WHERE category='store' AND LOWER(subtype) in ('folder', 'zerotier')")
db.execute("DELETE FROM config WHERE category='networking'")
except Exception:
pass
rows = db.fetchall("SELECT category, subtype, item_name, key, value FROM config") rows = db.fetchall("SELECT category, subtype, item_name, key, value FROM config")
config: Dict[str, Any] = {} config: Dict[str, Any] = {}