去掉redis
启动时只检查数据库连接不更新表结构
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
# 数据库配置
|
||||
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/ai_talk_callback_db
|
||||
|
||||
# Redis配置
|
||||
REDIS_URL=redis://localhost:6379/0
|
||||
REDIS_LOCK_TIMEOUT=30
|
||||
REDIS_LOCK_RETRY_DELAY=0.1
|
||||
REDIS_LOCK_MAX_RETRIES=100
|
||||
|
||||
# 业务配置
|
||||
COUNT_THRESHOLD=3
|
||||
EXTERNAL_API_ENABLED=false
|
||||
|
||||
@@ -5,12 +5,6 @@ class Settings(BaseSettings):
|
||||
# 数据库配置
|
||||
database_url: str = ""
|
||||
|
||||
# Redis配置
|
||||
redis_url: str = "redis://localhost:6379/0"
|
||||
redis_lock_timeout: int = 30 # 锁超时时间(秒)
|
||||
redis_lock_retry_delay: float = 0.1 # 锁重试延迟(秒)
|
||||
redis_lock_max_retries: int = 100 # 最大重试次数
|
||||
|
||||
# 业务配置
|
||||
count_threshold: int = 3 # count阈值,大于等于此值直接返回
|
||||
external_api_enabled: bool = False # 是否启用外部API调用
|
||||
|
||||
@@ -106,5 +106,4 @@ def get_logger(name: Optional[str] = None) -> logging.Logger:
|
||||
app_logger = get_logger("app")
|
||||
api_logger = get_logger("api")
|
||||
db_logger = get_logger("database")
|
||||
redis_logger = get_logger("redis")
|
||||
external_api_logger = get_logger("external_api")
|
||||
@@ -167,15 +167,6 @@ async def ai_talk_callback(
|
||||
AI Talk回调接口处理
|
||||
"""
|
||||
try:
|
||||
# 参数校验
|
||||
if not callback_data:
|
||||
logger.error("❌ callback_data 参数为空")
|
||||
raise HTTPException(status_code=400, detail="callback_data 参数不能为空")
|
||||
|
||||
if not isinstance(callback_data.get('data'), list):
|
||||
logger.error(f"❌ data 参数类型无效: {type(callback_data.get('data'))}")
|
||||
raise HTTPException(status_code=400, detail="data 参数必须是数组")
|
||||
|
||||
# 记录回调请求(包含siteId)
|
||||
success, callback_log_id = await log_callback_request(db, request, siteId, callback_data)
|
||||
|
||||
|
||||
33
main.py
33
main.py
@@ -1,10 +1,10 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from contextlib import asynccontextmanager
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.config import settings
|
||||
from app.database import init_db
|
||||
from app.redis_lock import redis_manager
|
||||
from app.database import engine
|
||||
from app.routes import router
|
||||
from app.logger import get_logger, LoggerManager
|
||||
|
||||
@@ -20,15 +20,20 @@ async def lifespan(app: FastAPI):
|
||||
logger.info("🚀 应用启动中...")
|
||||
|
||||
try:
|
||||
# 初始化数据库
|
||||
logger.info("📊 初始化数据库连接...")
|
||||
await init_db()
|
||||
logger.info("✅ 数据库连接成功")
|
||||
|
||||
# 初始化Redis
|
||||
logger.info("🔴 初始化Redis连接...")
|
||||
await redis_manager.connect()
|
||||
logger.info("✅ Redis连接成功")
|
||||
# 验证数据库连接
|
||||
logger.info("📊 验证数据库连接...")
|
||||
try:
|
||||
async with engine.begin() as conn:
|
||||
# 执行简单的查询来验证连接
|
||||
result = await conn.execute(text("SELECT 1"))
|
||||
connection_test = result.scalar()
|
||||
if connection_test == 1:
|
||||
logger.info("✅ 数据库连接验证成功")
|
||||
else:
|
||||
raise Exception("数据库连接测试失败")
|
||||
except Exception as db_error:
|
||||
logger.error(f"❌ 数据库连接验证失败: {db_error}")
|
||||
raise
|
||||
|
||||
logger.info(f"🎉 {settings.app_name} 启动完成!")
|
||||
yield
|
||||
@@ -40,12 +45,6 @@ async def lifespan(app: FastAPI):
|
||||
finally:
|
||||
# 关闭时清理
|
||||
logger.info("🛑 应用关闭中...")
|
||||
try:
|
||||
await redis_manager.disconnect()
|
||||
logger.info("✅ Redis连接已关闭")
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Redis关闭时出错: {e}")
|
||||
|
||||
logger.info("👋 应用已关闭")
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ uvicorn[standard]>=0.38.0
|
||||
sqlalchemy>=2.0.44
|
||||
asyncpg>=0.13.0
|
||||
alembic>=1.17.2
|
||||
redis[hiredis]>=7.1.0
|
||||
pydantic>=2.12.5
|
||||
pydantic-settings>=2.12.0
|
||||
python-multipart>=0.0.20
|
||||
|
||||
8
setup.py
8
setup.py
@@ -139,7 +139,6 @@ async def main():
|
||||
print("🚀 开始数据库初始化...")
|
||||
print(f"📋 配置信息:")
|
||||
print(f" - 数据库URL: {settings.database_url}")
|
||||
print(f" - Redis URL: {settings.redis_url}")
|
||||
print(f" - 应用名称: {settings.app_name}")
|
||||
print(f" - 外部API URL: {settings.external_api_url}")
|
||||
print()
|
||||
@@ -164,10 +163,9 @@ async def main():
|
||||
print("🎉 数据库初始化完成!")
|
||||
print("\n📝 下一步:")
|
||||
print(" 1. 配置 .env 文件中的数据库连接信息")
|
||||
print(" 2. 确保 Redis 服务正在运行")
|
||||
print(" 3. 运行应用: python main.py")
|
||||
print(f" 4. 访问API文档: http://localhost:8000/docs")
|
||||
print(f" 5. 接口地址: POST /ai-talk/callback/{{siteId}}/failure")
|
||||
print(" 2. 运行应用: python main.py")
|
||||
print(f" 3. 访问API文档: http://localhost:8000/docs")
|
||||
print(f" 4. 接口地址: POST /ai-talk/callback/{{siteId}}/failure")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user