From e7054ea13fbb198ce8d6fa27f9f3b947759c07c9 Mon Sep 17 00:00:00 2001 From: CGD <3030332422@qq.com> Date: Wed, 7 May 2025 14:36:08 +0800 Subject: [PATCH] =?UTF-8?q?update:=E9=87=8D=E5=90=AF=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=8A=9F=E8=83=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 33 +++++++++++++++++++ main/xiaozhi-server/core/handle/textHandle.py | 3 ++ 2 files changed, 36 insertions(+) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 060de4c9..8a3fea26 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -1,6 +1,7 @@ import os import copy import json +import sys import uuid import time import queue @@ -233,6 +234,38 @@ class ConnectionHandler: elif isinstance(message, bytes): await handleAudioMessage(self, message) + async def handle_restart(self, message): + """处理服务器重启请求""" + try: + + self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...") + + # 发送确认响应 + await self.websocket.send(json.dumps({ + "type": "server_response", + "status": "success", + "message": "服务器重启中..." + })) + + # 异步执行重启操作 + def restart_server(): + """实际执行重启的方法""" + time.sleep(1) + self.logger.bind(tag=TAG).info("执行服务器重启...") + python = sys.executable + os.execl(python, python, *sys.argv) + + # 使用线程执行重启避免阻塞事件循环 + threading.Thread(target=restart_server, daemon=True).start() + + except Exception as e: + self.logger.bind(tag=TAG).error(f"重启失败: {str(e)}") + await self.websocket.send(json.dumps({ + "type": "server_response", + "status": "error", + "message": f"Restart failed: {str(e)}" + })) + def _initialize_components(self, private_config): """初始化组件""" if private_config is not None: diff --git a/main/xiaozhi-server/core/handle/textHandle.py b/main/xiaozhi-server/core/handle/textHandle.py index 0bded15b..61ef92c3 100644 --- a/main/xiaozhi-server/core/handle/textHandle.py +++ b/main/xiaozhi-server/core/handle/textHandle.py @@ -136,5 +136,8 @@ async def handleTextMessage(conn, message): } ) ) + # 重启服务器 + elif msg_json["action"] == "restart": + await conn.handle_restart(msg_json) except json.JSONDecodeError: await conn.websocket.send(message)