You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

3 months ago
import asyncio
import sys
import signal
from config.settings import load_config, check_config_file
from core.websocket_server import WebSocketServer
from core.utils.util import check_ffmpeg_installed
3 months ago
from core.utils.mysql_util import init_mysql_pool, close_mysql_pool
3 months ago
TAG = __name__
async def wait_for_exit():
"""Windows 和 Linux 兼容的退出监听"""
loop = asyncio.get_running_loop()
stop_event = asyncio.Event()
if sys.platform == "win32":
# Windows: 用 sys.stdin.read() 监听 Ctrl + C
await loop.run_in_executor(None, sys.stdin.read)
else:
# Linux/macOS: 用 signal 监听 Ctrl + C
def stop():
stop_event.set()
loop.add_signal_handler(signal.SIGINT, stop)
loop.add_signal_handler(signal.SIGTERM, stop) # 支持 kill 进程
await stop_event.wait()
async def main():
3 months ago
# 检查配置文件
3 months ago
check_config_file()
3 months ago
# 检查 ffmpeg 是否安装
3 months ago
check_ffmpeg_installed()
3 months ago
# 加载配置
3 months ago
config = load_config()
3 months ago
# 【黄海】初始化 MySQL 连接池
3 months ago
await init_mysql_pool()
3 months ago
# 启动 WebSocket 服务器
ws_server = WebSocketServer(config)
ws_task = asyncio.create_task(ws_server.start())
try:
await wait_for_exit() # 监听退出信号
except asyncio.CancelledError:
print("任务被取消,清理资源中...")
finally:
ws_task.cancel()
try:
await ws_task
except asyncio.CancelledError:
pass
3 months ago
# 关闭 MySQL 连接池
await close_mysql_pool()
3 months ago
print("服务器已关闭,程序退出。")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
3 months ago
print("手动中断,程序终止。")