refactor: 重构项目目录结构与代码组织
1. 重构controlplane与webhost后端的包结构,按领域拆分模块到core/apis子包 2. 移动并整理工具脚本到tools子目录,统一管理运维脚本 3. 重构插件运行时、调度、作业管理等核心模块到core子包 4. 更新AGENTS.md文档,明确各代码形态的组织规范 5. 清理重复的旧文件与冗余代码
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""WebSocket 广播枢纽:把插件 run 线程产生的 日志/进度/完成 事件实时推给前端。
|
||||
run 在 worker 线程执行,publish 通过 run_coroutine_threadsafe 投递到主事件循环。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from typing import Dict
|
||||
|
||||
|
||||
class Hub:
|
||||
def __init__(self):
|
||||
self._loop: asyncio.AbstractEventLoop | None = None
|
||||
self._clients: list = []
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def bind(self, loop: asyncio.AbstractEventLoop):
|
||||
self._loop = loop
|
||||
|
||||
async def connect(self, websocket) -> None:
|
||||
await websocket.accept()
|
||||
with self._lock:
|
||||
self._clients.append(websocket)
|
||||
try:
|
||||
await websocket.receive_text() # 挂起直到客户端断开
|
||||
finally:
|
||||
with self._lock:
|
||||
self._clients.remove(websocket)
|
||||
|
||||
def publish(self, event: Dict) -> None:
|
||||
if self._loop is None:
|
||||
return
|
||||
asyncio.run_coroutine_threadsafe(self._send(event), self._loop)
|
||||
|
||||
async def _send(self, event: Dict) -> None:
|
||||
with self._lock:
|
||||
clients = list(self._clients)
|
||||
for ws in clients:
|
||||
try:
|
||||
await ws.send_json(event)
|
||||
except Exception:
|
||||
with self._lock:
|
||||
self._clients.remove(ws)
|
||||
|
||||
|
||||
HUB = Hub()
|
||||
Reference in New Issue
Block a user