create DB service to record messages #4
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
## High‑level plan
Introduce a lightweight Logging Service
New container, small FastAPI app exposing
POST /api/v1/logto receive the JSON payload and write it to Postgres.Why? Decouples DB latency from the Matrix bot; we reuse httpx already.
Refine schema & future‑proofing
Separate tables later (e.g.
bot_replies,edits,reactions) without touching this ingest path.Flow
matrix_service.on_message⮕ post JSON to Logging Service (non‑blocking,
fire_and_forget=Trueviaasyncio.create_task).⮕ continue current AI delegation logic.
Fault handling
event_idas PK ⇒ logging is naturally idempotent.Observability
logfmt), health endpoint/healthzin both services.Migrations
Security
LOG_TOKEN) in header to write endpoints..envonly; no credentials baked in images.Testing
psql -c "SELECT count(*) …"in CI.## 5. Implementation proposal (concrete steps & deliverables)
docker-compose.yml→ newpostgresservice with volume, env‑vars.logging_service/•
main.py(FastAPI, asyncpg)•
Dockerfile•
requirements.txtmatrix_service/main.py• Add
LOG_HANDLER_URL&LOG_TOKENenv•
async def log_event(payload):(httpx post, timeout 2 s,raise_for_statusoff)• In
on_message():asyncio.create_task(log_event(payload))migrations/with Alembic rev001_create_messages_table.py.env.exampleupdated with PG creds,LOG_TOKEN, etc.tests/test_logging.py(pytest‑asyncio) & GitHub/Gitea Actions workflow.README.mdsection “Conversation Logging” with setup instructions and query examples.Estimated effort
The proposal keeps today’s two‑microservice pattern, adds one focused microservice, and leaves both latency‑sensitive (Matrix) and compute‑heavy (AI) paths untouched. It fulfils the immediate goal—store
timestamp • room • user • message—while creating a clean lane for future enrichment (e.g., Thread context building, analytics, or GDPR exports).