fix: 修复定时任务日志重复打印问题

- 添加配置变化检测,只在首次运行或配置变化时打印日志
- 避免每5秒重复打印相同的定时任务设置日志
- 减少日志噪音,提高日志可读性

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-13 03:38:47 +08:00
parent 42cc86e290
commit d77595eba0

18
app.py
View File

@@ -3978,10 +3978,19 @@ def scheduled_task_worker():
import traceback import traceback
traceback.print_exc() traceback.print_exc()
# 用于跟踪配置变化,避免重复打印日志
last_config_hash = [None] # 使用列表以便在闭包中修改
# 每分钟检查一次配置 # 每分钟检查一次配置
def check_and_schedule(): def check_and_schedule():
config = database.get_system_config() config = database.get_system_config()
# 计算配置哈希,检测配置是否变化
config_hash = (config.get('schedule_enabled'), config.get('schedule_time', '02:00'))
config_changed = (last_config_hash[0] != config_hash)
is_first_run = (last_config_hash[0] is None)
last_config_hash[0] = config_hash
# 清除旧的任务 # 清除旧的任务
schedule.clear() schedule.clear()
@@ -4004,10 +4013,13 @@ def scheduled_task_worker():
# 始终添加每天凌晨3点CST的数据清理任务 # 始终添加每天凌晨3点CST的数据清理任务
cleanup_utc_time = cst_to_utc_time("03:00") cleanup_utc_time = cst_to_utc_time("03:00")
schedule.every().day.at(cleanup_utc_time).do(cleanup_old_data) schedule.every().day.at(cleanup_utc_time).do(cleanup_old_data)
print(f"[定时任务] 已设置数据清理任务: 每天 CST 03:00 (UTC {cleanup_utc_time})")
# 每小时清理过期验证码 # 每小时清理过期验证码
schedule.every().hour.do(cleanup_expired_captcha) schedule.every().hour.do(cleanup_expired_captcha)
# 只在首次运行时打印基础任务日志
if is_first_run:
print(f"[定时任务] 已设置数据清理任务: 每天 CST 03:00 (UTC {cleanup_utc_time})")
print(f"[定时任务] 已设置验证码清理任务: 每小时执行一次") print(f"[定时任务] 已设置验证码清理任务: 每小时执行一次")
# 如果启用了定时浏览任务,则添加 # 如果启用了定时浏览任务,则添加
@@ -4015,7 +4027,11 @@ def scheduled_task_worker():
schedule_time_cst = config.get('schedule_time', '02:00') schedule_time_cst = config.get('schedule_time', '02:00')
schedule_time_utc = cst_to_utc_time(schedule_time_cst) schedule_time_utc = cst_to_utc_time(schedule_time_cst)
schedule.every().day.at(schedule_time_utc).do(run_scheduled_task) schedule.every().day.at(schedule_time_utc).do(run_scheduled_task)
# 只在首次运行或配置变化时打印
if is_first_run or config_changed:
print(f"[定时任务] 已设置浏览任务: 每天 CST {schedule_time_cst} (UTC {schedule_time_utc})") print(f"[定时任务] 已设置浏览任务: 每天 CST {schedule_time_cst} (UTC {schedule_time_utc})")
elif config_changed and not is_first_run:
print(f"[定时任务] 浏览任务已禁用")
# 初始检查 # 初始检查
check_and_schedule() check_and_schedule()