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
Aggregator- 聚合任务到组中进行批量处理Forwarder- 转发已到期的调度任务和重试任务Healthcheck- 执行健康检查Heartbeat- 发送心跳以维护服务器状态Janitor- 清理过期任务和死亡服务器PeriodicTaskManager- 管理周期性任务Recoverer- 恢复孤儿任务Subscriber- 订阅任务事件
§注意 / 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 selfshutdown()是异步的 /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§
Sourcefn start(self: Arc<Self>) -> JoinHandle<()>
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