-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
250 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/modules/aggregatorStateManager/aggregatorStateManager.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { AggregatorState } from './entities/aggregatorState.entity'; | ||
import { AggregatorStateManagerService } from './aggregatorStateManager.service'; | ||
import { OneTimeJobsManagerService } from './oneTimeJobsManager.service'; | ||
import { AccountAggregationFlowProducer } from '../queueProcessor/services/producers/accountAggregationFlow.producer'; | ||
import { DatasourceChunksParallelHandlingProducer } from '../queueProcessor/services/producers/datasourceChunksParallelHandling.producer'; | ||
import { DatasourceHandlingProducer } from '../queueProcessor/services/producers/datasourceHandling.producer'; | ||
import { registerBullQueues } from '../../modulesConfig/bullModule.forRoot'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([AggregatorState]), registerBullQueues()], | ||
providers: [ | ||
AggregatorStateManagerService, | ||
OneTimeJobsManagerService, | ||
AccountAggregationFlowProducer, | ||
DatasourceChunksParallelHandlingProducer, | ||
DatasourceHandlingProducer, | ||
], | ||
exports: [AggregatorStateManagerService, OneTimeJobsManagerService], | ||
}) | ||
export class AggregatorStateManagerModule {} |
34 changes: 34 additions & 0 deletions
34
src/modules/aggregatorStateManager/aggregatorStateManager.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { In, Repository } from 'typeorm'; | ||
import { AggregatorState } from './entities/aggregatorState.entity'; | ||
|
||
@Injectable() | ||
export class AggregatorStateManagerService { | ||
constructor( | ||
@InjectRepository(AggregatorState) | ||
public readonly aggregatorStateRepository: Repository<AggregatorState>, | ||
) {} | ||
|
||
async getOrCreateAggregatorState() { | ||
let existingState = await this.aggregatorStateRepository.findOne({ | ||
where: { id: '1' }, | ||
}); | ||
if (existingState) return existingState; | ||
existingState = new AggregatorState(); | ||
existingState.id = '1'; | ||
existingState.oneTimeJobs = []; | ||
await this.aggregatorStateRepository.save(existingState); | ||
return existingState; | ||
} | ||
|
||
async updateDataSourcesState(newState: Partial<AggregatorState>) { | ||
const stateEntity = await this.getOrCreateAggregatorState(); | ||
|
||
if ('oneTimeJobs' in newState) | ||
stateEntity.oneTimeJobs = newState.oneTimeJobs; | ||
|
||
await this.aggregatorStateRepository.save(stateEntity); | ||
return stateEntity; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/modules/aggregatorStateManager/entities/aggregatorState.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Column, Entity, PrimaryColumn } from 'typeorm'; | ||
import { Field } from '@nestjs/graphql'; | ||
|
||
@Entity() | ||
export class AggregatorState { | ||
@PrimaryColumn() | ||
id: string; | ||
|
||
@Column('text', { | ||
array: true, | ||
nullable: false, | ||
default: [], | ||
name: 'one_time_jobs', | ||
}) | ||
oneTimeJobs?: string[]; | ||
} |
92 changes: 92 additions & 0 deletions
92
src/modules/aggregatorStateManager/oneTimeJobsManager.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AggregatorStateManagerService } from './aggregatorStateManager.service'; | ||
import { AccountAggregationFlowProducer } from '../queueProcessor/services/producers/accountAggregationFlow.producer'; | ||
import { DatasourceChunksParallelHandlingProducer } from '../queueProcessor/services/producers/datasourceChunksParallelHandling.producer'; | ||
import { DatasourceHandlingProducer } from '../queueProcessor/services/producers/datasourceHandling.producer'; | ||
|
||
type TerminateAllActiveJobsJobPayload = {}; | ||
|
||
type OneTimeJob = { id: string; action: string } & { | ||
payload: TerminateAllActiveJobsJobPayload; | ||
}; | ||
|
||
const oneTimeJobsList: OneTimeJob[] = [ | ||
{ | ||
id: '1707177600', | ||
action: 'activeStakingHandleDailyAggregationJob', | ||
payload: {}, | ||
}, | ||
]; | ||
|
||
@Injectable() | ||
export class OneTimeJobsManagerService { | ||
constructor( | ||
public aggregatorStateManagerService: AggregatorStateManagerService, | ||
public accountAggregationFlowProducer: AccountAggregationFlowProducer, | ||
public datasourceChunksParallelHandlingProducer: DatasourceChunksParallelHandlingProducer, | ||
public datasourceHandlingProducer: DatasourceHandlingProducer, | ||
) {} | ||
|
||
async runOneTimeJobs() { | ||
const migrationsMap = new Map( | ||
oneTimeJobsList.map((item) => [item.id, item]), | ||
); | ||
let pendingMigrationIds: string[] = []; | ||
const aggregatorState = | ||
await this.aggregatorStateManagerService.getOrCreateAggregatorState(); | ||
|
||
if ( | ||
!aggregatorState.oneTimeJobs || | ||
aggregatorState.oneTimeJobs.length === 0 | ||
) { | ||
pendingMigrationIds = [...migrationsMap.keys()]; | ||
} else { | ||
for (const item of aggregatorState.oneTimeJobs) { | ||
migrationsMap.delete(item); | ||
} | ||
pendingMigrationIds = [...migrationsMap.keys()]; | ||
} | ||
|
||
if (!pendingMigrationIds || pendingMigrationIds.length === 0) { | ||
console.log(`OneTime Jobs :: No pending OneTime Job found.`); | ||
return; | ||
} | ||
|
||
const processedJobs = []; | ||
|
||
console.log('pendingOneTimeJobIds - ', pendingMigrationIds); | ||
|
||
for (const jobId of pendingMigrationIds) { | ||
const jobDetails = migrationsMap.get(jobId); | ||
|
||
switch (jobDetails.action) { | ||
case 'activeStakingHandleDailyAggregationJob': { | ||
try { | ||
await this.accountAggregationFlowProducer.removeAllActiveJobs(); | ||
await this.datasourceChunksParallelHandlingProducer.removeAllActiveJobs(); | ||
await this.datasourceHandlingProducer.removeAllActiveJobs(); | ||
|
||
processedJobs.push(jobId); | ||
console.log( | ||
`OneTime Jobs :: OneTime Job has been completed with details: [id: ${jobDetails.id} // action: ${jobDetails.action}]`, | ||
); | ||
} catch (e) { | ||
console.log( | ||
`OneTime Jobs :: OneTime Job ${jobId} has been processed with error.`, | ||
); | ||
console.log(e); | ||
} | ||
break; | ||
} | ||
|
||
default: | ||
} | ||
} | ||
|
||
await this.aggregatorStateManagerService.updateDataSourcesState({ | ||
oneTimeJobs: [ | ||
...new Set([...aggregatorState.oneTimeJobs, ...processedJobs]).values(), | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { BlockchainService } from '../modules/entities/blockchain/blockchain.service'; | ||
import { OneTimeJobsManagerService } from '../modules/aggregatorStateManager/oneTimeJobsManager.service'; | ||
|
||
@Injectable() | ||
export class CommonBootstrapperService implements OnApplicationBootstrap { | ||
constructor(private blockchainService: BlockchainService) {} | ||
constructor( | ||
private blockchainService: BlockchainService, | ||
private oneTimeJobsManagerService: OneTimeJobsManagerService, | ||
) {} | ||
|
||
async onApplicationBootstrap(): Promise<void> { | ||
await this.blockchainService.initSupportedBlockchains(); | ||
|
||
await this.oneTimeJobsManagerService.runOneTimeJobs(); | ||
} | ||
} |
Oops, something went wrong.