-
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
Changes from 2 commits
f94c48d
1ccb408
0bb15fa
7721c0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? 🤔 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. actually it's typed as
|
||
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(); | ||
}); |
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 | ||
|
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