generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: retroactively handle strategy #45
Open
0xnigir1
wants to merge
2
commits into
dev
Choose a base branch
from
feat/retroactively-handle-strategy
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { inspect } from "util"; | ||
|
||
import { environment } from "./config/index.js"; | ||
import { ProcessingService } from "./services/processing.service.js"; | ||
|
||
let processor: ProcessingService; | ||
|
||
const main = async (): Promise<void> => { | ||
processor = await ProcessingService.initialize(environment); | ||
await processor.processRetroactiveEvents(); | ||
}; | ||
|
||
process.on("unhandledRejection", (reason, p) => { | ||
console.error(`Unhandled Rejection at: \n${inspect(p, undefined, 100)}, \nreason: ${reason}`); | ||
process.exit(1); | ||
}); | ||
|
||
process.on("uncaughtException", (error: Error) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Error or unknown? 🤔 |
||
console.error( | ||
`An uncaught exception occurred: ${error}\n` + `Exception origin: ${error.stack}`, | ||
); | ||
process.exit(1); | ||
}); | ||
|
||
main() | ||
.catch((err) => { | ||
console.error(`Caught error in main handler: ${err}`); | ||
process.exit(1); | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
.finally(async () => { | ||
await processor?.releaseResources(); | ||
}); |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ import { | |
InMemoryCachedEventRegistry, | ||
InMemoryCachedStrategyRegistry, | ||
Orchestrator, | ||
RetroactiveProcessor, | ||
} from "@grants-stack-indexer/data-flow"; | ||
import { ChainId, Logger } from "@grants-stack-indexer/shared"; | ||
|
||
|
@@ -27,12 +28,12 @@ import { SharedDependencies, SharedDependenciesService } from "./index.js"; | |
* - Manages graceful shutdown on termination signals | ||
*/ | ||
export class ProcessingService { | ||
private readonly orchestrators: Map<ChainId, Orchestrator> = new Map(); | ||
private readonly orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]> = new Map(); | ||
private readonly logger = new Logger({ className: "ProcessingService" }); | ||
private readonly kyselyDatabase: SharedDependencies["kyselyDatabase"]; | ||
|
||
private constructor( | ||
orchestrators: Map<ChainId, Orchestrator>, | ||
orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]>, | ||
kyselyDatabase: SharedDependencies["kyselyDatabase"], | ||
) { | ||
this.orchestrators = orchestrators; | ||
|
@@ -43,8 +44,12 @@ export class ProcessingService { | |
const sharedDependencies = await SharedDependenciesService.initialize(env); | ||
const { CHAINS: chains } = env; | ||
const { core, registriesRepositories, indexerClient, kyselyDatabase } = sharedDependencies; | ||
const { eventRegistryRepository, strategyRegistryRepository } = registriesRepositories; | ||
const orchestrators: Map<ChainId, Orchestrator> = new Map(); | ||
const { | ||
eventRegistryRepository, | ||
strategyRegistryRepository, | ||
strategyProcessingCheckpointRepository, | ||
} = registriesRepositories; | ||
const orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]> = new Map(); | ||
|
||
const strategyRegistry = await InMemoryCachedStrategyRegistry.initialize( | ||
new Logger({ className: "InMemoryCachedStrategyRegistry" }), | ||
|
@@ -70,21 +75,32 @@ export class ProcessingService { | |
[chain.id as ChainId], | ||
); | ||
|
||
orchestrators.set( | ||
const orchestrator = new Orchestrator( | ||
chain.id as ChainId, | ||
{ ...core, evmProvider }, | ||
indexerClient, | ||
{ | ||
eventsRegistry: cachedEventsRegistry, | ||
strategyRegistry, | ||
}, | ||
chain.fetchLimit, | ||
chain.fetchDelayMs, | ||
chainLogger, | ||
); | ||
const retroactiveProcessor = new RetroactiveProcessor( | ||
chain.id as ChainId, | ||
new Orchestrator( | ||
chain.id as ChainId, | ||
{ ...core, evmProvider }, | ||
indexerClient, | ||
{ | ||
eventsRegistry: cachedEventsRegistry, | ||
strategyRegistry, | ||
}, | ||
chain.fetchLimit, | ||
chain.fetchDelayMs, | ||
chainLogger, | ||
), | ||
{ ...core, evmProvider }, | ||
indexerClient, | ||
{ | ||
eventsRegistry: cachedEventsRegistry, | ||
strategyRegistry, | ||
checkpointRepository: strategyProcessingCheckpointRepository, | ||
}, | ||
chain.fetchLimit, | ||
chainLogger, | ||
); | ||
|
||
orchestrators.set(chain.id as ChainId, [orchestrator, retroactiveProcessor]); | ||
} | ||
|
||
return new ProcessingService(orchestrators, kyselyDatabase); | ||
|
@@ -114,7 +130,7 @@ export class ProcessingService { | |
}); | ||
|
||
try { | ||
for (const orchestrator of this.orchestrators.values()) { | ||
for (const [orchestrator, _] of this.orchestrators.values()) { | ||
this.logger.info(`Starting orchestrator for chain ${orchestrator.chainId}...`); | ||
orchestratorProcesses.push(orchestrator.run(abortController.signal)); | ||
} | ||
|
@@ -126,6 +142,12 @@ export class ProcessingService { | |
} | ||
} | ||
|
||
async processRetroactiveEvents(): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs here? |
||
for (const [_, retroactiveProcessor] of this.orchestrators.values()) { | ||
await retroactiveProcessor.processRetroactiveStrategies(); | ||
} | ||
} | ||
|
||
/** | ||
* Call this function when the processor service is terminated | ||
* - Releases database resources | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to add some way to communicate if it initialized successfully/unsuccessfully
Could either have an initialized log after success
Or could have a failed to initialize X service message in the initialize error handling