Files
group_fqcd_jr/app/repository/agent_run_repository.py
T
张胜宇 e239eb778b docs: 品牌全量口径统一为「南方基金」+ 作废文档清理
1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富
   统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」;
   同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。
2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本),
   新增《文档规整方案与开发前待决事项-2026-09-17》。
3) 客服agent 四份交付文档首次纳入本分支。
2026-09-17 15:15:22 +08:00

68 lines
2.8 KiB
Python

from datetime import UTC, datetime, timedelta
from sqlalchemy import select, update
from sqlalchemy.engine import CursorResult
from sqlalchemy.ext.asyncio import AsyncSession
from app.model.platform import AgentRun
class AgentRunRepository:
def __init__(self, session: AsyncSession) -> None:
self.session = session
async def get(self, run_id: str) -> AgentRun | None:
result = await self.session.execute(select(AgentRun).where(AgentRun.run_id == run_id))
return result.scalar_one_or_none()
async def claim(self, run_id: str, worker_id: str, lease_seconds: int) -> bool:
now = datetime.now(UTC).replace(tzinfo=None)
result = await self.session.execute(
update(AgentRun)
.where(AgentRun.run_id == run_id, AgentRun.status.in_(("queued", "running")))
.where((AgentRun.locked_until.is_(None)) | (AgentRun.locked_until < now))
.values(status="running", worker_id=worker_id,
locked_until=now + timedelta(seconds=lease_seconds),
attempt_count=AgentRun.attempt_count + 1)
)
return isinstance(result, CursorResult) and result.rowcount == 1
async def request_cancel(self, run_id: str, when: datetime) -> bool:
result = await self.session.execute(
update(AgentRun)
.where(AgentRun.run_id == run_id)
.where(AgentRun.status.in_(("queued", "running")))
.values(status="cancel_requested", cancel_requested_at=when)
)
return isinstance(result, CursorResult) and result.rowcount == 1
async def renew(self, run_id: str, worker_id: str, lease_seconds: int) -> bool:
now = datetime.now(UTC).replace(tzinfo=None)
result = await self.session.execute(
update(AgentRun)
.where(AgentRun.run_id == run_id, AgentRun.worker_id == worker_id,
AgentRun.status == "running", AgentRun.locked_until > now)
.values(locked_until=now + timedelta(seconds=lease_seconds))
)
return isinstance(result, CursorResult) and result.rowcount == 1
async def is_cancel_requested(self, run_id: str) -> bool:
run = await self.get(run_id)
return run is not None and run.status == "cancel_requested"
async def record_failure(self, run_id: str, error_code: str, retry_limit: int) -> bool:
run = await self.get(run_id)
if run is None:
return False
if run.attempt_count < retry_limit:
run.status = "queued"
run.error_code = error_code
run.locked_until = None
await self.session.flush()
return True
run.status = "failed"
run.error_code = error_code
run.locked_until = None
await self.session.flush()
return False