1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-05-10 00:44:18 +08:00

fix: 修复关闭不了的问题

This commit is contained in:
涵曦
2026-01-07 12:31:49 +08:00
parent 33b446b749
commit 640aa156cf
3 changed files with 44 additions and 8 deletions

View File

@@ -162,9 +162,13 @@ def main():
except Exception as e:
print(f"Execption {e}")
# 全局变量用于信号处理
xiaomusic_instance = None
def run_server(port):
xiaomusic = XiaoMusic(config)
HttpInit(xiaomusic)
nonlocal xiaomusic_instance
xiaomusic_instance = XiaoMusic(config)
HttpInit(xiaomusic_instance)
uvicorn.run(
HttpApp,
host=["0.0.0.0", "::"],
@@ -174,6 +178,16 @@ def main():
def signal_handler(sig, frame):
print("主进程收到退出信号,准备退出...")
# 关闭 JS 插件管理器
if (
xiaomusic_instance
and hasattr(xiaomusic_instance, "js_plugin_manager")
and xiaomusic_instance.js_plugin_manager
):
try:
xiaomusic_instance.js_plugin_manager.shutdown()
except Exception as e:
print(f"关闭 JS 插件管理器时出错: {e}")
os._exit(0) # 退出主进程
# 捕获主进程的退出信号

View File

@@ -32,6 +32,7 @@ class JSPluginManager:
self._lock = threading.Lock()
self.request_id = 0
self.pending_requests = {}
self._is_shutting_down = False # 添加关闭标志
# 启动 Node.js 子进程
self._start_node_process()
@@ -70,9 +71,12 @@ class JSPluginManager:
def _monitor_node_process(self):
"""监控 Node.js 进程状态"""
while True:
if self._is_shutting_down:
break
if self.node_process and self.node_process.poll() is not None:
self.log.warning("Node.js process died, restarting...")
self._start_node_process()
if not self._is_shutting_down:
self.log.warning("Node.js process died, restarting...")
self._start_node_process()
time.sleep(5)
def _start_message_handler(self):
@@ -1113,6 +1117,24 @@ class JSPluginManager:
def shutdown(self):
"""关闭插件管理器"""
self.log.info("Shutting down JS Plugin Manager...")
self._is_shutting_down = True
if self.node_process:
self.node_process.terminate()
self.node_process.wait()
try:
# 先尝试优雅关闭
self.node_process.terminate()
# 等待最多 3 秒
try:
self.node_process.wait(timeout=3)
self.log.info("Node.js process terminated gracefully")
except subprocess.TimeoutExpired:
# 如果超时,强制杀死
self.log.warning("Node.js process did not terminate, killing...")
self.node_process.kill()
self.node_process.wait()
self.log.info("Node.js process killed")
except Exception as e:
self.log.error(f"Error during shutdown: {e}")
self.log.info("JS Plugin Manager shutdown complete")

View File

@@ -647,11 +647,11 @@ const runner = new PluginRunner();
// 处理进程退出
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully');
// 不输出任何内容,避免干扰 JSON 通信
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully');
// 不输出任何内容,避免干扰 JSON 通信
process.exit(0);
});