将数据持久化记录归入storage路径

This commit is contained in:
2026-04-17 06:03:27 +08:00
parent 64d3edbee2
commit b30cb99b72
8 changed files with 87 additions and 25 deletions
+4
View File
@@ -86,3 +86,7 @@ project_files/**/prod.json
# Binlog 位置管理器文件
.binlog_position.json
*.sqlite*
storage/
storage/**/
+15 -12
View File
@@ -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}")
+9
View File
@@ -0,0 +1,9 @@
from .settings import BASE_DIR
from .database import TORTOISE_ORM_CONFIG
__all__ = [
"TORTOISE_ORM_CONFIG",
"BASE_DIR"
]
+1 -1
View File
@@ -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 内存缓存
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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 = "./."
+44 -9
View File
@@ -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
+11
View File
@@ -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"]
},
}