feat(mining-admin): implement transactional idempotent consumer for 100% exactly-once semantics

- Use Prisma $transaction with Serializable isolation level
- Insert idempotency record FIRST, then execute business logic
- Unique constraint violation (P2002) indicates duplicate event
- All operations atomic - either fully commit or fully rollback
- Modified all handlers to accept transaction client parameter
- Removed old non-atomic isEventProcessed/recordProcessedEvent methods

This ensures 100% data consistency for CDC synchronization, which is
critical for financial data where any error is catastrophic.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-12 19:11:30 -08:00
parent 577f626972
commit 70135938c4
3 changed files with 881 additions and 1157 deletions

View File

@ -48,6 +48,8 @@ export interface ServiceEvent {
export type CdcHandler = (event: CdcEvent) => Promise<void>;
export type ServiceEventHandler = (event: ServiceEvent) => Promise<void>;
/** 支持事务的 handler 类型tx 参数为 Prisma 事务客户端 */
export type TransactionalServiceEventHandler = (event: ServiceEvent, tx: any) => Promise<void>;
@Injectable()
export class CdcConsumerService implements OnModuleInit, OnModuleDestroy {