mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:xiaozhi-server端提供重启server服务
This commit is contained in:
parent
80be8b2eaa
commit
f861a4fa62
2 changed files with 51 additions and 0 deletions
|
|
@ -6,6 +6,8 @@ import time
|
||||||
import queue
|
import queue
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import websockets
|
import websockets
|
||||||
|
|
@ -214,6 +216,53 @@ class ConnectionHandler:
|
||||||
elif isinstance(message, bytes):
|
elif isinstance(message, bytes):
|
||||||
await handleAudioMessage(self, message)
|
await handleAudioMessage(self, message)
|
||||||
|
|
||||||
|
async def _handle_server_message(self, msg_json):
|
||||||
|
"""处理服务器控制消息"""
|
||||||
|
if msg_json.get("server_action") == "restart":
|
||||||
|
# 验证密钥
|
||||||
|
if msg_json.get("paramValue") == "43c716ae-37c4-49ba-84ab-2f0fbfcd7115":
|
||||||
|
self.logger.bind(tag=TAG).info("收到合法的服务器重启请求")
|
||||||
|
# 发送确认消息
|
||||||
|
await self.websocket.send(json.dumps({
|
||||||
|
"type": "server",
|
||||||
|
"status": "restarting",
|
||||||
|
"message": "服务器即将重启"
|
||||||
|
}))
|
||||||
|
# 在子线程中执行重启
|
||||||
|
threading.Thread(target=self._restart_server).start()
|
||||||
|
else:
|
||||||
|
await self.websocket.send(json.dumps({
|
||||||
|
"type": "server",
|
||||||
|
"status": "error",
|
||||||
|
"message": "无效的密钥"
|
||||||
|
}))
|
||||||
|
|
||||||
|
def _restart_server(self):
|
||||||
|
"""重启方法"""
|
||||||
|
|
||||||
|
async def graceful_shutdown():
|
||||||
|
"""关闭连接并停止事件循环"""
|
||||||
|
await self.close() # 关闭连接
|
||||||
|
self.loop.stop() # 停止事件循环
|
||||||
|
|
||||||
|
# 在事件循环中执行关闭操作
|
||||||
|
asyncio.run_coroutine_threadsafe(graceful_shutdown(), self.loop).result()
|
||||||
|
|
||||||
|
# 等待关闭操作完全执行后再重启服务
|
||||||
|
def start_new_process():
|
||||||
|
"""启动新进程"""
|
||||||
|
time.sleep(3)
|
||||||
|
subprocess.Popen(
|
||||||
|
[sys.executable, "app.py"],
|
||||||
|
stdin=sys.stdin,
|
||||||
|
stdout=sys.stdout,
|
||||||
|
stderr=sys.stderr,
|
||||||
|
start_new_session=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# 在新的线程中启动新进程
|
||||||
|
threading.Thread(target=start_new_process).start()
|
||||||
|
|
||||||
def _initialize_components(self, private_config):
|
def _initialize_components(self, private_config):
|
||||||
"""初始化组件"""
|
"""初始化组件"""
|
||||||
if private_config is not None:
|
if private_config is not None:
|
||||||
|
|
|
||||||
|
|
@ -61,5 +61,7 @@ async def handleTextMessage(conn, message):
|
||||||
asyncio.create_task(handleIotDescriptors(conn, msg_json["descriptors"]))
|
asyncio.create_task(handleIotDescriptors(conn, msg_json["descriptors"]))
|
||||||
if "states" in msg_json:
|
if "states" in msg_json:
|
||||||
asyncio.create_task(handleIotStatus(conn, msg_json["states"]))
|
asyncio.create_task(handleIotStatus(conn, msg_json["states"]))
|
||||||
|
elif msg_json["type"] == "server":
|
||||||
|
await conn._handle_server_message(msg_json)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
await conn.websocket.send(message)
|
await conn.websocket.send(message)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue