[M-AI-03] 实现 AiJob 状态机与原子状态仓储 #286
Closed
opened 2026-06-20 16:43:25 +08:00 by wangdl
·
1 comment
Labels
Clear labels
adr
Area: adr
architecture
Area: architecture
area:activity
活动/统计
area:admin
管理后台
area:admin-api
area:ai
AI/RAG
area:ai-runtime
AI Runtime / AI 分析体系相关
area:analytics
area:api
API 接口
area:auth
认证与授权
area:cos
对象存储
area:database
数据库/Migration
area:import
文件导入/解析
area:knowledge
知识库/知识点
area:learning-info
area:learning-session
area:quiz
测验/自测
area:reading-event
area:reading-progress
area:review
复习系统
area:security
安全相关
audit
Area: audit
audit:api-admin-info
audit:api-info
audit:planned
已完成宏观规划,尚未代码审查
audit:reviewed
backend
Area: backend
billing
Area: billing
blocked-by:api-info-aggregation
blocked-by:api-info-core
blocked-by:api-info-ops
blocked-by:api-info-schema
blocked-by:processor
blocked-by:schema
compatibility
Area: compatibility
gate
Area: gate
infrastructure
Area: infrastructure
priority:p0
最高优先级,阻塞发布
priority:p1
高优先级,里程碑必需
priority:p2
中优先级,后续版本
prisma
Area: prisma
release
Area: release
repo:api
API 仓库 Issue
status:blocked
被阻塞
status:done
已完成
status:partial
status:todo
type:aggregation
type:bug
缺陷修复
type:design
设计
type:docs
文档
type:feature
新功能
type:migration
type:refactor
重构
type:test
work:admin-api
work:aggregation
work:api
work:artifact
题目/卡片产物
work:audit
work:circuit-breaker
熔断
work:contract
work:design
架构/协议设计工作
work:docs
work:export
work:extend-existing
work:internal-api
Runtime 内部接口
work:job
Job 调度相关
work:new-module
work:new-table
work:ops
work:query
work:quota
额度/限流
work:schema
Prisma Schema 设计
work:security
work:service
Service 层实现
work:snapshot
Snapshot 构建
work:test
No Label
Milestone
No items
No Milestone
M-AI-03 统一 Job Engine、Registry 与 Outbox
Projects
Clear projects
No project
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: wangdl/api-server#286
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking 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?
类型 / 标签
风险等级
P0
依赖
目标
实现统一生命周期的唯一写入入口,禁止业务代码任意修改
lifecycleStatus。实现内容
新增:
AiJobStateMachineAiJobLifecycleRepository要求
状态更新必须使用条件更新:
禁止:
prisma.aiJob.update({ lifecycleStatus });Legacy 映射
至少保证:
cancel_requested保留当前 Legacy status,直到进入 cancelled。非目标
验收标准
开发完成评论
完成内容
ai-job-state-machine.ts):IllegalStateTransitionErrorLIFECYCLE_TO_STATUS+STATUS_TO_LIFECYCLE),含 #285 修正的 cancelled/cancel_requestedparse()方法处理 null/undefined/无效字符串ai-job-lifecycle.repository.ts):createJob(tx, input):在事务中创建 queued 状态的 Job + Shadow WritelockJob():CASqueued → running(WHERE lifecycleStatus = 'queued'),失败时区分终态/已锁定/不存在extendLock():续约lockUntil+ 返回cancelRequested信号markSucceeded(tx?):CASrunning → succeeded+ Shadow Writecompleted,清除错误和运行时字段markFailed(error, tx?):CASrunning → failed+ 写入errorCode/publicErrorMessage/internalErrorMessagemarkCancelled(tx?):从 queued 或 running 取消 →cancelled,Shadow Writefailed+errorCode=CANCELLEDrequestCancellation():queued→直接 cancel;running→设置cancelRequestedAt信号findByLifecycleStatus()/findExpiredLocks()/findById()只读查询ai-job.errors.ts):JobLockConflictError,JobNotActiveError,JobAlreadyTerminalError,JobNotCancellableError,JobExtendLockError,IllegalStateTransitionErrorai-job.module.ts):独立模块,当前导出 StateMachine + Repository修改文件
src/modules/ai-job/ai-job.errors.ts(新增,56 行)src/modules/ai-job/ai-job-state-machine.ts(新增,99 行)src/modules/ai-job/ai-job-lifecycle.repository.ts(新增,351 行)src/modules/ai-job/ai-job.module.ts(新增,20 行)src/modules/ai-job/ai-job-lifecycle.repository.spec.ts(新增,340 行)测试情况
npx jest --testPathPatterns="ai-job-lifecycle.repository.spec" --no-coverage验收标准对照
markSucceeded/markFailed/markCancelledCAS 失败时抛出JobAlreadyTerminalErrorlockJobCAS updateMany + count===0 检测lifecycleStatus+status(Shadow Write)createJob:queuedAt,lockJob:startedAt/lockedAt/lockUntil,markSucceeded/markFailed/markCancelled:finishedAtmarkSucceeded清除 errorCode 等字段;markFailed写入;markCancelled写入 errorCode=CANCELLED是否建议进入 Review