Files
xian_algorithm_new/app/core/launcher.py
T

122 lines
3.8 KiB
Python
Raw Normal View History

"""
应用启动器
"""
import sys
from pathlib import Path
2026-06-12 14:53:35 +08:00
from datetime import datetime
from app.core.env_checker import check_environment
from app.core.venv_manager import check_virtualenv
from app.core.dependency_manager import check_dependencies
class AppLauncher:
"""应用启动器"""
2026-05-12 16:20:02 +08:00
def __init__(self, project_root: Path):
"""
初始化启动器
Args:
project_root: 项目根目录路径
"""
self.project_root = project_root
2026-06-17 21:47:10 +08:00
"""
初始化logger
"""
self.logger = None
2026-05-12 16:20:02 +08:00
def run(self):
"""执行完整的启动流程"""
try:
# 检查系统和Python版本
if not check_environment():
sys.exit(1)
2026-05-12 16:20:02 +08:00
# 检查虚拟环境
check_virtualenv(self.project_root)
2026-05-12 16:20:02 +08:00
2026-06-06 14:18:07 +08:00
# 检查是否正在使用虚拟环境运行
import platform
import sys
venv_path = self.project_root / ".venv"
os_name = platform.system()
if os_name == 'Windows':
venv_python = venv_path / "Scripts" / "python.exe"
else: # Linux/Mac
venv_python = venv_path / "bin" / "python3"
# 如果当前不是使用虚拟环境的Python,则重新启动
current_python = Path(sys.executable).resolve()
venv_python_resolved = venv_python.resolve()
if current_python != venv_python_resolved:
print("\n" + "=" * 50)
print("检测到未使用虚拟环境,正在切换到虚拟环境...")
print("=" * 50)
2026-06-12 09:45:35 +08:00
# 使用虚拟环境的Python重新启动应用(不传递参数避免重复检查)
2026-06-06 14:18:07 +08:00
import subprocess
cmd = [str(venv_python)] + sys.argv
subprocess.run(cmd, check=True)
return
2026-06-12 09:45:35 +08:00
# 以下代码仅在虚拟环境中执行
2026-06-12 14:53:35 +08:00
# 检查安装依赖
2026-06-12 09:45:35 +08:00
check_dependencies(self.project_root)
# 启动应用
print("\n" + "=" * 50)
2026-06-20 15:50:24 +08:00
print("[OK] 所有检查通过,准备启动应用...")
print("=" * 50)
2026-06-17 21:47:10 +08:00
# 延迟导入logger
from app.utils.logger import get_logger
self.logger = get_logger()
self.logger.info("系统环境检查通过,开始执行主程序...")
2026-05-12 16:20:02 +08:00
start()
except Exception as e:
2026-06-20 15:50:24 +08:00
if self.logger:
self.logger.error(f"启动失败: {e}")
else:
print(f"[FAIL] 启动失败: {e}")
sys.exit(1)
2026-05-12 16:20:02 +08:00
def start():
"""启动应用服务"""
2026-06-06 08:38:19 +08:00
import threading
from config import settings
2026-05-30 15:03:39 +08:00
from app.core.rainfall_manager import rainfall_manager
2026-05-30 16:05:41 +08:00
from app.utils.logger import get_logger
from app.utils.thread_pool_manager import block_main_thread, thread_pool_manager
2026-05-30 15:03:39 +08:00
2026-05-12 16:20:02 +08:00
logger = get_logger()
2026-05-30 15:03:39 +08:00
2026-06-06 08:38:19 +08:00
# 启动 FastAPI 服务(守护线程)
def run_api_server():
import uvicorn
from app.core.server import create_app
api_app = create_app()
uvicorn.run(
api_app,
host=getattr(settings, "API_HOST", "127.0.0.1"),
port=int(getattr(settings, "API_PORT", 8082)),
log_level="info",
)
api_thread = threading.Thread(target=run_api_server, daemon=True, name="api-server")
api_thread.start()
logger.info(f"FastAPI 服务已启动: http://{getattr(settings, 'API_HOST', '127.0.0.1')}:{getattr(settings, 'API_PORT', 8082)}")
2026-05-12 16:20:02 +08:00
# 启动降雨站点监测
logger.info("启动降雨站点监测服务...")
2026-06-12 14:53:35 +08:00
2026-05-18 17:26:24 +08:00
rainfall_manager.monitoring_rainfall_station_id('2025-09-16 20:00:00')
2026-05-30 15:03:39 +08:00
2026-05-12 16:20:02 +08:00
# 阻塞主线程,防止程序立即退出
block_main_thread()