From 23bb8baa9c0499a74e7619da1b969435f93f14c4 Mon Sep 17 00:00:00 2001 From: hailin Date: Fri, 16 Jan 2026 04:44:27 -0800 Subject: [PATCH] =?UTF-8?q?feat(mining):=20=E8=87=AA=E5=8A=A8=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=85=A8=E7=BD=91=E7=90=86=E8=AE=BA=E7=AE=97=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 启动时自动从 contribution-service 同步全网数据 - 每分钟定时同步全网理论算力和系统账户算力 - 使用分布式锁防止并发同步 Co-Authored-By: Claude Opus 4.5 --- .../schedulers/mining.scheduler.ts | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/backend/services/mining-service/src/application/schedulers/mining.scheduler.ts b/backend/services/mining-service/src/application/schedulers/mining.scheduler.ts index c21350ff..a393fbee 100644 --- a/backend/services/mining-service/src/application/schedulers/mining.scheduler.ts +++ b/backend/services/mining-service/src/application/schedulers/mining.scheduler.ts @@ -1,6 +1,8 @@ import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { Cron, CronExpression } from '@nestjs/schedule'; import { MiningDistributionService } from '../services/mining-distribution.service'; +import { NetworkSyncService } from '../services/network-sync.service'; import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service'; import { RedisService } from '../../infrastructure/redis/redis.service'; @@ -10,12 +12,16 @@ export class MiningScheduler implements OnModuleInit { constructor( private readonly distributionService: MiningDistributionService, + private readonly networkSyncService: NetworkSyncService, private readonly prisma: PrismaService, private readonly redis: RedisService, + private readonly configService: ConfigService, ) {} - onModuleInit() { + async onModuleInit() { this.logger.log('Mining scheduler initialized'); + // 启动时同步全网数据 + await this.syncNetworkData(); } /** @@ -110,4 +116,33 @@ export class MiningScheduler implements OnModuleInit { this.logger.error('Failed to cleanup old minute stats', error); } } + + /** + * 每分钟同步全网数据(系统账户算力、全网理论算力) + */ + @Cron(CronExpression.EVERY_MINUTE) + async syncNetworkData(): Promise { + const lockValue = await this.redis.acquireLock('mining:network-sync:lock', 30); + if (!lockValue) { + return; + } + + try { + const contributionServiceUrl = this.configService.get( + 'CONTRIBUTION_SERVICE_URL', + 'http://localhost:3020', + ); + + const result = await this.networkSyncService.syncFromContributionService(contributionServiceUrl); + if (result.success) { + this.logger.debug(`Network sync completed: ${result.message}`); + } else { + this.logger.warn(`Network sync failed: ${result.message}`); + } + } catch (error) { + this.logger.error('Failed to sync network data', error); + } finally { + await this.redis.releaseLock('mining:network-sync:lock', lockValue); + } + } }