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响应模型以提供 更准确的数据类型定义。 ```
116 lines
3.8 KiB
Python
116 lines
3.8 KiB
Python
from typing import Any
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import func, select
|
|
|
|
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.events.constants import (
|
|
EVENT_CODE_PREFIX,
|
|
EventErrorDetail,
|
|
)
|
|
from app.modules.events.models import DomainEvent
|
|
from app.modules.events.services.serialization import _serialize_event
|
|
|
|
|
|
class EventQueryMixin:
|
|
def enqueue(
|
|
self,
|
|
event_type: str,
|
|
source: str,
|
|
aggregate_type: str,
|
|
aggregate_id: str | int | None,
|
|
actor: str = ActorValue.SYSTEM,
|
|
payload: dict[str, Any] | None = None,
|
|
idempotency_key: str | None = None,
|
|
) -> DomainEvent:
|
|
"""Stage an outbox event in the caller's transaction."""
|
|
|
|
existing = self._find_idempotent_event(idempotency_key)
|
|
if existing is not None:
|
|
return existing
|
|
|
|
settings = get_settings()
|
|
now = utc_now()
|
|
record = DomainEvent(
|
|
event_id=f"{EVENT_CODE_PREFIX}-{now:%Y%m%d%H%M%S%f}",
|
|
event_type=event_type,
|
|
source=source,
|
|
aggregate_type=aggregate_type,
|
|
aggregate_id=str(aggregate_id) if aggregate_id is not None else None,
|
|
actor=actor,
|
|
payload=payload or {},
|
|
idempotency_key=idempotency_key,
|
|
next_attempt_at=now,
|
|
max_attempts=settings.event_dispatch_max_attempts,
|
|
)
|
|
self.db.add(record)
|
|
self.db.flush()
|
|
return record
|
|
|
|
def emit(
|
|
self,
|
|
event_type: str,
|
|
source: str,
|
|
aggregate_type: str,
|
|
aggregate_id: str | int | None,
|
|
actor: str = ActorValue.SYSTEM,
|
|
payload: dict[str, Any] | None = None,
|
|
idempotency_key: str | None = None,
|
|
) -> DomainEvent:
|
|
existing = self._find_idempotent_event(idempotency_key)
|
|
if existing is not None:
|
|
return existing
|
|
|
|
record = self.enqueue(
|
|
event_type=event_type,
|
|
source=source,
|
|
aggregate_type=aggregate_type,
|
|
aggregate_id=aggregate_id,
|
|
actor=actor,
|
|
payload=payload,
|
|
idempotency_key=idempotency_key,
|
|
)
|
|
self.db.commit()
|
|
self.db.refresh(record)
|
|
return record
|
|
|
|
def _find_idempotent_event(self, idempotency_key: str | None) -> DomainEvent | None:
|
|
if not idempotency_key:
|
|
return None
|
|
return self.db.execute(
|
|
select(DomainEvent).where(DomainEvent.idempotency_key == idempotency_key)
|
|
).scalar_one_or_none()
|
|
|
|
def list_events(
|
|
self,
|
|
status_filter: str | None = None,
|
|
event_type: str | None = None,
|
|
limit: int = 100,
|
|
) -> list[dict[str, Any]]:
|
|
stmt = select(DomainEvent).order_by(DomainEvent.id.desc()).limit(bounded_limit(limit))
|
|
if status_filter:
|
|
stmt = stmt.where(DomainEvent.status == status_filter)
|
|
if event_type:
|
|
stmt = stmt.where(DomainEvent.event_type == event_type)
|
|
return [_serialize_event(item) for item in self.db.execute(stmt).scalars()]
|
|
|
|
def count_by_status(self) -> dict[str, int]:
|
|
rows = self.db.execute(
|
|
select(DomainEvent.status, func.count()).group_by(DomainEvent.status)
|
|
).all()
|
|
return {str(status_value): int(count) for status_value, count in rows}
|
|
|
|
def get_event(self, event_id: str) -> DomainEvent:
|
|
record = self.db.execute(
|
|
select(DomainEvent).where(DomainEvent.event_id == event_id)
|
|
).scalar_one_or_none()
|
|
if record is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=EventErrorDetail.EVENT_NOT_FOUND,
|
|
)
|
|
return record
|