37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from fastapi import FastAPI
|
|
|
|
from app.api.routes import router as api_router
|
|
from app.core.config import get_settings
|
|
from app.core.logging import setup_logging
|
|
from app.services.runtime import auto_indexer, scheduler, worker
|
|
|
|
settings = get_settings()
|
|
|
|
setup_logging()
|
|
|
|
app = FastAPI(title=settings.app_name)
|
|
app.include_router(api_router, prefix=settings.api_prefix)
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def _startup() -> None:
|
|
scheduler.start()
|
|
if getattr(settings, "auto_index_enabled", False):
|
|
try:
|
|
await auto_indexer.startup()
|
|
except Exception: # noqa: BLE001
|
|
# 启动阶段不阻塞服务:自动入库失败会在后续 tick 中继续重试/记录
|
|
pass
|
|
scheduler.add_interval_job(
|
|
job_id="auto_index_tick",
|
|
func=auto_indexer.tick,
|
|
seconds=int(getattr(settings, "auto_index_interval_seconds", 5)),
|
|
)
|
|
await worker.start()
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def _shutdown() -> None:
|
|
scheduler.shutdown()
|
|
await worker.stop()
|