feat(mining): 自动同步全网理论算力
- 启动时自动从 contribution-service 同步全网数据 - 每分钟定时同步全网理论算力和系统账户算力 - 使用分布式锁防止并发同步 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
7909bcc3d1
commit
23bb8baa9c
|
|
@ -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<void> {
|
||||
const lockValue = await this.redis.acquireLock('mining:network-sync:lock', 30);
|
||||
if (!lockValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const contributionServiceUrl = this.configService.get<string>(
|
||||
'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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue