feat: compact durable logs and refine framework routing
This commit is contained in:
@@ -10,6 +10,7 @@ from common import parse_datetime, read_json, utc_now, write_json
|
||||
|
||||
OFFICIAL_CAPABILITY_VERSION = 1
|
||||
DEFAULT_OFFICIAL_CAPABILITIES_PATH = Path(".modelhub_state/official_capabilities.json")
|
||||
DEFAULT_MODEL_GPU_CACHE_LIMIT = 1500
|
||||
|
||||
|
||||
class OfficialCapabilityUnavailable(RuntimeError):
|
||||
@@ -61,6 +62,18 @@ class OfficialCapabilityRegistry:
|
||||
self.pause_reason: str | None = None
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@staticmethod
|
||||
def _prune_model_gpu_cache(state: dict[str, Any]) -> None:
|
||||
cache = state.get("modelGpuTaskTypes")
|
||||
if not isinstance(cache, dict) or len(cache) <= DEFAULT_MODEL_GPU_CACHE_LIMIT:
|
||||
return
|
||||
ordered = sorted(
|
||||
cache.items(),
|
||||
key=lambda pair: parse_datetime((pair[1] or {}).get("updatedAt")) or datetime.min.replace(tzinfo=utc_now().tzinfo),
|
||||
reverse=True,
|
||||
)
|
||||
state["modelGpuTaskTypes"] = dict(ordered[:DEFAULT_MODEL_GPU_CACHE_LIMIT])
|
||||
|
||||
def _load(self) -> dict[str, Any]:
|
||||
try:
|
||||
value = read_json(self.path)
|
||||
@@ -73,6 +86,7 @@ class OfficialCapabilityRegistry:
|
||||
def prepare(self, client: Any, *, fallback_gpus: list[str], task_types: list[str], now: datetime | None = None) -> dict[str, Any]:
|
||||
now = now or utc_now()
|
||||
state = self._load()
|
||||
self._prune_model_gpu_cache(state)
|
||||
catalog_usable = _fresh(state.get("catalogUpdatedAt"), now, 1800)
|
||||
task_tree_usable = _fresh(state.get("taskTreeUpdatedAt"), now, 7 * 86400)
|
||||
errors: list[str] = []
|
||||
@@ -159,6 +173,7 @@ class OfficialCapabilityRegistry:
|
||||
with self._lock:
|
||||
cache = self.state.setdefault("modelGpuTaskTypes", {})
|
||||
cache[key] = {"updatedAt": now.isoformat(), "taskTypes": task_types}
|
||||
self._prune_model_gpu_cache(self.state)
|
||||
self.state["generatedAt"] = now.isoformat()
|
||||
write_json(self.path, self.state)
|
||||
return task_types
|
||||
|
||||
Reference in New Issue
Block a user