添加日志并推送到 WebSocket(线程安全)
(self, task_uuid: str, log_message: str)
| 97 | logger.info(f"任务 {task_uuid} 已标记为取消") |
| 98 | |
| 99 | def add_log(self, task_uuid: str, log_message: str): |
| 100 | """添加日志并推送到 WebSocket(线程安全)""" |
| 101 | # 先广播到 WebSocket,确保实时推送 |
| 102 | # 然后再添加到队列,这样 get_unsent_logs 不会获取到这条日志 |
| 103 | if self._loop and self._loop.is_running(): |
| 104 | try: |
| 105 | asyncio.run_coroutine_threadsafe( |
| 106 | self._broadcast_log(task_uuid, log_message), |
| 107 | self._loop |
| 108 | ) |
| 109 | except Exception as e: |
| 110 | logger.warning(f"推送日志到 WebSocket 失败: {e}") |
| 111 | |
| 112 | # 广播后再添加到队列 |
| 113 | with _get_log_lock(task_uuid): |
| 114 | _log_queues[task_uuid].append(log_message) |
| 115 | |
| 116 | async def _broadcast_log(self, task_uuid: str, log_message: str): |
| 117 | """广播日志到所有 WebSocket 连接""" |
no test coverage detected