refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)

配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
This commit is contained in:
Sully
2026-05-22 13:44:50 +08:00
committed by GitHub
parent f09ae248f9
commit 53e0065e3e
298 changed files with 15247 additions and 4344 deletions

View File

@@ -10,18 +10,20 @@ from app.core.chapter_pipeline_lock import (
release_chapter_pipeline_lock,
)
from app.core.config import settings
from app.core.db import get_sync_db
from app.core.db import get_sync_db, transactional_sync
from app.core.logging import get_logger
from app.core.memoir_pipeline_progress import merge_fanout_item
from app.core.memoir_pipeline_trace import new_memoir_correlation_id
from app.core.memory_compaction_schedule import schedule_memory_compaction_run
from app.features.memoir import repo as memoir_repo
from app.features.memoir.models import Chapter
from app.features.memoir.constants import memoir
from app.features.story.constants import story
logger = get_logger(__name__)
@shared_task(bind=True, max_retries=8, default_retry_delay=30)
@shared_task(bind=True, max_retries=8, default_retry_delay=30, ignore_result=True)
def recompose_chapter(
self, chapter_id: str, memoir_correlation_id: str | None = None
) -> dict:
@@ -29,7 +31,7 @@ def recompose_chapter(
按章节物化 canonical_markdown仅当 markdown_compose_dirty 为 True 时执行;
与 pipeline 共用章节级 Redis 锁,拿不到锁则跳过(依赖后续触发重试)。
"""
lock_ttl = int(settings.chapter_pipeline_lock_ttl_seconds)
lock_ttl = int(story.chapter_pipeline_lock_ttl_seconds)
tid = str(self.request.id)
t0 = time.perf_counter()
merge_fanout_item(
@@ -90,10 +92,10 @@ def recompose_chapter(
chapter_id,
uid,
stage,
settings.memoir_recompose_retry_on_lock_contention,
memoir.recompose_retry_on_lock_contention,
ms,
)
if settings.memoir_recompose_retry_on_lock_contention:
if memoir.recompose_retry_on_lock_contention:
countdown = max(15, min(120, lock_ttl // 4))
raise self.retry(countdown=countdown)
merge_fanout_item(
@@ -106,13 +108,12 @@ def recompose_chapter(
)
return {"status": "skip_lock_contention"}
try:
composed = memoir_repo.compose_chapter_from_story_links_sync(
session, chapter_id
)
session.commit()
with transactional_sync(session):
composed = memoir_repo.compose_chapter_from_story_links_sync(
session, chapter_id
)
user_id = uid
except Exception as exc:
session.rollback()
logger.warning(
"recompose_chapter failed chapter_id={} err={}", chapter_id, exc
)