mirror of
https://github.com/rnvm9wjdtj-bot/myaps_api.git
synced 2026-06-02 05:54:40 +00:00
将数据持久化记录归入storage路径
This commit is contained in:
+5
-1
@@ -85,4 +85,8 @@ test/
|
|||||||
project_files/**/prod.json
|
project_files/**/prod.json
|
||||||
|
|
||||||
# Binlog 位置管理器文件
|
# Binlog 位置管理器文件
|
||||||
.binlog_position.json
|
.binlog_position.json
|
||||||
|
|
||||||
|
*.sqlite*
|
||||||
|
storage/
|
||||||
|
storage/**/
|
||||||
@@ -71,16 +71,20 @@ LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO"
|
|||||||
logger = log_config.get_logger(__name__, level=LOG_LEVEL)
|
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:
|
class BinlogPositionManager:
|
||||||
"""Binlog 位置管理器 - 负责持久化和恢复 binlog 位置"""
|
"""Binlog 位置管理器 - 负责持久化和恢复 binlog 位置"""
|
||||||
|
|
||||||
def __init__(self, position_file: str = None):
|
def __init__(self):
|
||||||
if position_file is None:
|
self.position_file = BINLOG_POSITION_FILE
|
||||||
# 默认保存到项目根目录下的 .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
|
|
||||||
|
|
||||||
self._lock = threading.RLock()
|
self._lock = threading.RLock()
|
||||||
self._last_save_time = 0
|
self._last_save_time = 0
|
||||||
@@ -276,6 +280,8 @@ class ConnectionHealthChecker:
|
|||||||
# 使用事件等待,支持快速退出
|
# 使用事件等待,支持快速退出
|
||||||
self._stop_event.wait(self.check_interval)
|
self._stop_event.wait(self.check_interval)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MySQLBinlogMonitor:
|
class MySQLBinlogMonitor:
|
||||||
# 单例模式实现
|
# 单例模式实现
|
||||||
_instance = None
|
_instance = None
|
||||||
@@ -325,12 +331,9 @@ class MySQLBinlogMonitor:
|
|||||||
else:
|
else:
|
||||||
self._position_manager = None
|
self._position_manager = None
|
||||||
logger.info("⚠️ Binlog 位置管理器已禁用")
|
logger.info("⚠️ Binlog 位置管理器已禁用")
|
||||||
# 检查并删除已存在的标记点文件
|
if os.path.exists(BINLOG_POSITION_FILE):
|
||||||
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):
|
|
||||||
try:
|
try:
|
||||||
os.remove(position_file)
|
os.remove(BINLOG_POSITION_FILE)
|
||||||
logger.info("🗑️ 已删除旧的 binlog 标记点文件")
|
logger.info("🗑️ 已删除旧的 binlog 标记点文件")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"⚠️ 删除 binlog 标记点文件失败: {e}")
|
logger.warning(f"⚠️ 删除 binlog 标记点文件失败: {e}")
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
from .settings import BASE_DIR
|
||||||
|
from .database import TORTOISE_ORM_CONFIG
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"TORTOISE_ORM_CONFIG",
|
||||||
|
"BASE_DIR"
|
||||||
|
]
|
||||||
+1
-1
@@ -38,7 +38,7 @@ connections = {
|
|||||||
"local_data": {
|
"local_data": {
|
||||||
"engine": "tortoise.backends.sqlite",
|
"engine": "tortoise.backends.sqlite",
|
||||||
"credentials": {
|
"credentials": {
|
||||||
"file_path": BASE_DIR / "local_data.sqlite3", # 统一管理数据文件
|
"file_path": BASE_DIR / "storage" / "local_data.sqlite3", # 统一管理数据文件
|
||||||
"journal_mode": "WAL", # 写前日志,提升并发性能
|
"journal_mode": "WAL", # 写前日志,提升并发性能
|
||||||
"synchronous": "NORMAL", # 性能与安全的平衡
|
"synchronous": "NORMAL", # 性能与安全的平衡
|
||||||
"cache_size": -100000, # 100MB 内存缓存
|
"cache_size": -100000, # 100MB 内存缓存
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
# - free2 客户产品号
|
# - free2 客户产品号
|
||||||
# - free3 包装要求
|
# - 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 (
|
from .._base import (
|
||||||
get_scheduler_minute, cron_task, CLIENT_LOGGER, CLIENT_SESSION, PROJECT_JSON_FILE,
|
get_scheduler_minute, cron_task, CLIENT_LOGGER, CLIENT_SESSION, PROJECT_JSON_FILE,
|
||||||
ApsHelpers, get_session
|
ApsHelpers, get_session
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
[tool.aerich]
|
[tool.aerich]
|
||||||
tortoise_orm = "core.database.TORTOISE_ORM_CONFIG"
|
tortoise_orm = "scripts.migrate.monitor_orm_config.monitor_orm_config"
|
||||||
location = "./migrations"
|
location = "./migrations"
|
||||||
src_folder = "./."
|
src_folder = "./."
|
||||||
|
|||||||
@@ -1,18 +1,53 @@
|
|||||||
@echo off
|
@echo off
|
||||||
|
set "PROJECT_DIR="
|
||||||
|
for /f "tokens=2 delims==" %%a in ('findstr "^PROJECT_DIR=" "%~dp0\..\..\.env"') do set "PROJECT_DIR=%%a"
|
||||||
|
|
||||||
REM 设置项目目录
|
cd /d "%~dp0\..\.."
|
||||||
set PROJECT_DIR=JYHDXS
|
|
||||||
|
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 Project Directory: %PROJECT_DIR%
|
||||||
echo Starting database migration...
|
echo Starting monitor models database migration...
|
||||||
|
|
||||||
REM 使用完整路径运行 Python
|
REM 检查是否已存在迁移文件夹,若存在则跳过初始化
|
||||||
"%~dp0\..\..\venv\Scripts\python.exe" -m aerich init -t core.database.TORTOISE_ORM
|
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
|
pause
|
||||||
|
|||||||
@@ -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"]
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user