Refactor Uptime Kuma heartbeat implementation to use UptimeKumaMonitor class, enhancing maintainability and logging capabilities
This commit is contained in:
@@ -7,7 +7,6 @@ import datetime
|
||||
import urllib.parse
|
||||
import warnings
|
||||
import time
|
||||
import requests
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from sqlalchemy import create_engine, text
|
||||
@@ -19,6 +18,11 @@ from log_utils import (log_error, log_warning, log_info, log_processing, log_fil
|
||||
log_start, log_complete, log_stop, LoggerManager)
|
||||
from config import (DB_CONFIG, CACHE_DIR, EXCEL_CONFIGS, BATCH_SIZE, TABLE_SCHEMA,
|
||||
EXCEL_SYNC_INTERVAL, EXCEL_SYNC_UPTIME_KUMA_CONFIG)
|
||||
from uptime_kuma_utils import UptimeKumaMonitor
|
||||
|
||||
# 初始化 Uptime Kuma 监控器
|
||||
excel_uptime_monitor = UptimeKumaMonitor(EXCEL_SYNC_UPTIME_KUMA_CONFIG)
|
||||
excel_uptime_monitor.set_logger(log_warning)
|
||||
|
||||
# ================= 抑制 openpyxl 的数据验证警告 =================
|
||||
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
|
||||
@@ -295,28 +299,7 @@ class DataSynchronizer:
|
||||
log_error(f"生成 ContractData 失败: {e}", exc_info=True)
|
||||
|
||||
# ================= Uptime Kuma 心跳 =================
|
||||
|
||||
_last_heartbeat_time = 0
|
||||
|
||||
def send_heartbeat():
|
||||
"""发送心跳信号到 Uptime Kuma"""
|
||||
global _last_heartbeat_time
|
||||
|
||||
if not EXCEL_SYNC_UPTIME_KUMA_CONFIG.get('enabled', False):
|
||||
return
|
||||
|
||||
try:
|
||||
url = EXCEL_SYNC_UPTIME_KUMA_CONFIG['push_url']
|
||||
params = {
|
||||
'status': 'up',
|
||||
'msg': 'OK',
|
||||
'ping': ''
|
||||
}
|
||||
response = requests.get(url, params=params, timeout=5)
|
||||
response.raise_for_status()
|
||||
_last_heartbeat_time = time.time()
|
||||
except Exception as e:
|
||||
log_warning(f"心跳发送失败: {e}")
|
||||
# 使用 uptime_kuma_utils.UptimeKumaMonitor 替代原有实现
|
||||
|
||||
def main():
|
||||
# 初始化日志管理器
|
||||
@@ -347,7 +330,7 @@ def main():
|
||||
log_info("=" * 70)
|
||||
|
||||
# 启动时发送第一次心跳
|
||||
send_heartbeat()
|
||||
excel_uptime_monitor.send_heartbeat()
|
||||
|
||||
try:
|
||||
while True:
|
||||
@@ -365,9 +348,7 @@ def main():
|
||||
# 等待下次同步,期间持续发送心跳
|
||||
while time.time() < next_sync_time:
|
||||
# 检查是否需要发送心跳
|
||||
time_since_last_heartbeat = time.time() - _last_heartbeat_time
|
||||
if time_since_last_heartbeat >= EXCEL_SYNC_UPTIME_KUMA_CONFIG['heartbeat_interval']:
|
||||
send_heartbeat()
|
||||
excel_uptime_monitor.check_and_send_heartbeat()
|
||||
|
||||
# 短暂休眠
|
||||
time.sleep(1)
|
||||
@@ -382,13 +363,7 @@ def main():
|
||||
time.sleep(EXCEL_SYNC_INTERVAL)
|
||||
finally:
|
||||
# 停止时发送心跳停止信号
|
||||
if EXCEL_SYNC_UPTIME_KUMA_CONFIG.get('enabled', False):
|
||||
try:
|
||||
url = EXCEL_SYNC_UPTIME_KUMA_CONFIG['push_url']
|
||||
params = {'status': 'down', 'msg': 'Service stopped'}
|
||||
requests.get(url, params=params, timeout=5)
|
||||
except:
|
||||
pass
|
||||
excel_uptime_monitor.send_stop_signal()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user