fix: dispatch detect start to mainland nodes

This commit is contained in:
Your Name
2026-04-19 02:18:42 +08:00
parent b9c29481b5
commit 1adfeed1ab
6 changed files with 297 additions and 5 deletions

View File

@@ -16,6 +16,9 @@ STRUCTURED_ACTIONS = {
"diagnostics.collect",
"runtime.start_worker",
"runtime.stop_worker",
"runtime.start_detection",
"runtime.stop_detection",
"runtime.pull_tasks",
"runtime.restart_api",
"runtime.start_sync_agent",
"runtime.stop_sync_agent",
@@ -186,6 +189,19 @@ def execute_structured_action(
result.update(host_details)
return int(code or 0) == 0, stdout or stderr or f"{service_name} status collected", result
if normalized_action in {"runtime.start_detection", "runtime.stop_detection", "runtime.pull_tasks"}:
from app.services.runtime_control_service import runtime_action
runtime_action_name = normalized_action.split(".", 1)[1]
ok, message, result = runtime_action(runtime_action_name, normalized_payload)
result = {
**dict(result or {}),
"runtime_action": runtime_action_name,
"payload": normalized_payload,
}
result.update(host_details)
return ok, message, result
systemctl_action = systemctl_action_name(normalized_action)
if systemctl_action:
service_name = service_name_for_action(normalized_action, normalized_payload, normalized_service_names)

View File

@@ -97,6 +97,8 @@ ALTER TABLE ops_jobs ADD COLUMN IF NOT EXISTS metadata_json JSONB;
_LOCAL_RUNTIME_ACTIONS = {
"runtime.start_worker": "start_worker",
"runtime.stop_worker": "stop_worker",
"runtime.start_detection": "start_detection",
"runtime.stop_detection": "stop_detection",
"runtime.restart_api": "restart_api",
"runtime.start_sync_agent": "start_sync_agent",
"runtime.stop_sync_agent": "stop_sync_agent",

View File

@@ -25,6 +25,9 @@ _SSH_REMOTE_TIMEOUT_SECONDS = {
"diagnostics.collect": 90,
"runtime.start_worker": 45,
"runtime.stop_worker": 45,
"runtime.start_detection": 45,
"runtime.stop_detection": 45,
"runtime.pull_tasks": 60,
"runtime.restart_api": 45,
"runtime.start_sync_agent": 45,
"runtime.stop_sync_agent": 45,
@@ -231,6 +234,18 @@ def candidate_runtime_logs_dirs():
return dirs
def runtime_module_roots():
roots = []
for candidate in (
Path.cwd() / "domain-api",
Path("/opt/domaincheck/domain-api"),
Path("/www/wwwroot/getDomain/domain-api"),
):
if candidate not in roots:
roots.append(candidate)
return roots
def service_log_file_candidates(service_name):
normalized_service_name = str(service_name or "").strip()
if not normalized_service_name:
@@ -297,6 +312,29 @@ def systemctl_action_name(action):
return ""
def execute_runtime_action(action, payload):
runtime_action_name = str(action or "").strip().split(".", 1)[-1]
for root in runtime_module_roots():
app_dir = root / "app"
if not app_dir.exists():
continue
root_text = str(root)
if root_text not in sys.path:
sys.path.insert(0, root_text)
try:
from app.services.runtime_control_service import runtime_action
except Exception:
continue
ok, message, result = runtime_action(runtime_action_name, dict(payload or {{}}))
normalized_result = dict(result or {{}})
normalized_result["runtime_action"] = runtime_action_name
normalized_result["payload"] = dict(payload or {{}})
normalized_result["executor"] = "ssh"
normalized_result["runtime_root"] = root_text
return ok, message, normalized_result
return False, f"runtime action import failed: {{runtime_action_name}}", {{"action": action, "executor": "ssh"}}
def execute(action, payload):
if action == "health.snapshot":
checks = {{}}
@@ -320,6 +358,9 @@ def execute(action, payload):
}}
return int(code or 0) == 0, stdout or stderr or f"{{service_name}} status collected", result
if action in ("runtime.start_detection", "runtime.stop_detection", "runtime.pull_tasks"):
return execute_runtime_action(action, payload)
systemctl_action = systemctl_action_name(action)
if systemctl_action:
service_name = service_name_for_action(action, payload)

View File

@@ -8,7 +8,7 @@ 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, normalize_systemctl_error, start_worker, stop_worker
from app.services.worker_control_service import _run_systemctl, normalize_systemctl_error, send_worker_command, start_worker, stop_worker
def _workspace_root() -> Path:
@@ -134,8 +134,9 @@ def stop_sync_agent() -> tuple[bool, str]:
return _run_systemd_action(service_name, "stop")
def runtime_action(action: str) -> tuple[bool, str, dict]:
def runtime_action(action: str, payload: dict | None = None) -> tuple[bool, str, dict]:
normalized_action = str(action or "").strip().lower().replace("-", "_")
normalized_payload = dict(payload or {})
if normalized_action != "push_debug_probe":
_emit_runtime_action_event(
normalized_action,
@@ -174,10 +175,42 @@ def runtime_action(action: str) -> tuple[bool, str, dict]:
_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()
pull_limit = normalized_payload.get("limit")
ok, message, data = pull_detect_task_batch_now(limit=int(pull_limit or 0) or None)
result = _build_runtime_action_result(action=normalized_action, poll_after_seconds=2, refresh_runtime=True, ok=ok, message=message, data=data)
_emit_runtime_action_event(normalized_action, stage="finished", ok=ok, message=message, data=result)
return ok, message, result
if normalized_action == "start_detection":
command_ok, command_message = send_worker_command(
"start_detection",
payload={
key: value
for key, value in normalized_payload.items()
if value not in (None, "")
},
)
result = _build_runtime_action_result(
action=normalized_action,
poll_after_seconds=2,
refresh_runtime=True,
ok=command_ok,
message=command_message,
data={"payload": normalized_payload},
)
_emit_runtime_action_event(normalized_action, stage="finished", ok=command_ok, message=command_message, data=result)
return command_ok, command_message, result
if normalized_action == "stop_detection":
command_ok, command_message = send_worker_command("stop_detection")
result = _build_runtime_action_result(
action=normalized_action,
poll_after_seconds=2,
refresh_runtime=True,
ok=command_ok,
message=command_message,
data={},
)
_emit_runtime_action_event(normalized_action, stage="finished", ok=command_ok, message=command_message, data=result)
return command_ok, command_message, result
if normalized_action == "push_debug_probe":
ok, message, data = push_debug_event(
service="domain-api",