Files
MoFin/deploy/profile-scripts/vacuum_state_db.py
T
知微 80d59c9331 feat: 统一部署目录——所有运行时文件归入MoFin repo
- deploy/bot/ — XMPP bot核心(xmpp_agent_core + xmpp_zhiwei_bot)
- deploy/profile-scripts/ — cron脚本(price_monitor等)
- 运行时文件已替换为指向MoFin的符号链接
- 改代码只需改MoFin,系统自动生效
2026-07-17 23:12:35 +08:00

31 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""vacuum_state_db.py — 每周整理state.db,防止磁盘I/O退化
在非交易时段运行(周六凌晨),不影响交易系统。
"""
import sqlite3, os
DBS = [
"/home/hmo/.hermes/profiles/position-analyst/state.db",
"/home/hmo/.hermes/state.db",
]
for db_path in DBS:
if not os.path.exists(db_path):
continue
size_before = os.path.getsize(db_path) / 1024 / 1024
try:
c = sqlite3.connect(db_path)
c.execute("PRAGMA auto_vacuum=2")
# 只做incremental vacuum,不做full vacuum(耗时太长)
c.execute("PRAGMA incremental_vacuum(50000)")
c.execute("PRAGMA cache_size=-200000")
c.execute("PRAGMA mmap_size=268435456")
c.execute("PRAGMA wal_checkpoint(TRUNCATE)")
c.close()
size_after = os.path.getsize(db_path) / 1024 / 1024
saved = size_before - size_after
print(f"{db_path.split('/')[-2]}: {size_before:.0f}MB -> {size_after:.0f}MB (reclaim {saved:.0f}MB)")
except Exception as e:
print(f"{db_path.split('/')[-2]}: ERROR {e}")