This commit is contained in:
Your Name
2026-04-17 14:07:50 +08:00
parent dc34a4e294
commit 1921318c25
11 changed files with 2415 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ import subprocess
from pathlib import Path
from app.core.config import settings
from app.services.debug_event_service import push_debug_event
from app.services.sync_push_service import pull_detect_task_batch_now, push_runtime_projection_now
from app.services.runtime_settings_service import get_runtime_settings
from app.services.worker_control_service import _run_systemctl, start_worker, stop_worker
@@ -62,6 +63,23 @@ def _run_systemd_action(service_name: str, action: str, *, no_block: bool = Fals
return True, f"{service_name} {action} 命令已发送"
def _emit_runtime_action_event(action: str, *, stage: str, ok: bool, message: str, data: dict | None = None) -> None:
try:
push_debug_event(
service="runtime-control",
event_type=f"runtime_action_{stage}",
level="info" if ok else "warning",
message=message,
payload={
"action": action,
"ok": ok,
**(data or {}),
},
)
except Exception:
pass
def start_sync_agent() -> tuple[bool, str]:
runtime = get_runtime_settings()
worker_mode = runtime.get("worker_mode", settings.worker_mode)
@@ -81,39 +99,79 @@ def stop_sync_agent() -> tuple[bool, str]:
def runtime_action(action: str) -> tuple[bool, str, dict]:
if action == "start_worker":
normalized_action = str(action or "").strip().lower().replace("-", "_")
if normalized_action != "push_debug_probe":
_emit_runtime_action_event(
normalized_action,
stage="requested",
ok=True,
message=f"收到运行时动作请求: {normalized_action}",
)
if normalized_action == "start_worker":
ok, message = start_worker()
return ok, message, {"action": action, "poll_after_seconds": 2, "refresh_runtime": True}
if action == "stop_worker":
result = {"action": normalized_action, "poll_after_seconds": 2, "refresh_runtime": True}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "stop_worker":
ok, message = stop_worker()
return ok, message, {"action": action, "poll_after_seconds": 2, "refresh_runtime": True}
if action == "restart_api":
result = {"action": normalized_action, "poll_after_seconds": 2, "refresh_runtime": True}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "restart_api":
ok, message = restart_api()
return ok, message, {"action": action, "poll_after_seconds": 4, "refresh_runtime": True}
if action == "start_sync_agent":
result = {"action": normalized_action, "poll_after_seconds": 4, "refresh_runtime": True}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "start_sync_agent":
ok, message = start_sync_agent()
return ok, message, {"action": action, "poll_after_seconds": 2, "refresh_runtime": True}
if action == "stop_sync_agent":
result = {"action": normalized_action, "poll_after_seconds": 2, "refresh_runtime": True}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "stop_sync_agent":
ok, message = stop_sync_agent()
return ok, message, {"action": action, "poll_after_seconds": 2, "refresh_runtime": True}
if action == "push_sync":
result = {"action": normalized_action, "poll_after_seconds": 2, "refresh_runtime": True}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "push_sync":
ok, message, data = push_runtime_projection_now()
return ok, message, {
"action": action,
result = {
"action": normalized_action,
"poll_after_seconds": 2,
"refresh_runtime": True,
**(data or {}),
}
if action == "pull_tasks":
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "pull_tasks":
ok, message, data = pull_detect_task_batch_now()
return ok, message, {
"action": action,
result = {
"action": normalized_action,
"poll_after_seconds": 2,
"refresh_runtime": True,
**(data or {}),
}
return False, f"不支持的运行时动作: {action}", {
"action": action,
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "push_debug_probe":
ok, message, data = push_debug_event(
service="domain-api",
event_type="debug_probe",
level="info",
message="manual debug probe",
payload={"node_code": settings.node_code, "node_region": settings.node_region},
)
result = {
"action": normalized_action,
"poll_after_seconds": 1,
"refresh_runtime": False,
**(data or {}),
}
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
result = {
"action": normalized_action,
"poll_after_seconds": 0,
"refresh_runtime": False,
}
_emit_runtime_action_event(normalized_action, stage="finished", ok=False, message=f"不支持的运行时动作: {action}", data=result)
return False, f"不支持的运行时动作: {action}", result