fix: remove redundant ntfy messages and harden Uptime Kuma heartbeat

run_incremental_sync.py:
- Replace ntfy-pushing log_start/log_success with log_info. Liveness is
  owned by Uptime Kuma's heartbeat, so the startup and per-table success
  messages no longer duplicate Uptime's notifications.

uptime_kuma_utils.py (shared util, backward compatible):
- Run the heartbeat in a background daemon thread (new start()/stop()),
  decoupled from the sync loop so heartbeat network time/retries never
  delay syncing.
- Add retry (up to 3 attempts) and raise request timeout 5s -> 10s to
  tolerate Uptime Kuma's intermittent slow responses / transient 4xx.
- Keep send_heartbeat/check_and_send_heartbeat/send_stop_signal as
  backward-compatible wrappers; excel_sync_to_sql.py still uses them and
  now benefits from the longer timeout + retry.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-06-15 13:06:53 +08:00
parent 04779c5aea
commit dd27cbaf97
2 changed files with 112 additions and 82 deletions

View File

@@ -4,8 +4,8 @@ import sys
import pyodbc
from config import SQL_SERVER_CONN, ACCESS_DRIVER, SYNC_MAPPING, POLL_INTERVAL, LOG_TABLE_CONFIG, UPTIME_KUMA_CONFIG
import db_utils
from log_utils import (LoggerManager, log_success, log_error, log_warning, log_info, log_processing,
log_skip, log_critical, log_start, log_stop, log_file, log_database, log_sync)
from log_utils import (LoggerManager, log_error, log_warning, log_info, log_processing,
log_skip, log_critical, log_stop, log_file, log_database, log_sync)
from uptime_kuma_utils import UptimeKumaMonitor
# 初始化日志管理器
@@ -313,11 +313,11 @@ def process_sync_task():
file_error_count += 1
continue
# 同步成功
# 同步成功 (仅本地日志记录, 不推送 ntfy; 服务存活状态由 Uptime Kuma 心跳负责)
msg = f"表 [{target_table}] 同步并校验通过: {len(inserted_pk_set)} 条入库"
if removed_count > 0:
msg += f", {removed_count} 条随源删除"
log_success(msg)
log_info(msg)
file_success_count += 1
file_total_records += len(record_ids)
@@ -368,7 +368,8 @@ def process_sync_task():
# 使用 uptime_kuma_utils.UptimeKumaMonitor 替代原有实现
if __name__ == "__main__":
log_start("增量同步服务已启动 (配置驱动模式 + 逐主键落库校验)")
# 仅本地日志记录, 不推送 ntfy 启动消息 (服务存活状态由 Uptime Kuma 心跳负责, 避免重复通知)
log_info("增量同步服务已启动 (配置驱动模式 + 逐主键落库校验)")
log_info(f"轮询间隔: {POLL_INTERVAL}")
log_info(f"监控配置: {len(SYNC_MAPPING)} 个文件")
log_info(f"提交后复核: {'开启' if ENABLE_POST_COMMIT_VERIFY else '关闭'}")
@@ -376,16 +377,15 @@ if __name__ == "__main__":
log_info(f"心跳间隔: {UPTIME_KUMA_CONFIG['heartbeat_interval']}")
log_info("=" * 70)
# 启动时发送第一次心跳
uptime_monitor.send_heartbeat()
# 启动后台心跳线程: 立即发送首跳, 之后按间隔周期发送 (与同步主循环解耦)
uptime_monitor.start()
try:
while True:
try:
has_work = process_sync_task()
# 检查是否需要发送心跳
uptime_monitor.check_and_send_heartbeat()
# 心跳由后台线程独立周期发送, 此处无需再调用
# 如果有工作,说明可能还有积压,休息短一点(0.1s)
# 如果没工作,休息标准间隔
@@ -399,5 +399,5 @@ if __name__ == "__main__":
log_critical(f"主循环崩溃: {e}")
time.sleep(5)
finally:
# 停止时发送心跳停止信号(可选)
uptime_monitor.send_stop_signal()
# 停止后台心跳线程, 并向 Uptime Kuma 发送 down 信号
uptime_monitor.stop()