1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Lifecycle 模块
//! Lifecycle module
//!
//! 提供通用的组件生命周期管理 trait
//! Provides a common trait for component components management
//!
//! 此模块定义了统一的生命周期接口,用于管理各种后台组件(如 Aggregator、Forwarder 等)
//! This module defines a unified components interface for managing various background components
//! (such as Aggregator, Forwarder, etc.)
use Arc;
use JoinHandle;
/// 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`](aggregator::Aggregator) - 聚合任务到组中进行批量处理
/// - [`Forwarder`](forwarder::Forwarder) - 转发已到期的调度任务和重试任务
/// - [`Healthcheck`](healthcheck::Healthcheck) - 执行健康检查
/// - [`Heartbeat`](heartbeat::Heartbeat) - 发送心跳以维护服务器状态
/// - [`Janitor`](janitor::Janitor) - 清理过期任务和死亡服务器
/// - [`PeriodicTaskManager`](periodic_task_manager::PeriodicTaskManager) - 管理周期性任务
/// - [`Recoverer`](recoverer::Recoverer) - 恢复孤儿任务
/// - [`Subscriber`](subscriber::Subscriber) - 订阅任务事件
///
/// # 注意 / Note
///
/// [`Processor`](processor::Processor) 没有实现此 trait,因为它具有不同的接口:
/// [`Processor`](processor::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
///
/// ```rust,no_run
/// use asynq::components::ComponentLifecycle;
/// use asynq::components::janitor::{Janitor, JanitorConfig};
/// use std::sync::Arc;
/// #[cfg(feature = "default")]
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # use asynq::backend::RedisConnectionType;
/// # use asynq::backend::RedisBroker;
/// # let redis_config = asynq::backend::RedisConnectionType::single("redis://localhost:6379")?;
/// # let broker = Arc::new(RedisBroker::new(redis_config).await?);
/// # let janitor = Arc::new(Janitor::new(broker, JanitorConfig::default()));
///
/// // 启动组件
/// // Start component
/// let handle = janitor.clone().start();
///
/// // 检查状态
/// // Check state
/// assert!(!janitor.is_done());
///
/// // 关闭组件
/// // Shutdown component
/// janitor.shutdown();
/// assert!(janitor.is_done());
/// # Ok(())
/// # }
/// ```