- 迁移 084: domain_events_archive 归档表 + cleanup_old_published_events() - 迁移 085: processed_events 去重表 + cleanup_old_processed_events() - erp-core: is_event_processed() / mark_event_processed() 幂等性辅助 - erp-server: tasks::start_event_cleanup() 每 24h 归档 >90 天事件
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
use std::time::Duration;
|
|
|
|
/// 启动事件清理后台任务。
|
|
///
|
|
/// 每日执行一次:
|
|
/// - 调用 `cleanup_old_published_events()` 归档 >90 天的已发布事件
|
|
/// - 调用 `cleanup_old_processed_events()` 清理 >7 天的去重记录
|
|
pub fn start_event_cleanup(db: sea_orm::DatabaseConnection) {
|
|
tokio::spawn(async move {
|
|
let mut interval = tokio::time::interval(Duration::from_secs(86400));
|
|
loop {
|
|
interval.tick().await;
|
|
if let Err(e) = run_cleanup(&db).await {
|
|
tracing::warn!(error = %e, "事件清理任务执行失败");
|
|
}
|
|
}
|
|
});
|
|
tracing::info!("事件清理任务已启动(每 24 小时执行一次)");
|
|
}
|
|
|
|
async fn run_cleanup(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbErr> {
|
|
use sea_orm::ConnectionTrait;
|
|
|
|
// 归档 >90 天的已发布事件
|
|
match db
|
|
.execute_unprepared("SELECT cleanup_old_published_events(90, 1000)")
|
|
.await
|
|
{
|
|
Ok(result) => {
|
|
tracing::info!(
|
|
rows_affected = result.rows_affected(),
|
|
"已发布事件归档完成"
|
|
);
|
|
}
|
|
Err(e) => tracing::warn!(error = %e, "已发布事件归档失败"),
|
|
}
|
|
|
|
// 清理 >7 天的去重记录
|
|
match db
|
|
.execute_unprepared("SELECT cleanup_old_processed_events(7, 1000)")
|
|
.await
|
|
{
|
|
Ok(result) => {
|
|
tracing::info!(
|
|
rows_affected = result.rows_affected(),
|
|
"去重记录清理完成"
|
|
);
|
|
}
|
|
Err(e) => tracing::warn!(error = %e, "去重记录清理失败"),
|
|
}
|
|
|
|
Ok(())
|
|
}
|