Skip to main content

Plugin

Trait Plugin 

Source
pub trait Plugin: Send + Sync {
    // Required method
    fn name(&self) -> &str;

    // Provided methods
    fn on_startup<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_task_start<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_task_stop<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_shutdown<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_error<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
        error: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

插件抽象

支持在关键生命周期事件注入自定义逻辑

§实现说明

  • 使用 async-trait 宏支持 trait objects
  • 所有插件必须实现此 trait
  • 插件可以在运行时启动、任务启动/停止、停机等事件时执行

§示例

ⓘ
use flare_core_runtime::plugin::{Plugin, PluginContext};
use flare_core_runtime::error::PluginError;

struct LoggingPlugin;

impl Plugin for LoggingPlugin {
    fn name(&self) -> &str {
        "logging"
    }

    async fn on_startup(&self, ctx: &PluginContext) -> Result<(), PluginError> {
        tracing::info!("Runtime starting: {}", ctx.runtime_name);
        Ok(())
    }

    async fn on_shutdown(&self, ctx: &PluginContext) -> Result<(), PluginError> {
        tracing::info!("Runtime shutting down: {}", ctx.runtime_name);
        Ok(())
    }
}

Required Methods§

Source

fn name(&self) -> &str

获取插件名称

Provided Methods§

Source

fn on_startup<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

运行时启动时调用

§参数
  • ctx - 插件上下文
§返回

成功返回 Ok(()),失败返回 PluginError

Source

fn on_task_start<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

任务启动前调用

§参数
  • ctx - 插件上下文(包含任务名称和状态)
§返回

成功返回 Ok(()),失败返回 PluginError

Source

fn on_task_stop<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

任务停止后调用

§参数
  • ctx - 插件上下文(包含任务名称和状态)
§返回

成功返回 Ok(()),失败返回 PluginError

Source

fn on_shutdown<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

运行时停机时调用

§参数
  • ctx - 插件上下文
§返回

成功返回 Ok(()),失败返回 PluginError

Source

fn on_error<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, error: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

错误发生时调用

§参数
  • ctx - 插件上下文
  • error - 错误信息
§返回

成功返回 Ok(()),失败返回 PluginError

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§