refactor: own subscription create transactions

This commit is contained in:
jxxghp
2026-08-21 20:37:31 +08:00
parent de2957b9de
commit 7de72e0e83
11 changed files with 840 additions and 120 deletions
+5 -2
View File
@@ -361,8 +361,11 @@ flowchart LR
`app/application/`(见 `application/subscription/write.py``application/history.py`)。
订阅新增、查询、变更、删除、身份和搜索契约已经统一收口在 `application/subscription/`
不再保留主题包之外的第二个写入入口。
- Oper 只 stage mutation,不创建独立 Session、不提交;Application Command 通过请求或任务
入口注入的 UnitOfWork 统一 `commit/rollback`,事件、刷新和上报只在 commit 成功后执行。
- 规范写入口中的 Oper 只 stage mutation,不创建独立 Session、不提交;Application Command
通过请求或任务入口注入的 UnitOfWork 统一 `commit/rollback`,事件、刷新和上报只在 commit
成功后执行。订阅新增样板由 `startup/subscription.py` 创建独占 Session
`application/subscription/write.py` 决定事务与 post-commit 边界,`SubscribeOper.stage_add()`
只查重、`add``flush`。旧 SDK 显式构造的无会话 Oper 暂留兼容自动短会话,不得被新代码复用。
`transaction-debt-baseline.json` 将存量 178 个 Model 事务装饰器冻结为只降不增低水位。
- 每次表结构变更必须新增 `database/versions/` 下的 Alembic 迁移。
- 运行期业务配置使用 `SystemConfigKey` 枚举 + `SystemConfigOper`,禁止裸字符串键;
@@ -6,7 +6,7 @@
> 审计范围:宿主后端;排除 `app/plugins/**` 运行时插件副本
> 规范优先级:`AGENTS.md` 与 `docs/rules/` 高于本文
> 相关文档:`docs/architecture-overview.md`、`docs/refactor/backend-architecture-governance.md`、`docs/refactor/backend-module-refactor-compatibility.md`
> 实施进度:阶段 0(ARCH-201203)、阶段 1ARCH-210212)与 ARCH-220 已完成,后续任务按 ID 独立提交和回滚
> 实施进度:阶段 0(ARCH-201203)、阶段 1ARCH-210212)与 ARCH-220221 已完成,后续任务按 ID 独立提交和回滚
## 1. 结论先行
@@ -373,6 +373,20 @@ flowchart TB
**完成标准**:一个业务动作只有一个事务所有者;任意入口都不会因内部 Model 方法提前 commit 而产生部分写入。
**实施记录(2026-08-21**
- `app/startup/subscription.py` 为每次规范新增创建独占同步/异步 Session;
`CreateSubscriptionCommand` / `AsyncCreateSubscriptionCommand` 持有 UoWOper 只执行
查重、`add``flush`
- `SubscribeOper.stage_add()` 的查重 SQL 已收口到 Oper,不再调用 Model 自动会话装饰器;
无会话构造 `SubscribeOper()` 的旧 SDK 路径保留原自动短会话和返回值,未扩散为规范入口。
- Chain 把原有“成功消息 → `SubscribeAdded` 事件 → Server 统计”作为显式 post-commit
回调交给 Commandcommit/flush 失败回滚,事件或上报失败只传播原异常,不回滚已提交记录。
- 同步/异步 `SubscribeChain.add` 方法长度从各 203 行降至 183/186 行;新增 9 个事务边界测试,
覆盖成功顺序、commit/flush 失败、重复请求、Oper 不提交、事件失败、上报失败与真实落库。
- Model 装饰器总数仍为 178:本切片绕开了继承自 `Base.create/async_create` 的自动提交,
但为保留既有 Model/旧 SDK 查询兼容未机械删除查询装饰器;ratchet 保持不增,后续切片继续下降。
#### ARCH-222:按风险迁移其余写用例
推荐顺序:
+6 -1
View File
@@ -99,6 +99,11 @@ Oper classes accept and return persistence values. Turning a `MediaInfo` or
- A synchronous Session is private to one worker thread. An AsyncSession is
private to one asyncio task/operation; neither may be stored in a process
singleton or reused by concurrent work.
- Subscription creation is the reference slice: `app/startup/subscription.py`
creates an exclusive Session, `app/application/subscription/write.py` owns the
UoW and post-commit callback, and `SubscribeOper.stage_add()` only queries,
adds, and flushes. Preserve `SubscribeOper.add()` only for legacy SDK callers;
new host code must not use that auto-commit compatibility path.
Run `./.venv/bin/python scripts/architecture/baseline.py --check-host` after
persistence changes. A deliberate debt reduction may refresh the low-water mark
@@ -214,4 +219,4 @@ When `REDIS_HOST` is configured, `app/modules/redis/` provides a distributed cac
- `settings.API_TOKEN` and other secret fields must not be included in log output or API responses.
- The `config list --show-secrets` flag exists specifically to gate secret visibility in the CLI.
*Last Updated: 2026-08-14*
*Last Updated: 2026-08-21*