feat(core): 事件归档 + 消费者幂等性 — 迁移 084/085 + 清理任务
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

- 迁移 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 天事件
This commit is contained in:
iven
2026-04-27 18:12:43 +08:00
parent 97bb592688
commit 3197dde33c
8 changed files with 279 additions and 1 deletions

View File

@@ -83,6 +83,8 @@ mod m20260427_000080_create_medication_record;
mod m20260427_000081_create_dialysis_prescription;
mod m20260427_000082_seed_ai_prompts;
mod m20260427_000083_create_follow_up_template;
mod m20260427_000084_domain_events_cleanup;
mod m20260427_000085_processed_events;
pub struct Migrator;
@@ -173,6 +175,8 @@ impl MigratorTrait for Migrator {
Box::new(m20260427_000081_create_dialysis_prescription::Migration),
Box::new(m20260427_000082_seed_ai_prompts::Migration),
Box::new(m20260427_000083_create_follow_up_template::Migration),
Box::new(m20260427_000084_domain_events_cleanup::Migration),
Box::new(m20260427_000085_processed_events::Migration),
]
}
}

View File

@@ -0,0 +1,89 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 归档表 — 与 domain_events 结构相同,用于存放 >90 天的已发布事件
manager
.create_table(
Table::create()
.table(Alias::new("domain_events_archive"))
.if_not_exists()
.col(ColumnDef::new(Alias::new("id")).uuid().not_null().primary_key())
.col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("event_type")).string_len(200).not_null())
.col(ColumnDef::new(Alias::new("payload")).json().null())
.col(ColumnDef::new(Alias::new("correlation_id")).uuid().null())
.col(ColumnDef::new(Alias::new("status")).string_len(20).not_null())
.col(ColumnDef::new(Alias::new("attempts")).integer().not_null().default(0))
.col(ColumnDef::new(Alias::new("last_error")).text().null())
.col(ColumnDef::new(Alias::new("created_at")).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(Alias::new("published_at")).timestamp_with_time_zone().null())
.col(ColumnDef::new(Alias::new("archived_at")).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_domain_events_archive_created")
.table(Alias::new("domain_events_archive"))
.col(Alias::new("created_at"))
.to_owned(),
)
.await?;
// 清理函数:将 >90 天的已发布事件迁移到归档表
manager
.get_connection()
.execute_unprepared(
r#"
CREATE OR REPLACE FUNCTION cleanup_old_published_events(
retention_days INT DEFAULT 90,
batch_size INT DEFAULT 1000
) RETURNS INT AS $$
DECLARE
moved_count INT;
BEGIN
INSERT INTO domain_events_archive (id, tenant_id, event_type, payload, correlation_id, status, attempts, last_error, created_at, published_at)
SELECT id, tenant_id, event_type, payload, correlation_id, status, attempts, last_error, created_at, published_at
FROM domain_events
WHERE status = 'published'
AND published_at < NOW() - (retention_days || ' days')::INTERVAL
ORDER BY created_at ASC
LIMIT batch_size;
GET DIAGNOSTICS moved_count = ROW_COUNT;
DELETE FROM domain_events
WHERE status = 'published'
AND published_at < NOW() - (retention_days || ' days')::INTERVAL
LIMIT batch_size;
RETURN moved_count;
END;
$$ LANGUAGE plpgsql;
"#,
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared("DROP FUNCTION IF EXISTS cleanup_old_published_events(INT, INT);")
.await?;
manager
.drop_table(Table::drop().table(Alias::new("domain_events_archive")).to_owned())
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,61 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Alias::new("processed_events"))
.if_not_exists()
.col(ColumnDef::new(Alias::new("event_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("consumer_id")).string_len(200).not_null())
.col(ColumnDef::new(Alias::new("processed_at")).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
.primary_key(Index::create().col(Alias::new("event_id")).col(Alias::new("consumer_id")))
.to_owned(),
)
.await?;
// 7 天 TTL 清理函数
manager
.get_connection()
.execute_unprepared(
r#"
CREATE OR REPLACE FUNCTION cleanup_old_processed_events(
retention_days INT DEFAULT 7,
batch_size INT DEFAULT 1000
) RETURNS INT AS $$
DECLARE
deleted_count INT;
BEGIN
DELETE FROM processed_events
WHERE processed_at < NOW() - (retention_days || ' days')::INTERVAL
LIMIT batch_size;
GET DIAGNOSTICS deleted_count = ROW_COUNT;
RETURN deleted_count;
END;
$$ LANGUAGE plpgsql;
"#,
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.get_connection()
.execute_unprepared("DROP FUNCTION IF EXISTS cleanup_old_processed_events(INT, INT);")
.await?;
manager
.drop_table(Table::drop().table(Alias::new("processed_events")).to_owned())
.await?;
Ok(())
}
}