feat: price_monitor cron entry + health check script (DB freshness monitoring)

This commit is contained in:
知微
2026-07-01 23:07:24 +08:00
parent ec285669c4
commit 671fe4ecc2
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
"""保活检测:检查 DB 中的价格是否新鲜,price_monitor 是否在运行"""
import sqlite3, subprocess, sys, os
from datetime import datetime
DB = '/home/hmo/web-dashboard/data/mofin.db'
def check():
issues = []
# 1. 检查 price_monitor 进程
try:
r = subprocess.run(['pgrep', '-f', 'price_monitor'], capture_output=True, text=True)
if r.stdout.strip():
print(f" price_monitor: RUNNING (PID {r.stdout.strip()})")
else:
issues.append("price_monitor 未运行")
print(f" price_monitor: NOT RUNNING")
except:
pass
# 2. 检查 DB 价格新鲜度
try:
db = sqlite3.connect(DB)
db.row_factory = sqlite3.Row
row = db.execute("SELECT updated_at FROM portfolio_summary WHERE id=1").fetchone()
if row and row['updated_at']:
try:
dt = datetime.strptime(row['updated_at'], '%Y-%m-%d %H:%M')
age = (datetime.now() - dt).total_seconds() / 60
if age > 10:
issues.append(f"DB 价格 {age:.0f} 分钟未更新")
print(f" DB updated: {row['updated_at']} ({age:.0f} min ago)")
except:
pass
else:
issues.append("DB 无价格数据")
db.close()
except Exception as e:
issues.append(f"DB 读取失败: {e}")
# 3. 输出
if issues:
print(f"\n ISSUES: {', '.join(issues)}")
return 1
print(" OK")
return 0
if __name__ == '__main__':
sys.exit(check())