Add selftest scheduler, queue history, and OpenWrt signal stats
This commit is contained in:
@@ -16,10 +16,12 @@ _stats: dict[str, Any] = runtime_state.get("queue_stats", {}) or {
|
||||
"last_label": "",
|
||||
"last_finished_at": 0.0,
|
||||
}
|
||||
_history: deque[dict[str, Any]] = deque(runtime_state.get("queue_history", []) or [], maxlen=50)
|
||||
|
||||
|
||||
def _save_stats():
|
||||
runtime_state.set_state("queue_stats", _stats)
|
||||
runtime_state.set_state("queue_history", list(_history))
|
||||
|
||||
|
||||
async def enqueue(label: str, job: Callable[[], Awaitable[None]]) -> int:
|
||||
@@ -43,8 +45,11 @@ async def worker():
|
||||
pass
|
||||
_current_label = label
|
||||
_current_meta = {"enqueued_at": enqueued_at, "started_at": time.time()}
|
||||
status = "ok"
|
||||
try:
|
||||
await job()
|
||||
except Exception:
|
||||
status = "err"
|
||||
finally:
|
||||
finished_at = time.time()
|
||||
if _current_meta:
|
||||
@@ -60,6 +65,15 @@ async def worker():
|
||||
) / _stats["processed"]
|
||||
_stats["last_label"] = label
|
||||
_stats["last_finished_at"] = finished_at
|
||||
_history.appendleft(
|
||||
{
|
||||
"label": label,
|
||||
"wait_sec": int(wait_sec),
|
||||
"runtime_sec": int(runtime_sec),
|
||||
"finished_at": int(finished_at),
|
||||
"status": status,
|
||||
}
|
||||
)
|
||||
_save_stats()
|
||||
_current_label = None
|
||||
_current_meta = None
|
||||
@@ -111,4 +125,13 @@ def format_details(limit: int = 10) -> str:
|
||||
last_label = _stats.get("last_label")
|
||||
if last_label:
|
||||
lines.append(f"Last: {last_label}")
|
||||
if _history:
|
||||
lines.append("")
|
||||
lines.append("🗂 Last jobs:")
|
||||
for item in list(_history)[:5]:
|
||||
t = time.strftime("%H:%M:%S", time.localtime(item["finished_at"]))
|
||||
lines.append(
|
||||
f"- {t} {item['label']} {item['status']} "
|
||||
f"(wait {item['wait_sec']}s, run {item['runtime_sec']}s)"
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
Reference in New Issue
Block a user