plaita.event.core¶
事件系统核心:Event、EventSubscription、EventBus、EventStorage、EventSubscriptionStorage、EventProcessingTracker、RetryPolicy。
core
¶
事件系统的核心定义和接口
Event
¶
Bases: BaseModel
事件对象,包含事件类型、数据和元数据
EventSubscription
¶
Bases: BaseModel
事件订阅信息
mark_event_processed
¶
is_event_processed
¶
matches_event
¶
matches_event(event: Event, context: Dict[str, Any]) -> bool
检查事件是否匹配此订阅
源代码位于: plaita/event/core.py
EventStorage
¶
Bases: ABC
事件存储接口,定义了如何存储和检索事件
list_events
abstractmethod
async
¶
list_events(event_type: Optional[str] = None, start_time: Optional[float] = None, end_time: Optional[float] = None, limit: int = 100) -> List[Event]
delete_event
abstractmethod
async
¶
EventSubscriptionStorage
¶
Bases: ABC
事件订阅存储接口,定义了如何存储和检索事件订阅信息
store_subscription
abstractmethod
async
¶
store_subscription(subscription: EventSubscription) -> str
get_subscription
abstractmethod
async
¶
get_subscription(subscription_id: str) -> Optional[EventSubscription]
list_subscriptions
abstractmethod
async
¶
list_subscriptions(event_type: Optional[str] = None, correlation_id: Optional[str] = None, flow_id: Optional[str] = None, node_id: Optional[str] = None) -> List[EventSubscription]
列出符合条件的事件订阅
delete_subscription
abstractmethod
async
¶
find_matching_subscriptions
async
¶
find_matching_subscriptions(event: Event, context: Dict[str, Any] = None) -> List[EventSubscription]
查找与事件匹配的所有订阅
源代码位于: plaita/event/core.py
mark_event_processed
abstractmethod
async
¶
batch_mark_processed
async
¶
批量标记事件为已处理(默认实现)
源代码位于: plaita/event/core.py
EventProcessingTracker
¶
Bases: ABC
统一的事件处理记录接口,用于全局去重和状态追踪
RetryPolicy
¶
Bases: BaseModel
事件处理重试策略
EventBus
¶
Bases: ABC
事件总线接口,定义了事件发布和订阅的核心功能
matches_event_type
staticmethod
¶
检查事件类型是否匹配处理器的事件类型模式
支持的匹配规则: 1. None 或 "" - 匹配所有事件类型 2. 精确匹配 - "user.login" 只匹配 "user.login" 3. 前缀通配符 - "user." 匹配 "user.login", "user.logout" 等 4. 后缀通配符 - ".login" 匹配 "user.login", "admin.login" 等 5. 中间通配符 - ".user.*" 匹配 "app.user.login", "sys.user.update" 等
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
handler_event_type
|
Optional[str]
|
处理器注册的事件类型模式 |
必需 |
actual_event_type
|
str
|
实际事件类型 |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
bool |
bool
|
是否匹配 |
源代码位于: plaita/event/core.py
publish
abstractmethod
async
¶
publish(event: Union[Event, str, Dict[str, Any]], prevent_duplicate_consumption: bool = True, **kwargs) -> str
batch_publish
async
¶
batch_publish(events: List[Union[Event, str, Dict[str, Any]]], prevent_duplicate_consumption: bool = True) -> List[str]
批量发布事件(默认实现)
源代码位于: plaita/event/core.py
register_subscription
abstractmethod
async
¶
register_subscription(event_type: str, filter_condition: Optional[Dict[str, Any]] = None, correlation_id: Optional[str] = None, flow_id: Optional[str] = None, node_id: Optional[str] = None, timeout: Optional[float] = None, tenant_id: Optional[str] = None) -> str
注册事件订阅信息
源代码位于: plaita/event/core.py
unregister_subscription
abstractmethod
async
¶
wait_for_event
abstractmethod
async
¶
register_handler
abstractmethod
async
¶
register_handler(event_type: Optional[str] = None, handler: EventHandler = None, filter_condition: Optional[Dict[str, Any]] = None, retry_policy: Optional[RetryPolicy] = None) -> str
注册事件处理器
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
event_type
|
Optional[str]
|
事件类型模式,支持通配符: - None 或 "": 监听所有事件 - "prefix.": 监听prefix开头的所有事件 - ".suffix": 监听以suffix结尾的所有事件 - ".middle.*": 监听包含middle的所有事件 |
None
|
handler
|
EventHandler
|
事件处理器函数 |
None
|
filter_condition
|
Optional[Dict[str, Any]]
|
额外的过滤条件 |
None
|
retry_policy
|
Optional[RetryPolicy]
|
重试策略 |
None
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
处理器ID |
源代码位于: plaita/event/core.py
publish_sync
¶
publish_sync(event: Union[Event, str, Dict[str, Any]], prevent_duplicate_consumption: bool = True, **kwargs) -> str
Synchronous publish — delegates to the async publish method.
Detects whether an event loop is already running and uses
_run_async_from_sync to bridge appropriately.
On backends that dispatch handlers from publish (InMemory), the
bridge drains this publish's dispatch tasks — bounded by
publish_sync_drain_timeout — before asyncio.run closes the
private loop. Without that drain a slow async handler is cancelled
by loop shutdown and silently never runs (plaita#218).
publish itself stays fire-and-forget: async callers still return
before handlers finish.
源代码位于: plaita/event/core.py
bind_trusted_tenant
¶
发布时绑定受信租户(plaita#191)。
租户只从进程内 ContextVar 取(worker / console 请求上下文已 set),
绝不读 event.data["tenant_id"]——那是发布者/客户端可控载荷,正是
历史跨租户 resume 注入的来源。已显式设置 tenant_id 的事件不覆盖:
服务内部补偿链(delay / approval / http_callback 的 resume 事件)由发布
方从服务端任务配置显式写入受信租户。
源代码位于: plaita/event/core.py
event_handler
¶
event_handler(event_bus: EventBus, event_type: Optional[str] = None, filter_condition: Optional[Dict[str, Any]] = None, retry_policy: Optional[RetryPolicy] = None)
事件处理器装饰器
注册是异步的 (EventBus.register_handler 是 coroutine)。本装饰器在
模块导入期被触发, 当时未必有 running loop, 直接 asyncio.create_task
会抛 RuntimeError 或 (即使成功) task 引用丢失被 GC 回收。
处理策略:
1. 检测到 running loop —— create_task 并把引用存进模块级集合, 由 loop
自然驱动; 任务完成后从集合移除 (done_callback)。
2. 没 running loop (典型场景: 模块导入期) —— 把 register coroutine 存进
模块级待办列表, 暴露 flush_pending_handler_registrations() 让用户
在 loop 起来后显式 await。
源代码位于: plaita/event/core.py
flush_pending_handler_registrations
async
¶
驱动所有在无 running loop 期间被 @event_handler 排队的注册任务。
典型用法: 应用启动、asyncio.run 入口里 await flush_...() 一次。