Skip to content

Commit 269ac93

Browse files
Merge pull request #1049 from cardano-foundation/fix/slow-server-startup
fix: retry ada supply fetch on startup failure and stabilize ada test
2 parents 94eb68b + 05039a3 commit 269ac93

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

packages/api-cardano-db-hasura/test/ada.query.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path'
22

33
import { DocumentNode } from 'graphql'
4+
import pRetry from 'p-retry'
45
import util from '@cardano-graphql/util'
56
import { TestClient } from '@cardano-graphql/util-dev'
67
import { testClient } from './util'
@@ -23,16 +24,17 @@ describe('ada', () => {
2324
})
2425

2526
it('returns ada supply information', async () => {
26-
const result = await client.query({
27-
query: await loadQueryNode('adaSupply')
28-
})
27+
const query = await loadQueryNode('adaSupply')
28+
const result = await pRetry(
29+
() => client.query({ query }),
30+
{ retries: 40, minTimeout: 3000, factor: 1 }
31+
)
2932
const { ada } = result.data
3033
const circulatingSupply = new BigNumber(ada.supply.circulating).toNumber()
3134
const maxSupply = new BigNumber(ada.supply.max).toNumber()
3235
const totalSupply = new BigNumber(ada.supply.total).toNumber()
3336
expect(maxSupply).toEqual(genesis.shelley.maxLovelaceSupply)
3437
expect(maxSupply).toBeGreaterThan(circulatingSupply)
35-
// expect(totalSupply).toBeGreaterThan(circulatingSupply)
3638
expect(totalSupply).toBeLessThan(maxSupply)
3739
})
3840
})

packages/util/src/data_fetching/DataFetcher.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { FetchFunction } from './index'
22
import { clearIntervalAsync, setIntervalAsync, SetIntervalAsyncTimer } from 'set-interval-async/dynamic'
33
import { dummyLogger, Logger } from 'ts-log'
44

5+
const INITIAL_FETCH_RETRY_INTERVAL = 15000
6+
const INITIAL_FETCH_MAX_ATTEMPTS = 40
7+
58
export class DataFetcher<DataValue> {
69
private pollingQueryTimer: SetIntervalAsyncTimer
710
private fetch: () => Promise<void>
@@ -29,6 +32,12 @@ export class DataFetcher<DataValue> {
2932

3033
public async initialize () {
3134
await this.fetch()
35+
let initialAttempts = 0
36+
while (this.value === undefined && initialAttempts < INITIAL_FETCH_MAX_ATTEMPTS) {
37+
initialAttempts++
38+
await new Promise(resolve => setTimeout(resolve, INITIAL_FETCH_RETRY_INTERVAL))
39+
await this.fetch()
40+
}
3241
this.logger.debug({ module: 'DataFetcher', instance: this.name, value: this.value }, 'Initial value fetched')
3342
this.pollingQueryTimer = setIntervalAsync(async () => {
3443
await this.fetch()

0 commit comments

Comments
 (0)