fix: stats loader and smtp daily reset

This commit is contained in:
2025-12-13 22:01:12 +08:00
parent 85a60009f3
commit 6bff5e4d97
20 changed files with 76 additions and 64 deletions

34
app.py
View File

@@ -4594,46 +4594,32 @@ def scheduled_task_worker():
# 清除旧的任务(仅当配置变化时才重建)
schedule.clear()
# 时区转换函数将CST时间转换为UTC时间容器使用UTC
def cst_to_utc_time(cst_time_str):
"""将CST时间字符串HH:MM转换为UTC时间字符串
Args:
cst_time_str: CST时间字符串格式为 HH:MM
Returns:
UTC时间字符串格式为 HH:MM
"""
# 解析CST时间
hour, minute = map(int, cst_time_str.split(':'))
# CST是UTC+8所以UTC时间 = CST时间 - 8小时
utc_hour = (hour - 8) % 24
return f"{utc_hour:02d}:{minute:02d}"
# schedule 使用本地时区;当前进程启动时已统一设置为 Asia/Shanghai北京时间
# 因此此处直接使用 CST 时间字符串即可,避免重复换算导致任务触发时间偏移。
# 始终添加每天凌晨3点CST的数据清理任务
cleanup_utc_time = cst_to_utc_time("03:00")
schedule.every().day.at(cleanup_utc_time).do(cleanup_old_data)
cleanup_time_cst = "03:00"
schedule.every().day.at(cleanup_time_cst).do(cleanup_old_data)
# 每小时清理过期验证码
schedule.every().hour.do(cleanup_expired_captcha)
# 每天北京时间0点重置SMTP配额
quota_reset_utc_time = cst_to_utc_time("00:00")
schedule.every().day.at(quota_reset_utc_time).do(email_service.reset_smtp_daily_quota)
quota_reset_time_cst = "00:00"
schedule.every().day.at(quota_reset_time_cst).do(email_service.reset_smtp_daily_quota)
# 只在首次运行时打印基础任务日志
if is_first_run:
print(f"[定时任务] 已设置数据清理任务: 每天 CST 03:00 (UTC {cleanup_utc_time})")
print(f"[定时任务] 已设置数据清理任务: 每天 CST {cleanup_time_cst}")
print(f"[定时任务] 已设置验证码清理任务: 每小时执行一次")
print(f"[定时任务] 已设置SMTP配额重置: 每天 CST 00:00 (UTC {quota_reset_utc_time})")
print(f"[定时任务] 已设置SMTP配额重置: 每天 CST {quota_reset_time_cst}")
# 如果启用了定时浏览任务,则添加
if schedule_enabled:
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_cst).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}")
elif config_changed and not is_first_run:
print(f"[定时任务] 浏览任务已禁用")