tdm_server_rust/runner/mod.rs
1//! 启动时一次性任务 (Startup Runners)
2//!
3//! 对齐 Java Spring `CommandLineRunner`,在应用启动后、接受请求前执行。
4//!
5//! ## 执行顺序
6//!
7//! 1. [`password_encrypt_runner::run`] — 明文密码批量 bcrypt 加密
8//! 2. [`test_member_seed_runner::run`] — 预置测试角色账号
9//!
10//! ## 幂等性
11//!
12//! 两个 runner 均为幂等操作:
13//! - 密码加密:跳过已加密的密码(以 `$2a$` 或 `$2b$` 开头)
14//! - 测试种子:检测账号是否存在,存在则跳过
15
16pub mod password_encrypt_runner;
17pub mod test_member_seed_runner;
18
19use sqlx::MySqlPool;
20
21/// 执行所有启动任务
22///
23/// 在 `AppState::new()` 中数据库连接池建立后立即调用。
24/// 任一任务失败都会阻止应用启动。
25///
26/// # 参数
27///
28/// - `pool`: 数据库连接池
29///
30/// # Errors
31///
32/// 任一 runner 失败时返回错误,应用不会继续启动。
33pub async fn run_all(pool: &MySqlPool) -> anyhow::Result<()> {
34 password_encrypt_runner::run(pool).await?;
35 test_member_seed_runner::run(pool).await?;
36 Ok(())
37}