61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""Application entrypoint: FastAPI API + scheduler + Telegram bot (frozen wiring, #34)."""
|
|
|
|
import logging
|
|
from collections.abc import AsyncGenerator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from app.api.routes import router
|
|
from app.channels import telegram
|
|
from app.config import get_settings
|
|
from app.db.bootstrap import init_db
|
|
from app.db.session import dispose
|
|
from app.scheduler import jobs as scheduler
|
|
|
|
# Logs carry only tokens, states and event codes — never PII or raw message text.
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
|
log = logging.getLogger("app")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
|
|
settings = get_settings()
|
|
log.info("startup llm_provider=%s demo_clock=%s", settings.llm_provider, settings.demo_clock)
|
|
try:
|
|
await init_db()
|
|
log.info("db schema ready")
|
|
except (OSError, SQLAlchemyError) as exc:
|
|
# In compose Postgres is required and healthy before the app starts; this branch is for running the
|
|
# API (and its tests) without a database. Endpoints touching the DB will fail until it is reachable.
|
|
log.error("db init failed, continuing without db: %s", type(exc).__name__)
|
|
if settings.max_enabled:
|
|
raise # MAX requires persistent bindings and a delivery queue.
|
|
# Lanes fill these in (#13, #11); main.py itself stays frozen (#34).
|
|
max_channel = None
|
|
try:
|
|
await telegram.start()
|
|
if settings.max_enabled:
|
|
from app.channels import max_bot
|
|
|
|
max_channel = max_bot
|
|
await max_channel.start()
|
|
await scheduler.start()
|
|
yield
|
|
finally:
|
|
await scheduler.stop()
|
|
if max_channel is not None:
|
|
await max_channel.stop()
|
|
await telegram.stop()
|
|
await dispose()
|
|
|
|
|
|
app = FastAPI(title="PTSRaiAgent API", version="0.1.0", lifespan=lifespan)
|
|
app.include_router(router)
|
|
|
|
|
|
@app.get("/health")
|
|
async def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|