Skip to main content

Plugin

Trait Plugin 

Source
pub trait Plugin: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn start<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn stop<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn depends_on(&self) -> &[&str] { ... }
}
Expand description

插件生命周期:start/stop 对称

极简设计——只定义 start / stop 两个生命周期方法。 Rust 版增强:异步化 + 依赖声明 + 拓扑排序启动。

§示例

use alun_core::plugin::Plugin;

struct DbPlugin { pool: PgPool }

#[async_trait]
impl Plugin for DbPlugin {
    fn name(&self) -> &str { "pg" }

    async fn start(&self) -> Result<()> {
        sqlx::query("SELECT 1").fetch_one(&self.pool).await?;
        Ok(())
    }

    async fn stop(&self) -> Result<()> {
        self.pool.close().await;
        Ok(())
    }
}

Required Methods§

Source

fn name(&self) -> &str

插件唯一名称(用于注册、日志、依赖解析)

Source

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

启动插件:验证连接、初始化资源

PluginManager::start_all() 中被调用,按拓扑顺序依次执行。 若返回 Err,则启动流程中止,后续插件不会执行。

Source

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

关闭插件:释放资源

PluginManager::stop_all() 中被调用,按逆序执行。 即使返回 Err,也会继续关闭其余插件(仅记录日志)。

Provided Methods§

Source

fn depends_on(&self) -> &[&str]

依赖的其他插件名称(用于拓扑排序,保证启动顺序)

默认返回空数组(无依赖)。

Implementors§