Skip to content

Commit

Permalink
Merge pull request #158 from JoinColony/fix/157-graceful-loss-of-stat…
Browse files Browse the repository at this point in the history
…s-api

Restart if cannot fetch stats database data / API
  • Loading branch information
rdig authored Jan 16, 2024
2 parents 186e773 + fcca9fc commit 0413a1c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/utils/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ export const updateStats = async (
// This exists as a function to prevent accidental overwriting of the `stats` variable
export const getStats = (): typeof stats => ({ ...stats });

export const getLastBlockNumber = (): number =>
Number.isInteger(stats.lastBlockNumber) ? Number(stats.lastBlockNumber) : 1;
export const getLastBlockNumber = (): number => {
if (Number.isInteger(stats.lastBlockNumber)) {
return Number(stats.lastBlockNumber);
}
/*
* @NOTE This prevents accidental database stats overwriting if the API / GraphQL
* endpoint is not accessible
*
* It will throw the block ingestor (the pod that it's running on) into an restart
* loop until the API is accessible again
*/
throw new Error('Could not get last block number from stats. Aborting.');
};

export const setLastBlockNumber = (lastBlockNumber: number): void => {
updateStats({ lastBlockNumber });
Expand Down

0 comments on commit 0413a1c

Please sign in to comment.