Skip to content

Commit

Permalink
Try-catch update functions in cache-update loops. (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau authored Feb 5, 2024
1 parent 5636db5 commit 087e939
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
14 changes: 9 additions & 5 deletions indexer/packages/postgres/src/loops/asset-refresher.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {
stats, delay, logger, NodeEnv,
stats,
logger,
NodeEnv,
} from '@dydxprotocol-indexer/base';

import config from '../config';
import * as AssetTable from '../stores/asset-table';
import { AssetFromDatabase, AssetsMap, Options } from '../types';
import { startUpdateLoop } from './loopHelper';

let idToAsset: AssetsMap = {};

/**
* Refresh loop to cache the list of all assets from the database in-memory.
*/
export async function start(): Promise<void> {
for (;;) {
await updateAssets();
await delay(config.ASSET_REFRESHER_INTERVAL_MS);
}
await startUpdateLoop(
updateAssets,
config.ASSET_REFRESHER_INTERVAL_MS,
'updateAssets',
);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions indexer/packages/postgres/src/loops/liquidity-tier-refresher.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {
NodeEnv, delay, logger, stats,
NodeEnv,
logger,
stats,
} from '@dydxprotocol-indexer/base';

import config from '../config';
import * as LiquidityTiersTable from '../stores/liquidity-tiers-table';
import { LiquidityTiersFromDatabase, LiquidityTiersMap, Options } from '../types';
import { startUpdateLoop } from './loopHelper';

let idToLiquidityTier: LiquidityTiersMap = {};

/**
* Refresh loop to cache the list of all liquidity tiers from the database in-memory.
*/
export async function start(): Promise<void> {
for (; ;) {
await updateLiquidityTiers();
await delay(config.LIQUIDITY_TIER_REFRESHER_INTERVAL_MS);
}
await startUpdateLoop(
updateLiquidityTiers,
config.LIQUIDITY_TIER_REFRESHER_INTERVAL_MS,
'updateLiquidityTiers',
);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions indexer/packages/postgres/src/loops/loopHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { delay, logger } from '@dydxprotocol-indexer/base';

export async function startUpdateLoop(
updateFunction: () => Promise<void>,
delayMs: number,
name: string,
): Promise<void> {
for (;;) {
try {
await updateFunction();
} catch (error) {
logger.error({
at: name,
message: 'Failed to run update',
error,
});
}
await delay(delayMs);
}
}
15 changes: 10 additions & 5 deletions indexer/packages/postgres/src/loops/perpetual-market-refresher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { stats, delay, NodeEnv } from '@dydxprotocol-indexer/base';
import {
stats,
NodeEnv,
} from '@dydxprotocol-indexer/base';
import _ from 'lodash';

import config from '../config';
import * as PerpetualMarketTable from '../stores/perpetual-market-table';
import {
Options, PerpetualMarketColumns, PerpetualMarketFromDatabase, PerpetualMarketsMap,
} from '../types';
import { startUpdateLoop } from './loopHelper';

let idToPerpetualMarket: Record<string, PerpetualMarketFromDatabase> = {};

Expand All @@ -16,10 +20,11 @@ let idToPerpetualMarket: Record<string, PerpetualMarketFromDatabase> = {};
* Refresh loop to cache the list of all perpetual markets from the database in-memory.
*/
export async function start(): Promise<void> {
for (;;) {
await updatePerpetualMarkets();
await delay(config.PERPETUAL_MARKETS_REFRESHER_INTERVAL_MS);
}
await startUpdateLoop(
updatePerpetualMarkets,
config.PERPETUAL_MARKETS_REFRESHER_INTERVAL_MS,
'updatePerpetualMarkets',
);
}

/**
Expand Down

0 comments on commit 087e939

Please sign in to comment.