Skip to content

Commit

Permalink
contracts: update wait after fetch (#477)
Browse files Browse the repository at this point in the history
* contracts: update wait after fetch

* Update lib/contracts.js

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>

* fix

* clean up

---------

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
juliangruber and bajtos authored May 22, 2024
1 parent e8ac667 commit 5990b9a
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ const {
CONTRACT_ADDRESSES_IPNS_KEY = 'k51qzi5uqu5dmaqrefqazad0ca8b24fb79zlacfjw2awdt5gjf2cr6jto5jyqe'
} = process.env

const DELAY_IN_MINUTES = 10

export const runUpdateContractsLoop = async ({ provider, abi, contracts, onActivity }) => {
await timers.setTimeout(2_000)
while (true) {
const delayInMinutes = 10
const delay = delayInMinutes * 60 * 1000 // 10 minutes
const jitter = Math.random() * 20_000 - 10_000 // +- 10 seconds
try {
await timers.setTimeout(delay + jitter)
} catch (err) {
if (err.name === 'AbortError') return
throw err
}
try {
const newContracts = await getContractsWithRetry({ provider, abi })
contracts.splice(0)
Expand All @@ -27,9 +21,18 @@ export const runUpdateContractsLoop = async ({ provider, abi, contracts, onActiv
console.error('Failed to update the list of contract addresses. Will retry later.', err)
onActivity({
type: 'error',
message: `Cannot update scheduled rewards. Will retry in ${delayInMinutes} minutes.`
message: `Cannot update scheduled rewards. Will retry in ${DELAY_IN_MINUTES} minutes.`
})
}

const delay = DELAY_IN_MINUTES * 60 * 1000
const jitter = Math.random() * 20_000 - 10_000 // +- 10 seconds
try {
await timers.setTimeout(delay + jitter)
} catch (err) {
if (err.name === 'AbortError') return
throw err
}
}
}

Expand Down

0 comments on commit 5990b9a

Please sign in to comment.