update refactoring and add new features
This commit is contained in:
+8
-4
@@ -26,12 +26,16 @@ SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
_SAVE_LOCK_DIRNAME = ".medios_save_lock"
|
||||
_SAVE_LOCK_TIMEOUT = 30.0 # seconds to wait for save lock
|
||||
_SAVE_LOCK_STALE_SECONDS = 3600 # consider lock stale after 1 hour
|
||||
_SAVE_LOCK_POLL_INTERVAL = 0.1 # seconds between lock acquisition attempts
|
||||
|
||||
_WAL_CHECKPOINT_TIMEOUT = 5.0 # seconds for WAL checkpoint connection
|
||||
|
||||
_CONFIG_CACHE: Dict[str, Any] = {}
|
||||
_LAST_SAVED_CONFIG: Dict[str, Any] = {}
|
||||
_CONFIG_SUMMARY_PENDING = False
|
||||
_CONFIG_SAVE_MAX_RETRIES = 5
|
||||
_CONFIG_SAVE_RETRY_DELAY = 0.15
|
||||
_CONFIG_SAVE_VERIFY_RETRIES = 3
|
||||
_CONFIG_MISSING = object()
|
||||
_PATH_ALIAS_TOKEN_RE = re.compile(r"^\$(?:\((?P<braced>[^)]+)\)|(?P<plain>[A-Za-z0-9_.-]+))$")
|
||||
|
||||
@@ -890,7 +894,7 @@ def _acquire_save_lock(timeout: float = _SAVE_LOCK_TIMEOUT):
|
||||
logger.exception("Failed to inspect save lock directory %s: %s", lock_dir, exc)
|
||||
if time.time() - start > timeout:
|
||||
raise ConfigSaveConflict("Save lock busy; could not acquire in time")
|
||||
time.sleep(0.1)
|
||||
time.sleep(_SAVE_LOCK_POLL_INTERVAL)
|
||||
|
||||
|
||||
def _release_save_lock(lock_dir: Path) -> None:
|
||||
@@ -1030,10 +1034,10 @@ def save_config(config: Dict[str, Any]) -> int:
|
||||
# we don't contend with our main connection lock or active transactions.
|
||||
try:
|
||||
try:
|
||||
with sqlite3.connect(str(db.db_path), timeout=5.0) as _con:
|
||||
with sqlite3.connect(str(db.db_path), timeout=_WAL_CHECKPOINT_TIMEOUT) as _con:
|
||||
_con.execute("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
except Exception:
|
||||
with sqlite3.connect(str(db.db_path), timeout=5.0) as _con:
|
||||
with sqlite3.connect(str(db.db_path), timeout=_WAL_CHECKPOINT_TIMEOUT) as _con:
|
||||
_con.execute("PRAGMA wal_checkpoint")
|
||||
except Exception as exc:
|
||||
log(f"Warning: WAL checkpoint failed: {exc}")
|
||||
@@ -1084,7 +1088,7 @@ def save(config: Dict[str, Any]) -> int:
|
||||
return save_config(config)
|
||||
|
||||
|
||||
def save_config_and_verify(config: Dict[str, Any], retries: int = 3, delay: float = 0.15) -> int:
|
||||
def save_config_and_verify(config: Dict[str, Any], retries: int = _CONFIG_SAVE_VERIFY_RETRIES, delay: float = _CONFIG_SAVE_RETRY_DELAY) -> int:
|
||||
"""Save configuration and verify crucial keys persisted to disk.
|
||||
|
||||
This helper performs a best-effort verification loop that reloads the
|
||||
|
||||
Reference in New Issue
Block a user