diff --git a/.gitignore b/.gitignore index e44d36b..016a508 100644 --- a/.gitignore +++ b/.gitignore @@ -85,4 +85,8 @@ test/ project_files/**/prod.json # Binlog 位置管理器文件 -.binlog_position.json \ No newline at end of file +.binlog_position.json + +*.sqlite* +storage/ +storage/**/ \ No newline at end of file diff --git a/apps/data_opt/utils/mysqlmonitor.py b/apps/data_opt/utils/mysqlmonitor.py index bf0991f..f693fb2 100644 --- a/apps/data_opt/utils/mysqlmonitor.py +++ b/apps/data_opt/utils/mysqlmonitor.py @@ -71,16 +71,20 @@ LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO" logger = log_config.get_logger(__name__, level=LOG_LEVEL) + +BINLOG_POSITION_FILE = os.path.join( + os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), + "storage", + ".binlog_position.json" +) + + + class BinlogPositionManager: """Binlog 位置管理器 - 负责持久化和恢复 binlog 位置""" - def __init__(self, position_file: str = None): - if position_file is None: - # 默认保存到项目根目录下的 .binlog_position 文件 - base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) - self.position_file = os.path.join(base_dir, '.binlog_position.json') - else: - self.position_file = position_file + def __init__(self): + self.position_file = BINLOG_POSITION_FILE self._lock = threading.RLock() self._last_save_time = 0 @@ -276,6 +280,8 @@ class ConnectionHealthChecker: # 使用事件等待,支持快速退出 self._stop_event.wait(self.check_interval) + + class MySQLBinlogMonitor: # 单例模式实现 _instance = None @@ -325,12 +331,9 @@ class MySQLBinlogMonitor: else: self._position_manager = None logger.info("⚠️ Binlog 位置管理器已禁用") - # 检查并删除已存在的标记点文件 - base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) - position_file = os.path.join(base_dir, '.binlog_position.json') - if os.path.exists(position_file): + if os.path.exists(BINLOG_POSITION_FILE): try: - os.remove(position_file) + os.remove(BINLOG_POSITION_FILE) logger.info("🗑️ 已删除旧的 binlog 标记点文件") except Exception as e: logger.warning(f"⚠️ 删除 binlog 标记点文件失败: {e}") diff --git a/core/__init__.py b/core/__init__.py index e69de29..edc11f5 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -0,0 +1,9 @@ + +from .settings import BASE_DIR +from .database import TORTOISE_ORM_CONFIG + + +__all__ = [ + "TORTOISE_ORM_CONFIG", + "BASE_DIR" +] \ No newline at end of file diff --git a/core/database.py b/core/database.py index df098f2..de7ca58 100644 --- a/core/database.py +++ b/core/database.py @@ -38,7 +38,7 @@ connections = { "local_data": { "engine": "tortoise.backends.sqlite", "credentials": { - "file_path": BASE_DIR / "local_data.sqlite3", # 统一管理数据文件 + "file_path": BASE_DIR / "storage" / "local_data.sqlite3", # 统一管理数据文件 "journal_mode": "WAL", # 写前日志,提升并发性能 "synchronous": "NORMAL", # 性能与安全的平衡 "cache_size": -100000, # 100MB 内存缓存 diff --git a/project_files/HACYXS/client.py b/project_files/HACYXS/client.py index 3cb849f..ec51cde 100644 --- a/project_files/HACYXS/client.py +++ b/project_files/HACYXS/client.py @@ -15,7 +15,7 @@ # - free2 客户产品号 # - free3 包装要求 """ -from config.settings import MYAPS_DB_SET, MYAPS_MAIN_DB, THIS_BASE_URL, SCHEDULER_HOUR, SCHEDULER_MINUTE +from core.settings import MYAPS_DB_SET, MYAPS_MAIN_DB, THIS_BASE_URL, SCHEDULER_HOUR, SCHEDULER_MINUTE from .._base import ( get_scheduler_minute, cron_task, CLIENT_LOGGER, CLIENT_SESSION, PROJECT_JSON_FILE, ApsHelpers, get_session diff --git a/pyproject.toml b/pyproject.toml index 7da314f..e27257e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,4 +1,4 @@ [tool.aerich] -tortoise_orm = "core.database.TORTOISE_ORM_CONFIG" +tortoise_orm = "scripts.migrate.monitor_orm_config.monitor_orm_config" location = "./migrations" src_folder = "./." diff --git a/scripts/migrate/migrate_monitor.bat b/scripts/migrate/migrate_monitor.bat index cf20a73..2023ae5 100644 --- a/scripts/migrate/migrate_monitor.bat +++ b/scripts/migrate/migrate_monitor.bat @@ -1,18 +1,53 @@ @echo off +set "PROJECT_DIR=" +for /f "tokens=2 delims==" %%a in ('findstr "^PROJECT_DIR=" "%~dp0\..\..\.env"') do set "PROJECT_DIR=%%a" -REM 设置项目目录 -set PROJECT_DIR=JYHDXS +cd /d "%~dp0\..\.." + +if "%PROJECT_DIR%"=="" ( + echo Error: PROJECT_DIR not found in .env file + pause + exit /b 1 +) + +set PROJECT_DIR=%PROJECT_DIR% -REM 显示当前设置 echo Project Directory: %PROJECT_DIR% -echo Starting database migration... +echo Starting monitor models database migration... -REM 使用完整路径运行 Python -"%~dp0\..\..\venv\Scripts\python.exe" -m aerich init -t core.database.TORTOISE_ORM +REM 检查是否已存在迁移文件夹,若存在则跳过初始化 +if exist "migrations\monitor_models" ( + echo Migration folder already exists, skipping init... +) else ( + echo Initializing aerich for monitor_models... + venv\Scripts\python.exe -m aerich init -t scripts.migrate.monitor_orm_config.monitor_orm_config + if errorlevel 1 ( + echo Init failed, trying init-db anyway... + ) +) -"%~dp0\..\..\venv\Scripts\python.exe" -m aerich migrate --name monitor_models +REM 初始化数据库 +echo Running init-db... +venv\Scripts\python.exe -m aerich init-db +if errorlevel 1 ( + echo init-db may have already been run, continuing... +) -"%~dp0\..\..\venv\Scripts\python.exe" -m aerich upgrade +REM 执行迁移 +echo Running migrate... +venv\Scripts\python.exe -m aerich migrate --name monitor_models +if errorlevel 1 ( + echo Migrate failed, checking if already up to date... +) -echo Database migration completed! +REM 执行升级 +echo Running upgrade... +venv\Scripts\python.exe -m aerich upgrade +if errorlevel 1 ( + echo Upgrade failed or already at latest version. + goto :end +) + +echo Monitor models database migration completed successfully! +:end pause diff --git a/scripts/migrate/monitor_orm_config.py b/scripts/migrate/monitor_orm_config.py new file mode 100644 index 0000000..7942d2e --- /dev/null +++ b/scripts/migrate/monitor_orm_config.py @@ -0,0 +1,11 @@ +from core import TORTOISE_ORM_CONFIG + +# 复用 TORTOISE_ORM_CONFIG 中的 local_data 连接配置和 monitor_models 应用配置 +monitor_orm_config = { + "connections": { + "local_data": TORTOISE_ORM_CONFIG["connections"]["local_data"] + }, + "apps": { + "monitor_models": TORTOISE_ORM_CONFIG["apps"]["monitor_models"] + }, +}