pub struct TriggerRuleEngine { /* private fields */ }Expand description
触发规则引擎。
通过 EventBus 的 publish callback 机制监听所有事件,根据配置的规则匹配事件并执行对应的动作。
§生命周期
- 创建引擎(
new) - 注册 action handlers(
register_action) - 添加规则(
add_rule/add_rules) - 启动引擎(
start)- 注册 publish callback 开始监听事件 - 运行时管理规则(
update_rule,remove_rule,enable_rule,disable_rule)
Implementations§
Source§impl TriggerRuleEngine
impl TriggerRuleEngine
Sourcepub fn new(bus: EventBus) -> Self
pub fn new(bus: EventBus) -> Self
创建一个新的触发规则引擎。
引擎创建后需要调用 start 开始监听事件。
默认使用 InMemoryRuleStorage(内存存储)。
Sourcepub fn with_storage(bus: EventBus, storage: Arc<dyn RuleStorage>) -> Self
pub fn with_storage(bus: EventBus, storage: Arc<dyn RuleStorage>) -> Self
使用自定义存储后端创建触发规则引擎。
用于需要持久化规则到数据库或其他存储的场景。
Sourcepub fn with_limits(bus: EventBus, limits: ConditionLimits) -> Self
pub fn with_limits(bus: EventBus, limits: ConditionLimits) -> Self
Create a new engine with custom condition evaluation limits.
Sourcepub fn with_storage_and_limits(
bus: EventBus,
storage: Arc<dyn RuleStorage>,
limits: ConditionLimits,
) -> Self
pub fn with_storage_and_limits( bus: EventBus, storage: Arc<dyn RuleStorage>, limits: ConditionLimits, ) -> Self
Create a new engine with custom storage and condition evaluation limits.
Sourcepub fn storage(&self) -> &Arc<dyn RuleStorage> ⓘ
pub fn storage(&self) -> &Arc<dyn RuleStorage> ⓘ
获取存储后端引用。
用于高级管理操作,如批量导入/导出规则。
Sourcepub fn register_action<F, Fut>(&self, action_type: &str, handler: F)
pub fn register_action<F, Fut>(&self, action_type: &str, handler: F)
注册一个动作处理器。
action_type 对应 TriggerRule::action_type 中配置的值。
Sourcepub fn add_rule(&self, rule: TriggerRule)
pub fn add_rule(&self, rule: TriggerRule)
添加一条触发规则。
Sourcepub fn add_rules(&self, new_rules: Vec<TriggerRule>)
pub fn add_rules(&self, new_rules: Vec<TriggerRule>)
批量添加触发规则。
Sourcepub fn remove_rule(&self, rule_id: &str) -> Option<TriggerRule>
pub fn remove_rule(&self, rule_id: &str) -> Option<TriggerRule>
移除一条规则(按 ID)。
返回被移除的规则(如果存在)。
Sourcepub fn update_rule(&self, rule: TriggerRule) -> bool
pub fn update_rule(&self, rule: TriggerRule) -> bool
更新一条规则。
根据 rule.id 查找并替换。
Sourcepub fn enable_rule(&self, rule_id: &str) -> bool
pub fn enable_rule(&self, rule_id: &str) -> bool
启用一条规则。
Sourcepub fn disable_rule(&self, rule_id: &str) -> bool
pub fn disable_rule(&self, rule_id: &str) -> bool
禁用一条规则。
Sourcepub fn list_rules(&self) -> Vec<TriggerRule>
pub fn list_rules(&self) -> Vec<TriggerRule>
获取所有规则。
Sourcepub fn get_rule(&self, rule_id: &str) -> Option<TriggerRule>
pub fn get_rule(&self, rule_id: &str) -> Option<TriggerRule>
获取指定 ID 的规则。
Sourcepub fn rule_count(&self) -> usize
pub fn rule_count(&self) -> usize
获取规则数量。
Sourcepub fn limits(&self) -> &ConditionLimits
pub fn limits(&self) -> &ConditionLimits
获取当前的条件评估限制配置。
Sourcepub fn list_action_types(&self) -> Vec<String>
pub fn list_action_types(&self) -> Vec<String>
列出已注册的 action 类型。
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
检查引擎是否正在运行。
Sourcepub async fn start(&self) -> Result<()>
pub async fn start(&self) -> Result<()>
启动触发规则引擎。
通过 EventBus 的 publish callback 机制监听所有已发布的事件,
当事件的 to_json() 返回 JSON 数据时,自动匹配规则并执行动作。
如果引擎已经在运行,则不做任何操作。
Trait Implementations§
Source§impl Drop for TriggerRuleEngine
impl Drop for TriggerRuleEngine
Auto Trait Implementations§
impl !RefUnwindSafe for TriggerRuleEngine
impl !UnwindSafe for TriggerRuleEngine
impl Freeze for TriggerRuleEngine
impl Send for TriggerRuleEngine
impl Sync for TriggerRuleEngine
impl Unpin for TriggerRuleEngine
impl UnsafeUnpin for TriggerRuleEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more