"""Document Server diagnostic tools — scheduler and queue status.""" from __future__ import annotations import json import re from datetime import datetime, timezone from ..config import HOSTS from ..schemas import BaseResult from .ssh import run_command, run_local, SSHError, _is_local_host def _now() -> str: return datetime.now(timezone.utc).isoformat() async def scheduler_status() -> dict: """Get APScheduler job status from Document Server logs.""" cfg = HOSTS["gpu"] cmd = "docker logs hyungi_document_server-fastapi-1 --tail 100 2>&1 | grep -iE 'scheduler|apscheduler|job|trigger|cron|interval' | tail -20" try: if _is_local_host(cfg): stdout, _ = await run_local(cmd) else: stdout, _ = await run_command(cfg, cmd) except SSHError as e: return {"ok": False, "checked_at": _now(), "error_type": e.error_type, "error": str(e), "data": [], "summary": ""} lines = [l.strip() for l in stdout.strip().splitlines() if l.strip()] return { "ok": True, "checked_at": _now(), "data": lines, "summary": f"최근 스케줄러 로그 {len(lines)}줄", "error": "", } async def queue_status() -> dict: """Get document processing queue status from Document Server logs.""" cfg = HOSTS["gpu"] cmd = "docker logs hyungi_document_server-fastapi-1 --tail 200 2>&1 | grep -iE 'queue_consumer|pending|processing|completed|stale|batch' | tail -20" try: if _is_local_host(cfg): stdout, _ = await run_local(cmd) else: stdout, _ = await run_command(cfg, cmd) except SSHError as e: return {"ok": False, "checked_at": _now(), "error_type": e.error_type, "error": str(e), "data": [], "summary": ""} lines = [l.strip() for l in stdout.strip().splitlines() if l.strip()] return { "ok": True, "checked_at": _now(), "data": lines, "summary": f"최근 큐 로그 {len(lines)}줄", "error": "", }