mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-08-28 03:30:07 +08:00
本次提交主要包含以下内容:
1. **日志自动删除功能**:
* 新增环境变量 (`AUTO_DELETE_ERROR_LOGS_ENABLED`, `AUTO_DELETE_ERROR_LOGS_DAYS`, `AUTO_DELETE_REQUEST_LOGS_ENABLED`, `AUTO_DELETE_REQUEST_LOGS_DAYS`) 用于控制错误日志和请求日志的自动删除策略。
* 在 `app/config/config.py` 中添加了对这些新配置项的支持和验证逻辑 (Pydantic `validator` 更新为 `field_validator`)。
* 修改了 `app/log/logger.py` 以适应新的日志配置。
* 新增 `app/scheduler/scheduled_tasks.py` 用于执行定期的日志清理任务。
* 新增 `app/service/error_log/error_log_service.py` 和 `app/service/request_log/request_log_service.py` 来处理具体的日志删除逻辑。
* 更新了 `app/router/error_log_routes.py` 和 `app/router/scheduler_routes.py` 以集成新功能。
2. **前端配置页面更新**:
* 在 `app/templates/config_editor.html` 和 `app/static/js/config_editor.js` 中添加了用于配置日志自动删除选项的用户界面元素。
3. **代码和文件结构调整**:
* 删除了不再使用的 `app/scheduler/key_checker.py` 文件。
* 在 `.gitignore` 文件中添加了 `default_db` 以忽略该目录。
4. **其他**:
* 对 `app/core/application.py` 进行了相应调整。
该更新旨在增强应用的日志管理能力,提供更灵活的日志保留策略,并优化了配置界面的用户体验。
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
"""
|
|
定时任务控制路由模块
|
|
"""
|
|
|
|
from fastapi import APIRouter, Request, HTTPException, status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.core.security import verify_auth_token
|
|
from app.scheduler.scheduled_tasks import start_scheduler, stop_scheduler
|
|
from app.log.logger import get_scheduler_routes
|
|
|
|
logger = get_scheduler_routes()
|
|
|
|
router = APIRouter(
|
|
prefix="/api/scheduler",
|
|
tags=["Scheduler"]
|
|
)
|
|
|
|
async def verify_token(request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to scheduler API")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
@router.post("/start", summary="启动定时任务")
|
|
async def start_scheduler_endpoint(request: Request):
|
|
"""Start the background scheduler task"""
|
|
await verify_token(request)
|
|
try:
|
|
logger.info("Received request to start scheduler.")
|
|
start_scheduler()
|
|
return JSONResponse(content={"message": "Scheduler started successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error starting scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to start scheduler: {str(e)}"
|
|
)
|
|
|
|
@router.post("/stop", summary="停止定时任务")
|
|
async def stop_scheduler_endpoint(request: Request):
|
|
"""Stop the background scheduler task"""
|
|
await verify_token(request)
|
|
try:
|
|
logger.info("Received request to stop scheduler.")
|
|
stop_scheduler()
|
|
return JSONResponse(content={"message": "Scheduler stopped successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error stopping scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to stop scheduler: {str(e)}"
|
|
) |