refactor(health): 集中管理事件类型常量 + 积分过期发布事件
- event.rs 新增 20 个事件类型常量(PATIENT_CREATED 等) - 10 个 service 文件引用常量替代硬编码字符串 - expire_points 增加 EventBus 参数,处理完成后发布 points.expired 事件 - start_points_expiration_checker 传入 EventBus
This commit is contained in:
@@ -41,13 +41,13 @@ impl HealthModule {
|
||||
}
|
||||
|
||||
/// 启动积分过期清理(每 24 小时运行一次),返回 JoinHandle 用于优雅关闭
|
||||
pub fn start_points_expiration_checker(db: sea_orm::DatabaseConnection) -> tokio::task::JoinHandle<()> {
|
||||
pub fn start_points_expiration_checker(db: sea_orm::DatabaseConnection, event_bus: erp_core::events::EventBus) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(24 * 3600));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
match crate::service::points_service::expire_points(&db).await {
|
||||
match crate::service::points_service::expire_points(&db, &event_bus).await {
|
||||
Ok(count) if count > 0 => tracing::info!(count = count, "积分过期清理完成"),
|
||||
Ok(_) => {}
|
||||
Err(e) => tracing::warn!(error = %e, "积分过期清理失败"),
|
||||
@@ -641,15 +641,16 @@ impl ErpModule for HealthModule {
|
||||
// 启动积分过期清理(启动时执行一次 + 每 24 小时重复)
|
||||
{
|
||||
let db = ctx.db.clone();
|
||||
let event_bus = ctx.event_bus.clone();
|
||||
tokio::spawn(async move {
|
||||
match crate::service::points_service::expire_points(&db).await {
|
||||
match crate::service::points_service::expire_points(&db, &event_bus).await {
|
||||
Ok(count) if count > 0 => tracing::info!(count = count, "启动时积分过期清理完成"),
|
||||
Ok(_) => tracing::info!("启动时积分过期清理完成(无过期积分)"),
|
||||
Err(e) => tracing::warn!(error = %e, "启动时积分过期清理失败"),
|
||||
}
|
||||
});
|
||||
}
|
||||
let _expire_handle = Self::start_points_expiration_checker(ctx.db.clone());
|
||||
let _expire_handle = Self::start_points_expiration_checker(ctx.db.clone(), ctx.event_bus.clone());
|
||||
tracing::info!(module = "health", "Points expiration checker started");
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user