Skip to content

Commit 2af776a

Browse files
committed
Fix engines to properly loop w/o recursion
1 parent 1c37174 commit 2af776a

File tree

3 files changed

+77
-69
lines changed

3 files changed

+77
-69
lines changed

src/coinrankEngine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const coinrankEngine = async (): Promise<void> => {
6464
JSON.stringify(data)
6565
)
6666
} catch (e) {
67-
const err: any = e // Weird TS issue causing :any to get removed from above line
67+
const err: any = e // Weird TS issue causing :any to get removed from above line
6868
const message = `coinrankEngine failure: ${err.message}`
6969
slackMessage(message).catch(e => logger(e))
7070
logger(message)

src/ratesEngine.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,50 @@ const getCurrencyCodeList = async (): Promise<string[]> => {
5656

5757
export const ratesEngine = async (): Promise<void> => {
5858
const { ratesOffsetSeconds, ratesIntervalSeconds } = config
59-
const delay = getDelay({
60-
now: new Date(),
61-
offsetSeconds: ratesOffsetSeconds,
62-
intervalSeconds: ratesIntervalSeconds
63-
})
64-
logger(`**** RATES ENGINE SNOOZING ${delay / 1000}s`)
65-
await snooze(delay)
59+
while (true) {
60+
const delay = getDelay({
61+
now: new Date(),
62+
offsetSeconds: ratesOffsetSeconds,
63+
intervalSeconds: ratesIntervalSeconds
64+
})
65+
logger(`**** RATES ENGINE SNOOZING ${delay / 1000}s`)
66+
await snooze(delay)
6667

67-
try {
68-
const currentDate = normalizeDate(new Date().toISOString())
69-
const allCurrencies = await getCurrencyCodeList()
68+
try {
69+
const currentDate = normalizeDate(new Date().toISOString())
70+
const allCurrencies = await getCurrencyCodeList()
7071

71-
const data: ExchangeRateReq[] = [
72-
...preferredCryptoFiatPairs.map(currency_pair => ({
73-
currency_pair,
74-
date: currentDate
75-
}))
76-
]
77-
for (const currencyCode of allCurrencies) {
78-
data.push({
79-
currency_pair: `${currencyCode}_${bridgeCurrency}`,
80-
date: currentDate
81-
})
82-
}
83-
const promises: Array<Promise<any>> = []
84-
while (data.length > 0) {
85-
promises.push(
86-
fetch(endPoint, {
87-
headers: {
88-
'Content-Type': 'application/json'
89-
},
90-
method: 'POST',
91-
body: JSON.stringify({ data: data.splice(0, 100) })
92-
}).catch(e => logger('ratesEngine query error', e))
93-
)
72+
const data: ExchangeRateReq[] = [
73+
...preferredCryptoFiatPairs.map(currency_pair => ({
74+
currency_pair,
75+
date: currentDate
76+
}))
77+
]
78+
for (const currencyCode of allCurrencies) {
79+
data.push({
80+
currency_pair: `${currencyCode}_${bridgeCurrency}`,
81+
date: currentDate
82+
})
83+
}
84+
const promises: Array<Promise<any>> = []
85+
while (data.length > 0) {
86+
promises.push(
87+
fetch(endPoint, {
88+
headers: {
89+
'Content-Type': 'application/json'
90+
},
91+
method: 'POST',
92+
body: JSON.stringify({ data: data.splice(0, 100) })
93+
}).catch(e => logger('ratesEngine query error', e))
94+
)
95+
}
96+
await Promise.all(promises)
97+
} catch (e) {
98+
const message = `ratesEngine failure: ${e}`
99+
slackMessage(message).catch(e => logger(e))
100+
logger(message)
101+
} finally {
102+
logger('ratesEngine loop complete')
94103
}
95-
await Promise.all(promises)
96-
} catch (e) {
97-
const message = `ratesEngine failure: ${e}`
98-
slackMessage(message).catch(e => logger(e))
99-
logger(message)
100-
} finally {
101-
ratesEngine().catch(e => logger(e))
102104
}
103105
}

src/uidEngine.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,49 @@ import {
77
wrappedGetFromDb,
88
wrappedSaveToDb
99
} from './utils/dbUtils'
10-
import { logger } from './utils/utils'
10+
import { logger, snooze } from './utils/utils'
1111

1212
const providerAssets = {
1313
// coincap: coincapAssets, // Disabled because ZEC rates are bad. Can re-enable once we can blacklist IDs.
1414
coinMarketCap: coinMarketCapAssets,
1515
coingecko: coingeckoAssets
1616
}
1717

18+
const DEFAULT_WAIT_MS = 60 * 60 * 1000 // 1 hour
19+
1820
export const uidEngine = async (): Promise<void> => {
19-
logger('Updating UID Cache')
20-
try {
21-
const edgeDoc = (await wrappedGetFromDb(['currencyCodeMaps']))[0]
22-
const promises = Object.keys(providerAssets).map(provider =>
23-
providerAssets[provider]()
24-
.then(newMap => {
25-
// Combine the new UID map with existing UID map
26-
const assetMap = { ...edgeDoc[provider], ...newMap }
21+
while (true) {
22+
logger('Updating UID Cache')
23+
try {
24+
const edgeDoc = (await wrappedGetFromDb(['currencyCodeMaps']))[0]
25+
const promises = Object.keys(providerAssets).map(provider =>
26+
providerAssets[provider]()
27+
.then(newMap => {
28+
// Combine the new UID map with existing UID map
29+
const assetMap = { ...edgeDoc[provider], ...newMap }
2730

28-
// Remove the UIDs for the currency codes we've hardcoded
29-
for (let i = 0; i < edgeDoc.allEdgeCurrencies.length; i++) {
30-
delete assetMap[edgeDoc.allEdgeCurrencies[i]]
31-
}
31+
// Remove the UIDs for the currency codes we've hardcoded
32+
for (let i = 0; i < edgeDoc.allEdgeCurrencies.length; i++) {
33+
delete assetMap[edgeDoc.allEdgeCurrencies[i]]
34+
}
3235

33-
// Combine our codes with the new ones
34-
edgeDoc[provider] = {
35-
...assetMap,
36-
...currencyCodeMaps[provider]
37-
}
38-
})
39-
.catch(e => logger(`Failed to update ${provider}`, e))
40-
.finally(logger(`${provider} provider updated`))
41-
)
42-
await Promise.allSettled(promises)
43-
wrappedSaveToDb([edgeDoc])
44-
} catch (e) {
45-
const message = `ratesEngine failure: ${e}`
46-
slackMessage(message).catch(e => logger(e))
47-
logger(message)
36+
// Combine our codes with the new ones
37+
edgeDoc[provider] = {
38+
...assetMap,
39+
...currencyCodeMaps[provider]
40+
}
41+
})
42+
.catch(e => logger(`Failed to update ${provider}`, e))
43+
.finally(logger(`${provider} provider updated`))
44+
)
45+
await Promise.allSettled(promises)
46+
wrappedSaveToDb([edgeDoc])
47+
} catch (e) {
48+
const message = `ratesEngine failure: ${e}`
49+
slackMessage(message).catch(e => logger(e))
50+
logger(message)
51+
}
52+
logger('UID Cache updated. Snoozing for 1 hour')
53+
await snooze(DEFAULT_WAIT_MS)
4854
}
4955
}

0 commit comments

Comments
 (0)