refactor(Dockerfile): 使用requirements.txt替代硬编码依赖 将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装, 提高依赖管理的灵活性和可维护性。 feat(scheduling): 移除内置APScheduler,采用独立调度系统 移除app/core/background/scheduler.py中原来的APScheduler实现, 改为使用新的应用级调度系统app.application.scheduling。 refactor(task_queue): 调整任务队列模块结构和导入路径 将任务队列相关常量从app.core.background.task_queue.constants迁移至 app.tasks.constants,并更新所有相关导入路径和引用。 refactor(events): 将事件服务重构为独立的应用层组件 将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService 替代原有的app.modules.events.services.EventService。 feat(ai_memory): 增强AI记忆自动写入的安全策略 新增ai_memory_blocked_content_terms配置项用于阻止敏感内容, 添加TTL过期机制控制自动写入条目的生命周期。 fix(security): 强化生产环境安全验证机制 增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等 关键安全配置符合要求。 feat(risks): 优化风险事件操作动作的外键约束 为RiskEventAction模型的风险事件ID字段添加外键约束, 防止孤立记录并增强数据完整性。 refactor(audit): 优化审计服务方法命名和事务处理 将AuditService的log方法重命名为record以反映其阶段行为, 并调整事务提交时机以提高性能。 feat(events): 增强领域事件并发处理和响应模型 添加事件锁定机制防止重复处理,更新API响应模型以提供 更准确的数据类型定义。 ```
213 lines
7.3 KiB
Python
213 lines
7.3 KiB
Python
from datetime import timedelta
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import or_, select
|
|
|
|
from app.application.events.handlers import EventHandlerMixin
|
|
from app.core.config import get_settings
|
|
from app.core.constants import ActorValue
|
|
from app.core.http.pagination import bounded_limit
|
|
from app.core.utils.time import utc_now
|
|
from app.modules.audit.constants import (
|
|
AuditAction,
|
|
AuditRiskLevel,
|
|
AuditSource,
|
|
AuditTargetType,
|
|
)
|
|
from app.modules.audit.schemas import AuditLogCreate
|
|
from app.modules.audit.service import AuditService
|
|
from app.modules.events.constants import (
|
|
EventErrorDetail,
|
|
EventPayloadKey,
|
|
EventStatus,
|
|
)
|
|
from app.modules.events.models import DomainEvent
|
|
from app.modules.events.services import EventService
|
|
from app.modules.events.services.serialization import _serialize_event
|
|
|
|
|
|
class EventDispatchService(EventHandlerMixin):
|
|
"""Claim and dispatch outbox events at the application boundary."""
|
|
|
|
def __init__(self, db: Any):
|
|
self.db = db
|
|
self.events = EventService(db)
|
|
|
|
def get_event(self, event_id: str) -> DomainEvent:
|
|
return self.events.get_event(event_id)
|
|
|
|
def dispatch_event(
|
|
self,
|
|
event_id: str,
|
|
worker_id: str | None = None,
|
|
preclaimed: bool = False,
|
|
) -> DomainEvent:
|
|
lock_owner = worker_id or f"api:{uuid4().hex}"
|
|
record = (
|
|
self.get_event(event_id)
|
|
if preclaimed
|
|
else self._claim_event(event_id, lock_owner)
|
|
)
|
|
if record.status == EventStatus.PROCESSED:
|
|
return record
|
|
if preclaimed and record.locked_by != lock_owner:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=EventErrorDetail.EVENT_LOCKED,
|
|
)
|
|
if not preclaimed and record.locked_by != lock_owner:
|
|
return record
|
|
settings = get_settings()
|
|
try:
|
|
self._handle_event(record)
|
|
except Exception as exc:
|
|
retryable = record.attempts < self._max_attempts(record)
|
|
record.status = EventStatus.PENDING if retryable else EventStatus.FAILED
|
|
record.last_error = str(exc)
|
|
record.next_attempt_at = (
|
|
utc_now() + timedelta(seconds=settings.event_dispatch_retry_delay_seconds)
|
|
if retryable
|
|
else None
|
|
)
|
|
record.locked_by = None
|
|
record.locked_until = None
|
|
self.db.commit()
|
|
self.db.refresh(record)
|
|
self._audit_dispatch(record)
|
|
return record
|
|
record.status = EventStatus.PROCESSED
|
|
record.last_error = None
|
|
record.processed_at = utc_now()
|
|
record.next_attempt_at = None
|
|
record.locked_by = None
|
|
record.locked_until = None
|
|
self.db.commit()
|
|
self.db.refresh(record)
|
|
self._audit_dispatch(record)
|
|
return record
|
|
|
|
def dispatch_pending(
|
|
self,
|
|
limit: int = 100,
|
|
worker_id: str | None = None,
|
|
) -> list[dict[str, Any]]:
|
|
now = utc_now()
|
|
stmt = (
|
|
select(DomainEvent)
|
|
.where(
|
|
DomainEvent.status == EventStatus.PENDING,
|
|
or_(
|
|
DomainEvent.next_attempt_at.is_(None),
|
|
DomainEvent.next_attempt_at <= now,
|
|
),
|
|
or_(
|
|
DomainEvent.locked_until.is_(None),
|
|
DomainEvent.locked_until <= now,
|
|
),
|
|
or_(
|
|
DomainEvent.max_attempts.is_(None),
|
|
DomainEvent.attempts < DomainEvent.max_attempts,
|
|
),
|
|
)
|
|
.order_by(DomainEvent.id.asc())
|
|
.limit(bounded_limit(limit))
|
|
.with_for_update(skip_locked=True)
|
|
)
|
|
records = list(self.db.execute(stmt).scalars())
|
|
lock_owner = worker_id or f"worker:{uuid4().hex}"
|
|
locked_until = now + timedelta(
|
|
seconds=get_settings().event_dispatch_lock_seconds
|
|
)
|
|
for record in records:
|
|
record.locked_by = lock_owner
|
|
record.locked_until = locked_until
|
|
record.attempts += 1
|
|
self.db.commit()
|
|
return [
|
|
_serialize_event(
|
|
self.dispatch_event(
|
|
record.event_id,
|
|
worker_id=lock_owner,
|
|
preclaimed=True,
|
|
)
|
|
)
|
|
for record in records
|
|
]
|
|
|
|
def retry_event(self, event_id: str, actor: str = ActorValue.API) -> DomainEvent:
|
|
record = self.get_event(event_id)
|
|
if record.status == EventStatus.PROCESSED:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=EventErrorDetail.EVENT_NOT_RETRYABLE,
|
|
)
|
|
record.status = EventStatus.PENDING
|
|
record.actor = actor
|
|
record.attempts = 0
|
|
record.max_attempts = record.max_attempts or get_settings().event_dispatch_max_attempts
|
|
record.last_error = None
|
|
record.locked_by = None
|
|
record.locked_until = None
|
|
record.next_attempt_at = utc_now()
|
|
self.db.commit()
|
|
self.db.refresh(record)
|
|
return record
|
|
|
|
def _audit_dispatch(self, record: DomainEvent) -> None:
|
|
AuditService(self.db).log(
|
|
AuditLogCreate(
|
|
actor=record.actor,
|
|
source=AuditSource.EVENTS,
|
|
action=AuditAction.EVENT_DISPATCH,
|
|
target_type=AuditTargetType.DOMAIN_EVENT,
|
|
target_id=record.event_id,
|
|
risk_level=AuditRiskLevel.LOW,
|
|
response_payload={
|
|
EventPayloadKey.STATUS: record.status,
|
|
EventPayloadKey.ATTEMPTS: record.attempts,
|
|
EventPayloadKey.ERROR_MESSAGE: record.last_error,
|
|
},
|
|
)
|
|
)
|
|
|
|
def _can_attempt(self, record: DomainEvent) -> bool:
|
|
return record.attempts < self._max_attempts(record)
|
|
|
|
def _claim_event(self, event_id: str, lock_owner: str) -> DomainEvent:
|
|
now = utc_now()
|
|
record = self.db.execute(
|
|
select(DomainEvent)
|
|
.where(DomainEvent.event_id == event_id)
|
|
.with_for_update(skip_locked=True)
|
|
).scalar_one_or_none()
|
|
if record is None:
|
|
self.db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=EventErrorDetail.EVENT_LOCKED,
|
|
)
|
|
if record.status == EventStatus.PROCESSED or not self._can_attempt(record):
|
|
self.db.commit()
|
|
return record
|
|
if record.locked_until is not None and record.locked_until > now:
|
|
self.db.commit()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail=EventErrorDetail.EVENT_LOCKED,
|
|
)
|
|
record.locked_by = lock_owner
|
|
record.locked_until = now + timedelta(
|
|
seconds=get_settings().event_dispatch_lock_seconds
|
|
)
|
|
record.status = EventStatus.PENDING
|
|
record.attempts += 1
|
|
self.db.commit()
|
|
self.db.refresh(record)
|
|
return record
|
|
|
|
@staticmethod
|
|
def _max_attempts(record: DomainEvent) -> int:
|
|
return record.max_attempts or get_settings().event_dispatch_max_attempts
|