File size: 3,493 Bytes
d0dd276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sys,asyncio
#from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.asyncio import AsyncIOScheduler  # 替换为异步调度器
from app.utils.logging import log
from app.utils.stats import api_stats_manager
from app.utils import check_version
from zoneinfo import ZoneInfo
from app.config import settings,persistence
import copy  # 添加copy模块导入

def handle_exception(exc_type, exc_value, exc_traceback):
    """
    全局异常处理函数
    
    处理未捕获的异常,并记录到日志中
    """
    if issubclass(exc_type, KeyboardInterrupt):
        sys.excepthook(exc_type, exc_value, exc_traceback)
        return
    from app.utils.error_handling import translate_error
    error_message = translate_error(str(exc_value))
    log('error', f"未捕获的异常: {error_message}", status_code=500, error_message=error_message)


def schedule_cache_cleanup(response_cache_manager, active_requests_manager):
    """
    设置定期清理缓存和活跃请求的定时任务
    顺便定时检查更新
    Args:
        response_cache_manager: 响应缓存管理器实例
        active_requests_manager: 活跃请求管理器实例
    """
    beijing_tz = ZoneInfo("Asia/Shanghai")
    scheduler = AsyncIOScheduler(timezone=beijing_tz)  # 使用 AsyncIOScheduler 替代 BackgroundScheduler
    
    # 添加任务时直接传递异步函数(无需额外包装)
    scheduler.add_job(response_cache_manager.clean_expired, 'interval', minutes=1)
    scheduler.add_job(active_requests_manager.clean_completed, 'interval', seconds=30)
    scheduler.add_job(active_requests_manager.clean_long_running, 'interval', minutes=5, args=[300])
    
    # 使用同步包装器调用异步函数
    def run_cleanup():
        try:
            # 创建新的事件循环而不是获取现有的
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            
            # 在这个新循环中运行清理操作
            loop.run_until_complete(api_stats_manager.cleanup())
        except Exception as e:
            log('error', f"清理统计数据时出错: {str(e)}")
        finally:
            # 确保关闭循环以释放资源
            loop.close()
    
    # 添加同步的清理任务
    scheduler.add_job(run_cleanup, 'interval', minutes=5)
    
    # 同样修改定时重置函数
    def run_reset():
        try:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            loop.run_until_complete(api_call_stats_clean())
        except Exception as e:
            log('error', f"重置统计数据时出错: {str(e)}")
        finally:
            loop.close()
    
    scheduler.add_job(check_version, 'interval', hours=4)
    scheduler.add_job(run_reset, 'cron', hour=15, minute=0)
    scheduler.start()
    return scheduler

async def api_call_stats_clean():
    """
    每天定时重置API调用统计数据
    
    使用新的统计系统重置
    """
    from app.utils.logging import log
    
    try:
        # 记录重置前的状态
        log('info', "开始重置API调用统计数据")
        
        # 使用新的统计系统重置
        await api_stats_manager.reset()
        
        log('info', "API调用统计数据已成功重置")
        persistence.save_settings()
            
    except Exception as e:
        log('error', f"重置API调用统计数据时发生错误: {str(e)}")
        raise