Skip to main content

ComponentLifecycle

Trait ComponentLifecycle 

Source
pub trait ComponentLifecycle {
    // Required methods
    fn start(self: Arc<Self>) -> JoinHandle<()>;
    fn shutdown(&self);
    fn is_done(&self) -> bool;
}
Expand description

Lifecycle trait - 组件生命周期管理接口 Lifecycle trait - Component components management interface

此 trait 定义了组件的基本生命周期操作:启动、关闭和状态检查 This trait defines the basic components operations for components: start, shutdown, and state check

§实现者 / Implementors

§注意 / Note

Processor 没有实现此 trait,因为它具有不同的接口: Processor does not implement this trait because it has a different interface:

  • 需要泛型 Handler 参数 / Requires a generic Handler parameter
  • start() 方法接受 &mut self / start() method takes &mut self
  • shutdown() 是异步的 / shutdown() is async

§示例 / Example

use asynq::components::ComponentLifecycle;
use asynq::components::janitor::{Janitor, JanitorConfig};
use std::sync::Arc;
  #[cfg(feature = "default")]

// 启动组件
// Start component
let handle = janitor.clone().start();

// 检查状态
// Check state
assert!(!janitor.is_done());

// 关闭组件
// Shutdown component
janitor.shutdown();
assert!(janitor.is_done());

Required Methods§

Source

fn start(self: Arc<Self>) -> JoinHandle<()>

启动组件 Start the component

此方法启动组件的后台任务,返回一个 JoinHandle 用于等待任务完成 This method starts the component’s background task, returning a JoinHandle to wait for completion

§返回 / Returns

返回一个 JoinHandle<()>,可用于等待组件任务完成 Returns a JoinHandle<()> that can be used to wait for the component task to complete

Source

fn shutdown(&self)

关闭组件 Shutdown the component

此方法发送关闭信号给组件,组件会在完成当前操作后停止 This method sends a shutdown signal to the component, which will stop after completing current operations

Source

fn is_done(&self) -> bool

检查组件是否已完成 Check if the component is done

§返回 / Returns

如果组件已停止返回 true,否则返回 false Returns true if the component has stopped, otherwise returns false

Implementors§