Files
boss_dp/server/api/workers.py
2026-02-26 21:21:47 +08:00

76 lines
2.5 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 rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from server.core.response import api_success, api_error
from server.core.worker_manager import worker_manager
@api_view(["GET"])
@permission_classes([AllowAny])
def health_check(request):
"""健康检查。"""
online = len([w for w in worker_manager.get_all_workers() if w.online])
return api_success({"status": "ok", "workers_online": online})
def _worker_to_dict(w) -> dict:
return {
"worker_id": w.worker_id,
"worker_name": w.worker_name,
"browsers": [b.model_dump() for b in w.browsers],
"online": w.online,
"current_task_id": w.current_task_id,
}
@api_view(["GET"])
def worker_list(request):
"""获取所有已注册的 Worker含在线状态与浏览器列表"""
workers = worker_manager.get_all_workers()
return api_success([_worker_to_dict(w) for w in workers])
@api_view(["GET"])
def worker_detail(request, worker_id):
"""获取指定 Worker 的详情。"""
w = worker_manager.get_worker(worker_id)
if not w:
return api_error(status.HTTP_404_NOT_FOUND, f"Worker {worker_id} 不存在")
return api_success(_worker_to_dict(w))
def _worker_browsers_response(worker_id: str):
"""根据 worker_id 返回比特浏览器环境列表。"""
w = worker_manager.get_worker(worker_id)
if not w:
return api_error(status.HTTP_404_NOT_FOUND, f"Worker {worker_id} 不存在", data=None)
browsers = [
{"id": b.id, "name": b.name or "", "remark": b.remark or ""}
for b in w.browsers
]
return api_success(data=browsers, msg="获取成功")
@api_view(["POST"])
def worker_browsers(request):
"""获取指定电脑Worker中比特浏览器的环境名称列表。worker_id 可从 form-data 或 query 传入。"""
worker_id = (
request.query_params.get("worker_id")
or request.data.get("worker_id")
or request.POST.get("worker_id")
)
if not worker_id:
return api_error(status.HTTP_400_BAD_REQUEST, "请提供 worker_id 参数", data=None)
return _worker_browsers_response(str(worker_id))
@api_view(["GET"])
def worker_browsers_by_path(request, worker_id):
"""获取指定电脑Worker中比特浏览器的环境名称列表。worker_id 在路径中。"""
return _worker_browsers_response(worker_id)