Skip to content

Commit

Permalink
fix: don't crash when getContractsWithRetry fails (#474)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos authored May 22, 2024
1 parent eaac0cb commit 37f0df7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 6 additions & 1 deletion commands/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ export const station = async ({ json, experimental }) => {
}),
runPingLoop({ STATION_ID }),
runMachinesLoop({ STATION_ID }),
runUpdateContractsLoop({ provider, abi, contracts }),
runUpdateContractsLoop({
provider,
abi,
contracts,
onActivity: (activity) => activities.submit(activity)
}),
runUpdateRewardsLoop({
contracts,
ethAddress,
Expand Down
19 changes: 14 additions & 5 deletions lib/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ const {
CONTRACT_ADDRESSES_IPNS_KEY = 'k51qzi5uqu5dmaqrefqazad0ca8b24fb79zlacfjw2awdt5gjf2cr6jto5jyqe'
} = process.env

export const runUpdateContractsLoop = async ({ provider, abi, contracts }) => {
export const runUpdateContractsLoop = async ({ provider, abi, contracts, onActivity }) => {
while (true) {
const delay = 10 * 60 * 1000 // 10 minutes
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
}
const newContracts = await getContractsWithRetry({ provider, abi })
contracts.splice(0)
contracts.push(...newContracts)
try {
const newContracts = await getContractsWithRetry({ provider, abi })
contracts.splice(0)
contracts.push(...newContracts)
} catch (err) {
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.`
})
}
}
}

Expand Down

0 comments on commit 37f0df7

Please sign in to comment.