fix(douyu): unify binding state and scheduler health

This commit is contained in:
yml
2026-11-23 20:16:59 +08:00
parent 0a32f24fa8
commit e6629a9c91
20 changed files with 1336 additions and 158 deletions
+39 -2
View File
@@ -2,11 +2,16 @@
from __future__ import annotations
import json
from pathlib import Path
from web.backend import background_runtime
def test_background_services_start_and_stop_once(monkeypatch):
def test_background_services_start_and_stop_once(monkeypatch, tmp_path):
calls: list[str] = []
heartbeat_path = tmp_path / "background_runtime.json"
monkeypatch.setenv("BACKGROUND_RUNTIME_HEARTBEAT_PATH", str(heartbeat_path))
def mark(name: str):
return lambda *_args, **_kwargs: calls.append(name) or True
@@ -84,6 +89,7 @@ def test_background_services_start_and_stop_once(monkeypatch):
assert background_runtime.start_background_services() is False
background_runtime.stop_background_services()
background_runtime.stop_background_services()
assert not heartbeat_path.exists()
assert calls == [
"execution",
@@ -104,9 +110,13 @@ def test_background_services_start_and_stop_once(monkeypatch):
]
def test_execution_only_worker_can_disable_control_plane(monkeypatch):
def test_execution_only_worker_can_disable_control_plane(monkeypatch, tmp_path):
calls: list[str] = []
monkeypatch.setenv("BACKGROUND_CONTROL_PLANE_ENABLED", "false")
monkeypatch.setenv(
"BACKGROUND_RUNTIME_HEARTBEAT_PATH",
str(tmp_path / "background_runtime.json"),
)
monkeypatch.setattr(
"web.backend.services.execution_worker.start_execution_job_worker",
lambda: calls.append("start") or True,
@@ -124,3 +134,30 @@ def test_execution_only_worker_can_disable_control_plane(monkeypatch):
background_runtime.stop_background_services()
assert calls == ["start", "stop"]
def test_background_runtime_heartbeat_reports_scheduler_process(
monkeypatch,
tmp_path,
):
heartbeat_path = tmp_path / "background_runtime.json"
monkeypatch.setenv("BACKGROUND_RUNTIME_HEARTBEAT_PATH", str(heartbeat_path))
monkeypatch.setattr(
"web.backend.services.automation.scheduler.scheduler_status",
lambda: {
"enabled": True,
"running": True,
"tick_seconds": 5,
"job_interval_seconds": 300,
"registered": ["ck_relogin"],
},
)
background_runtime._write_runtime_heartbeat()
status = background_runtime.background_runtime_status()
payload = json.loads(Path(heartbeat_path).read_text(encoding="utf-8"))
assert status["running"] is True
assert status["pid"] == payload["pid"]
assert status["scheduler"]["running"] is True
assert status["ageSeconds"] is not None