Skip to main content

TriggerRuleEngine

Struct TriggerRuleEngine 

Source
pub struct TriggerRuleEngine { /* private fields */ }
Expand description

触发规则引擎。

通过 EventBus 的 publish callback 机制监听所有事件,根据配置的规则匹配事件并执行对应的动作。

§生命周期

  1. 创建引擎(new
  2. 注册 action handlers(register_action
  3. 添加规则(add_rule / add_rules
  4. 启动引擎(start)- 注册 publish callback 开始监听事件
  5. 运行时管理规则(update_rule, remove_rule, enable_rule, disable_rule

Implementations§

Source§

impl TriggerRuleEngine

Source

pub fn new(bus: EventBus) -> Self

创建一个新的触发规则引擎。

引擎创建后需要调用 start 开始监听事件。 默认使用 InMemoryRuleStorage(内存存储)。

Source

pub fn with_storage(bus: EventBus, storage: Arc<dyn RuleStorage>) -> Self

使用自定义存储后端创建触发规则引擎。

用于需要持久化规则到数据库或其他存储的场景。

Source

pub fn with_limits(bus: EventBus, limits: ConditionLimits) -> Self

Create a new engine with custom condition evaluation limits.

Source

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.

Source

pub fn storage(&self) -> &Arc<dyn RuleStorage>

获取存储后端引用。

用于高级管理操作,如批量导入/导出规则。

Source

pub fn register_action<F, Fut>(&self, action_type: &str, handler: F)
where F: Fn(TriggerContext) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

注册一个动作处理器。

action_type 对应 TriggerRule::action_type 中配置的值。

Source

pub fn add_rule(&self, rule: TriggerRule)

添加一条触发规则。

Source

pub fn add_rules(&self, new_rules: Vec<TriggerRule>)

批量添加触发规则。

Source

pub fn remove_rule(&self, rule_id: &str) -> Option<TriggerRule>

移除一条规则(按 ID)。

返回被移除的规则(如果存在)。

Source

pub fn update_rule(&self, rule: TriggerRule) -> bool

更新一条规则。

根据 rule.id 查找并替换。

Source

pub fn enable_rule(&self, rule_id: &str) -> bool

启用一条规则。

Source

pub fn disable_rule(&self, rule_id: &str) -> bool

禁用一条规则。

Source

pub fn list_rules(&self) -> Vec<TriggerRule>

获取所有规则。

Source

pub fn get_rule(&self, rule_id: &str) -> Option<TriggerRule>

获取指定 ID 的规则。

Source

pub fn rule_count(&self) -> usize

获取规则数量。

Source

pub fn limits(&self) -> &ConditionLimits

获取当前的条件评估限制配置。

Source

pub fn list_action_types(&self) -> Vec<String>

列出已注册的 action 类型。

Source

pub fn is_running(&self) -> bool

检查引擎是否正在运行。

Source

pub async fn start(&self) -> Result<()>

启动触发规则引擎。

通过 EventBus 的 publish callback 机制监听所有已发布的事件, 当事件的 to_json() 返回 JSON 数据时,自动匹配规则并执行动作。 如果引擎已经在运行,则不做任何操作。

Source

pub fn stop(&self)

停止触发规则引擎。

Sets the running flag to false. Note that already-spawned tasks from publish callbacks may still complete — this is acceptable for now.

Source

pub async fn process_event( &self, event_name: &str, event_data: &Value, ) -> Vec<Result<()>>

处理一个事件,匹配规则并执行动作。

此方法通常在引擎内部自动调用(通过 publish callback), 也可以手动调用用于测试或自定义集成。

Trait Implementations§

Source§

impl Drop for TriggerRuleEngine

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more