refactor(runtime): share home-dependent cache reset implementation

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-07 20:19:57 +08:00
parent d967457e69
commit 33c95086f6
3 changed files with 49 additions and 73 deletions

View File

@@ -0,0 +1,43 @@
"""Shared cache reset after selecting a runtime home.
Service imports remain lazy so CLI/bootstrap can select DEEPTUTOR_HOME first.
"""
from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
def reset_runtime_singletons() -> None:
"""Refresh home-dependent caches, reporting failures without skipping peers."""
try:
from deeptutor.services.path_service import PathService
PathService.reset_instance()
except Exception:
logger.exception(
"PathService reset failed; the runtime may keep writing to the "
"previous DEEPTUTOR_HOME instead of the selected one"
)
try:
from deeptutor.services.config.runtime_settings import RuntimeSettingsService
RuntimeSettingsService._instances.clear()
except Exception:
logger.warning(
"RuntimeSettings cache reset failed; settings cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)
try:
from deeptutor.services.config.model_catalog import ModelCatalogService
ModelCatalogService._instances.clear()
except Exception:
logger.warning(
"ModelCatalog cache reset failed; model catalog cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)

View File

@@ -141,41 +141,10 @@ def _log(message: str) -> None:
def _reset_runtime_singletons() -> None:
"""Make a just-selected DEEPTUTOR_HOME visible to path/config singletons.
"""Compatibility entry point for runtime-home selection."""
from deeptutor.runtime.cache_reset import reset_runtime_singletons
Same caveat as ``deeptutor_cli.init_cmd``: a reset that silently fails
leaves the previous home's cached paths in place, so failures are logged
instead of swallowed.
"""
try:
from deeptutor.services.path_service import PathService
PathService.reset_instance()
except Exception:
logger.exception(
"PathService reset failed; the runtime may keep writing to the "
"previous DEEPTUTOR_HOME instead of the selected one"
)
try:
from deeptutor.services.config.runtime_settings import RuntimeSettingsService
RuntimeSettingsService._instances.clear()
except Exception:
logger.warning(
"RuntimeSettings cache reset failed; settings cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)
try:
from deeptutor.services.config.model_catalog import ModelCatalogService
ModelCatalogService._instances.clear()
except Exception:
logger.warning(
"ModelCatalog cache reset failed; model catalog cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)
reset_runtime_singletons()
def _get_pgid(pid: int | None) -> int | None:

View File

@@ -10,7 +10,6 @@ intentionally thin so the order of steps is easy to read top-to-bottom.
from __future__ import annotations
import logging
from pathlib import Path
from typing import Any
@@ -21,47 +20,12 @@ from deeptutor.runtime.home import DEEPTUTOR_HOME_ENV, get_runtime_home
from . import init_wizard as wiz
logger = logging.getLogger(__name__)
def _reset_runtime_singletons() -> None:
"""Drop cached service instances so the new DEEPTUTOR_HOME takes effect.
"""Compatibility entry point for runtime-home selection."""
from deeptutor.runtime.cache_reset import reset_runtime_singletons
``deeptutor init`` may pass ``--home`` to target a different workspace; the
singletons cache paths from the *previous* PathService and will silently
write to the wrong place if not cleared. Failures are therefore logged
instead of swallowed: a silent reset failure means every later write still
lands in the old workspace with no trace of why.
"""
try:
from deeptutor.services.path_service import PathService
PathService.reset_instance()
except Exception:
logger.exception(
"PathService reset failed; later writes may keep going to the "
"previous DEEPTUTOR_HOME instead of the selected one"
)
try:
from deeptutor.services.config.runtime_settings import RuntimeSettingsService
RuntimeSettingsService._instances.clear()
except Exception:
logger.warning(
"RuntimeSettings cache reset failed; settings cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)
try:
from deeptutor.services.config.model_catalog import ModelCatalogService
ModelCatalogService._instances.clear()
except Exception:
logger.warning(
"ModelCatalog cache reset failed; model catalog cached for the "
"previous DEEPTUTOR_HOME may be reused",
exc_info=True,
)
reset_runtime_singletons()
def _ensure_model_service(catalog: dict, service_name: str, profile_id: str, model_id: str):