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: error retry handling #49
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -16,6 +16,9 @@ import { | |
isAlloEvent, | ||
isStrategyEvent, | ||
ProcessorEvent, | ||
RetriableError, | ||
RetryHandler, | ||
RetryStrategy, | ||
StrategyEvent, | ||
stringify, | ||
} from "@grants-stack-indexer/shared"; | ||
|
@@ -46,10 +49,11 @@ import { CoreDependencies, DataLoader, delay, IQueue, iStrategyAbi, Queue } from | |
* The Orchestrator provides fault tolerance and performance optimization through: | ||
* - Configurable batch sizes for event fetching | ||
* - Delayed processing to prevent overwhelming the system | ||
* - Error handling and logging for various failure scenarios | ||
* - Retry handling with exponential backoff for transient failures | ||
* - Comprehensive error handling and logging for various failure scenarios | ||
* - Registry tracking of supported/unsupported strategies and events | ||
* | ||
* TODO: Enhance the error handling/retries, logging and observability | ||
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. ✅ |
||
* TODO: Enhance logging and observability | ||
*/ | ||
export class Orchestrator { | ||
private readonly eventsQueue: IQueue<ProcessorEvent<ContractName, AnyEvent>>; | ||
|
@@ -58,13 +62,15 @@ export class Orchestrator { | |
private readonly eventsRegistry: IEventsRegistry; | ||
private readonly strategyRegistry: IStrategyRegistry; | ||
private readonly dataLoader: DataLoader; | ||
private readonly retryHandler: RetryHandler; | ||
|
||
/** | ||
* @param chainId - The chain id | ||
* @param dependencies - The core dependencies | ||
* @param indexerClient - The indexer client | ||
* @param registries - The registries | ||
* @param fetchLimit - The fetch limit | ||
* @param retryStrategy - The retry strategy | ||
* @param fetchDelayInMs - The fetch delay in milliseconds | ||
*/ | ||
constructor( | ||
|
@@ -77,6 +83,7 @@ export class Orchestrator { | |
}, | ||
private fetchLimit: number = 1000, | ||
private fetchDelayInMs: number = 10000, | ||
private retryStrategy: RetryStrategy, | ||
private logger: ILogger, | ||
) { | ||
this.eventsFetcher = new EventsFetcher(this.indexerClient); | ||
|
@@ -98,6 +105,7 @@ export class Orchestrator { | |
this.logger, | ||
); | ||
this.eventsQueue = new Queue<ProcessorEvent<ContractName, AnyEvent>>(fetchLimit); | ||
this.retryHandler = new RetryHandler(retryStrategy, this.logger); | ||
} | ||
|
||
async run(signal: AbortSignal): Promise<void> { | ||
|
@@ -119,46 +127,42 @@ export class Orchestrator { | |
await delay(this.fetchDelayInMs); | ||
continue; | ||
} | ||
|
||
await this.eventsRegistry.saveLastProcessedEvent(this.chainId, { | ||
...event, | ||
rawEvent: event, | ||
}); | ||
|
||
event = await this.enhanceStrategyId(event); | ||
if (this.isPoolCreated(event)) { | ||
const handleable = existsHandler(event.strategyId); | ||
await this.strategyRegistry.saveStrategyId( | ||
this.chainId, | ||
event.params.strategy, | ||
event.strategyId, | ||
handleable, | ||
); | ||
} else if (event.contractName === "Strategy" && "strategyId" in event) { | ||
if (!existsHandler(event.strategyId)) { | ||
this.logger.debug("Skipping event", { | ||
event, | ||
className: Orchestrator.name, | ||
chainId: this.chainId, | ||
}); | ||
// we skip the event if the strategy id is not handled yet | ||
continue; | ||
} | ||
} | ||
|
||
const changesets = await this.eventsProcessor.processEvent(event); | ||
await this.dataLoader.applyChanges(changesets); | ||
await this.retryHandler.execute( | ||
async () => { | ||
await this.handleEvent(event!); | ||
}, | ||
{ abortSignal: signal }, | ||
); | ||
} catch (error: unknown) { | ||
// TODO: improve error handling, retries and notify | ||
// TODO: notify | ||
if ( | ||
error instanceof UnsupportedStrategy || | ||
error instanceof InvalidEvent || | ||
error instanceof UnsupportedEventException | ||
) { | ||
// this.logger.error( | ||
// `Current event cannot be handled. ${error.name}: ${error.message}. Event: ${stringify(event)}`, | ||
// ); | ||
this.logger.debug( | ||
`Current event cannot be handled. ${error.name}: ${error.message}.`, | ||
{ | ||
className: Orchestrator.name, | ||
chainId: this.chainId, | ||
event, | ||
}, | ||
); | ||
} else { | ||
if (error instanceof Error || isNativeError(error)) { | ||
if (error instanceof RetriableError) { | ||
error.message = `Error processing event after retries. ${error.message}`; | ||
this.logger.error(error, { | ||
event, | ||
className: Orchestrator.name, | ||
chainId: this.chainId, | ||
}); | ||
} else if (error instanceof Error || isNativeError(error)) { | ||
this.logger.error(error, { | ||
event, | ||
className: Orchestrator.name, | ||
|
@@ -201,6 +205,32 @@ export class Orchestrator { | |
this.eventsQueue.push(...events); | ||
} | ||
|
||
private async handleEvent(event: ProcessorEvent<ContractName, AnyEvent>): Promise<void> { | ||
event = await this.enhanceStrategyId(event); | ||
if (this.isPoolCreated(event)) { | ||
const handleable = existsHandler(event.strategyId); | ||
await this.strategyRegistry.saveStrategyId( | ||
this.chainId, | ||
event.params.strategy, | ||
event.strategyId, | ||
handleable, | ||
); | ||
} else if (event.contractName === "Strategy" && "strategyId" in event) { | ||
if (!existsHandler(event.strategyId)) { | ||
this.logger.debug("Skipping event", { | ||
event, | ||
className: Orchestrator.name, | ||
chainId: this.chainId, | ||
}); | ||
// we skip the event if the strategy id is not handled yet | ||
return; | ||
} | ||
} | ||
|
||
const changesets = await this.eventsProcessor.processEvent(event); | ||
await this.dataLoader.applyChanges(changesets); | ||
} | ||
|
||
/** | ||
* Enhance the event with the strategy id when required | ||
* @param event - The event | ||
|
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.
is it possible to mark these three as optional within schema chaining since they're technically not required?
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.
with the
default()
they are optional, you are not forced to pass them