Files
boss_dp/server/api/workers.py
2026-02-12 16:27:43 +08:00

43 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
Worker 查询 API。
"""
from fastapi import APIRouter, HTTPException
from typing import List
from server.models import WorkerOut
from server.core.worker_manager import worker_manager
router = APIRouter(prefix="/api/workers", tags=["workers"])
@router.get("", response_model=List[WorkerOut])
async def list_workers():
"""获取所有已注册的 Worker含在线状态与浏览器列表"""
workers = worker_manager.get_all_workers()
return [
WorkerOut(
worker_id=w.worker_id,
worker_name=w.worker_name,
browsers=w.browsers,
online=w.online,
current_task_id=w.current_task_id,
)
for w in workers
]
@router.get("/{worker_id}", response_model=WorkerOut)
async def get_worker(worker_id: str):
"""获取指定 Worker 的详情。"""
w = worker_manager.get_worker(worker_id)
if not w:
raise HTTPException(status_code=404, detail=f"Worker {worker_id} 不存在")
return WorkerOut(
worker_id=w.worker_id,
worker_name=w.worker_name,
browsers=w.browsers,
online=w.online,
current_task_id=w.current_task_id,
)