This commit is contained in:
2026-07-19 18:57:43 +02:00
parent 48bb6c2ec2
commit c53e2a3319
179 changed files with 9307 additions and 897 deletions
+20 -6
View File
@@ -123,7 +123,8 @@ class BaseService(ABC):
details = ""
DEFAULT_LOG_SIZE = 20
TICK_SECONDS = 1
PERSIST_SECONDS = 3
PERSIST_SECONDS = 8
METRICS_SECONDS = 15
STALE_SECONDS = 15
def __init__(self, name: str, interval_seconds: int = 3600):
@@ -144,6 +145,9 @@ class BaseService(ABC):
self._run_pending = False
self._last_command = None
self._last_persist = None
self._last_metrics = None
self._metrics_snapshot = {}
self._state_row_id = None
self.enabled_field = ConfigField(
self.enabled_key,
"Enabled",
@@ -234,6 +238,14 @@ class BaseService(ABC):
logger.warning(f"Could not collect metrics for {self.name}: {e}")
return {}
def _current_metrics(self, now, force: bool = False) -> dict:
if not force and self._last_metrics is not None:
if (now - self._last_metrics).total_seconds() < self.METRICS_SECONDS:
return self._metrics_snapshot
self._last_metrics = now
self._metrics_snapshot = self._safe_metrics()
return self._metrics_snapshot
def start_supervisor(self) -> None:
if self._task is not None:
return
@@ -344,16 +356,18 @@ class BaseService(ABC):
"started_at": self._started_at.isoformat() if self._started_at else "",
"heartbeat": now.isoformat(),
"logs": json.dumps(list(self.log_buffer)),
"metrics": json.dumps(self._safe_metrics()),
"metrics": json.dumps(self._current_metrics(now, force=force)),
"updated_at": now.isoformat(),
}
try:
table = get_table("service_state")
existing = table.find_one(name=self.name)
if existing:
table.update({**record, "id": existing["id"]}, ["id"])
if self._state_row_id is None:
existing = table.find_one(name=self.name)
self._state_row_id = existing["id"] if existing else None
if self._state_row_id is not None:
table.update({**record, "id": self._state_row_id}, ["id"])
else:
table.insert(record)
self._state_row_id = table.insert(record)
except Exception as e:
logger.warning(f"Could not persist state for {self.name}: {e}")