fix: auto-project mainland detect sync
This commit is contained in:
@@ -6,12 +6,32 @@ import time
|
||||
from app.core.config import settings
|
||||
from app.services.debug_event_service import push_debug_event
|
||||
from app.services.detect_job_service import get_active_detect_job_summary, get_detect_queue_health, list_recent_detect_run_events
|
||||
from app.services.sync_record_service import append_detect_result_projection_if_changed
|
||||
from app.services.sync_push_service import pull_detect_task_batch_now, push_runtime_projection_now
|
||||
|
||||
|
||||
logger = logging.getLogger("domaincheck.sync_agent")
|
||||
|
||||
|
||||
def _append_detect_result_projection_snapshot(active_job: dict) -> None:
|
||||
if not active_job:
|
||||
return
|
||||
append_detect_result_projection_if_changed(
|
||||
detect={
|
||||
"active_job": active_job,
|
||||
"progress": {
|
||||
"pending": int(active_job.get("items_pending", 0) or 0),
|
||||
"running": int(active_job.get("items_running", 0) or 0),
|
||||
"completed": int(active_job.get("items_completed", 0) or 0),
|
||||
"blacklisted": int(active_job.get("items_blacklisted", 0) or 0),
|
||||
"failed": int(active_job.get("items_failed", 0) or 0),
|
||||
},
|
||||
"phase_label": str(active_job.get("status") or "").strip(),
|
||||
"phase_detail": f"sync-agent snapshot for {active_job.get('job_code', '')}",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _emit_structured_tick(
|
||||
*,
|
||||
base_event_type: str,
|
||||
@@ -91,6 +111,9 @@ def main() -> None:
|
||||
)
|
||||
while True:
|
||||
try:
|
||||
active_job = get_active_detect_job_summary(event_limit=10)
|
||||
if active_job:
|
||||
_append_detect_result_projection_snapshot(active_job)
|
||||
ok, message, data = push_runtime_projection_now()
|
||||
logger.info("sync tick: ok=%s message=%s data=%s", ok, message, data)
|
||||
push_debug_event(
|
||||
@@ -112,7 +135,6 @@ def main() -> None:
|
||||
payload={"ok": pull_ok, "data": pull_data},
|
||||
)
|
||||
_emit_structured_tick(base_event_type="task_pull", ok=pull_ok, message=pull_message, data=pull_data)
|
||||
active_job = get_active_detect_job_summary(event_limit=10)
|
||||
if active_job:
|
||||
queue_health = get_detect_queue_health(window_minutes=15)
|
||||
recent_events = list_recent_detect_run_events(limit=8)
|
||||
|
||||
35
domain-api/tests/test_sync_agent.py
Normal file
35
domain-api/tests/test_sync_agent.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.sync_agent import _append_detect_result_projection_snapshot
|
||||
|
||||
|
||||
class SyncAgentTests(unittest.TestCase):
|
||||
@patch("app.sync_agent.append_detect_result_projection_if_changed")
|
||||
def test_append_detect_result_projection_snapshot_uses_active_job_summary(self, mock_append) -> None:
|
||||
_append_detect_result_projection_snapshot(
|
||||
{
|
||||
"job_id": 1,
|
||||
"job_code": "detect-20260419030000-abc123",
|
||||
"status": "running",
|
||||
"items_pending": 12,
|
||||
"items_running": 3,
|
||||
"items_completed": 8,
|
||||
"items_blacklisted": 1,
|
||||
"items_failed": 2,
|
||||
}
|
||||
)
|
||||
|
||||
mock_append.assert_called_once()
|
||||
payload = mock_append.call_args.kwargs["detect"]
|
||||
self.assertEqual("running", payload["phase_label"])
|
||||
self.assertEqual("detect-20260419030000-abc123", payload["active_job"]["job_code"])
|
||||
self.assertEqual(12, payload["progress"]["pending"])
|
||||
self.assertEqual(3, payload["progress"]["running"])
|
||||
self.assertEqual(8, payload["progress"]["completed"])
|
||||
self.assertEqual(1, payload["progress"]["blacklisted"])
|
||||
self.assertEqual(2, payload["progress"]["failed"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user