f
This commit is contained in:
115
cmdnat/worker.py
115
cmdnat/worker.py
@@ -12,7 +12,7 @@ from cmdlet import register
|
||||
from cmdlet._shared import Cmdlet, CmdletArg
|
||||
from SYS import pipeline as ctx
|
||||
from SYS.logger import log
|
||||
from SYS.config import get_local_storage_path
|
||||
from SYS.database import db as _db, get_worker_stdout
|
||||
|
||||
DEFAULT_LIMIT = 100
|
||||
WORKER_STATUS_FILTERS = {"running",
|
||||
@@ -74,6 +74,69 @@ CMDLET = Cmdlet(
|
||||
)
|
||||
|
||||
|
||||
def _normalize_worker_row(row: Dict[str, Any]) -> Dict[str, Any]:
|
||||
worker_id = row.get("id")
|
||||
created = row.get("created_at") or ""
|
||||
updated = row.get("updated_at") or ""
|
||||
payload = dict(row)
|
||||
payload["worker_id"] = worker_id
|
||||
payload["started_at"] = created
|
||||
payload["last_updated"] = updated
|
||||
payload["completed_at"] = updated
|
||||
payload["pipe"] = row.get("details") or row.get("title") or ""
|
||||
return payload
|
||||
|
||||
|
||||
class _WorkerDB:
|
||||
def clear_finished_workers(self) -> int:
|
||||
try:
|
||||
cur = _db.execute("DELETE FROM workers WHERE status != 'running'")
|
||||
return int(getattr(cur, "rowcount", 0) or 0)
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
def get_worker(self, worker_id: str) -> Dict[str, Any] | None:
|
||||
row = _db.fetchone("SELECT * FROM workers WHERE id = ?", (worker_id,))
|
||||
if not row:
|
||||
return None
|
||||
worker = _normalize_worker_row(dict(row))
|
||||
try:
|
||||
worker["stdout"] = get_worker_stdout(worker_id)
|
||||
except Exception:
|
||||
worker["stdout"] = ""
|
||||
return worker
|
||||
|
||||
def get_worker_events(self, worker_id: str) -> List[Dict[str, Any]]:
|
||||
try:
|
||||
rows = _db.fetchall(
|
||||
"SELECT content, channel, timestamp FROM worker_stdout WHERE worker_id = ? ORDER BY timestamp ASC",
|
||||
(worker_id,),
|
||||
)
|
||||
except Exception:
|
||||
rows = []
|
||||
events: List[Dict[str, Any]] = []
|
||||
for row in rows:
|
||||
try:
|
||||
events.append({
|
||||
"message": row.get("content"),
|
||||
"channel": row.get("channel") or "stdout",
|
||||
"created_at": row.get("timestamp"),
|
||||
})
|
||||
except Exception:
|
||||
continue
|
||||
return events
|
||||
|
||||
def get_all_workers(self, limit: int = 100) -> List[Dict[str, Any]]:
|
||||
try:
|
||||
rows = _db.fetchall(
|
||||
"SELECT * FROM workers ORDER BY created_at DESC LIMIT ?",
|
||||
(int(limit or 100),),
|
||||
)
|
||||
except Exception:
|
||||
rows = []
|
||||
return [_normalize_worker_row(dict(row)) for row in rows]
|
||||
|
||||
|
||||
def _has_help_flag(args_list: Sequence[str]) -> bool:
|
||||
return any(str(arg).lower() in HELP_FLAGS for arg in args_list)
|
||||
|
||||
@@ -101,39 +164,33 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
|
||||
options = _parse_worker_args(args_list)
|
||||
|
||||
library_root = get_local_storage_path(config or {})
|
||||
if not library_root:
|
||||
log("No library root configured. Use the .config command to set up storage.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
try:
|
||||
from API.folder import API_folder_store
|
||||
db = _WorkerDB()
|
||||
|
||||
with API_folder_store(library_root) as db:
|
||||
if options.clear:
|
||||
count = db.clear_finished_workers()
|
||||
log(f"Cleared {count} finished workers.")
|
||||
if options.clear:
|
||||
count = db.clear_finished_workers()
|
||||
log(f"Cleared {count} finished workers.")
|
||||
return 0
|
||||
|
||||
if options.worker_id:
|
||||
worker = db.get_worker(options.worker_id)
|
||||
if worker:
|
||||
events: List[Dict[str, Any]] = []
|
||||
try:
|
||||
wid = worker.get("worker_id")
|
||||
if wid:
|
||||
events = db.get_worker_events(wid)
|
||||
except Exception:
|
||||
pass
|
||||
_emit_worker_detail(worker, events)
|
||||
return 0
|
||||
log(f"Worker not found: {options.worker_id}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if options.worker_id:
|
||||
worker = db.get_worker(options.worker_id)
|
||||
if worker:
|
||||
events: List[Dict[str, Any]] = []
|
||||
try:
|
||||
wid = worker.get("worker_id")
|
||||
if wid and hasattr(db, "get_worker_events"):
|
||||
events = db.get_worker_events(wid)
|
||||
except Exception:
|
||||
pass
|
||||
_emit_worker_detail(worker, events)
|
||||
return 0
|
||||
log(f"Worker not found: {options.worker_id}", file=sys.stderr)
|
||||
return 1
|
||||
if selection_requested:
|
||||
return _render_worker_selection(db, result)
|
||||
|
||||
if selection_requested:
|
||||
return _render_worker_selection(db, result)
|
||||
|
||||
return _render_worker_list(db, options.status, options.limit)
|
||||
return _render_worker_list(db, options.status, options.limit)
|
||||
except Exception as exc:
|
||||
log(f"Workers query failed: {exc}", file=sys.stderr)
|
||||
import traceback
|
||||
|
||||
Reference in New Issue
Block a user