sales-assistant-py-new/app/uvicorn_main.py

54 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime, timedelta
from typing import Optional
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fastapi import FastAPI
from app.chat_qa_query.api.router.query_router import query_qa_router
from app.core.liftspan import lifespan
from app.summary.service import build
from app.core.log import logger
app = FastAPI(lifespan=lifespan)
scheduler = AsyncIOScheduler(timezone='Asia/Shanghai')
app.include_router(query_qa_router)
async def generate_daily_summary(date: Optional[str] = None):
if not date:
yesterday = datetime.now() - timedelta(days=1)
#date = yesterday.strftime("%Y-%-m-%-d") # 格式: 2026-6-15
date = datetime(yesterday.year, yesterday.month, yesterday.day)
# 按 年-月-日 无补零格式输出
date_format = f"{date.year}-{date.month}-{date.day}"
await build(date_format)
logger.info(f"{date} 消息摘要生成完成")
@app.on_event("startup")
async def startup_event():
scheduler.add_job(
generate_daily_summary,
trigger='cron',
hour=2,
minute=0,
second=0,
id='daily_summary',
name='每日消息摘要任务',
replace_existing=True
)
scheduler.start()
logger.info("定时任务调度器已启动每天凌晨2:00自动执行消息摘要任务")
@app.on_event("shutdown")
async def shutdown_event():
scheduler.shutdown()
logger.info("定时任务调度器已关闭")
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)