From 671fe4ecc2640e7248058136b796a8f29284a696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Wed, 1 Jul 2026 23:07:24 +0800 Subject: [PATCH] feat: price_monitor cron entry + health check script (DB freshness monitoring) --- scripts/check_price_monitor.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/check_price_monitor.py diff --git a/scripts/check_price_monitor.py b/scripts/check_price_monitor.py new file mode 100644 index 0000000..af8d0e4 --- /dev/null +++ b/scripts/check_price_monitor.py @@ -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())