File tree Expand file tree Collapse file tree 5 files changed +43
-30
lines changed
Expand file tree Collapse file tree 5 files changed +43
-30
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import {
2020 wasExistingMappings
2121} from '../../types'
2222import {
23+ create24HourSyncInterval ,
2324 createTokenId ,
2425 expandReturnedCryptoRates ,
2526 isCurrent ,
@@ -110,15 +111,9 @@ const platformIdMappingSyncDoc = syncedDocument(
110111 'coingecko:platforms' ,
111112 asStringNullMap
112113)
113- manualTokenMappingsSyncDoc . sync ( dbSettings ) . catch ( e => {
114- console . error ( 'manualTokenMappingsSyncDoc sync error' , e )
115- } )
116- automatedTokenMappingsSyncDoc . sync ( dbSettings ) . catch ( e => {
117- console . error ( 'automatedTokenMappingsSyncDoc sync error' , e )
118- } )
119- platformIdMappingSyncDoc . sync ( dbSettings ) . catch ( e => {
120- console . error ( 'platformIdMappingSyncDoc sync error' , e )
121- } )
114+ create24HourSyncInterval ( manualTokenMappingsSyncDoc , dbSettings )
115+ create24HourSyncInterval ( automatedTokenMappingsSyncDoc , dbSettings )
116+ create24HourSyncInterval ( platformIdMappingSyncDoc , dbSettings )
122117manualTokenMappingsSyncDoc . onChange ( manualMappings => {
123118 coingeckoTokenIdMap = {
124119 ...automatedTokenMappingsSyncDoc . doc ,
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import {
2222 wasExistingMappings
2323} from '../../types'
2424import {
25+ create24HourSyncInterval ,
2526 createTokenId ,
2627 expandReturnedCryptoRates ,
2728 isCurrent ,
@@ -144,15 +145,9 @@ const platformIdMappingSyncDoc = syncedDocument(
144145 'coinmarketcap:platforms' ,
145146 asStringNullMap
146147)
147- userTokenMappingsSyncDoc . sync ( dbSettings ) . catch ( e => {
148- console . error ( 'manualTokenMappingsSyncDoc sync error' , e )
149- } )
150- automatedTokenMappingsSyncDoc . sync ( dbSettings ) . catch ( e => {
151- console . error ( 'automatedTokenMappingsSyncDoc sync error' , e )
152- } )
153- platformIdMappingSyncDoc . sync ( dbSettings ) . catch ( e => {
154- console . error ( 'platformIdMappingSyncDoc sync error' , e )
155- } )
148+ create24HourSyncInterval ( userTokenMappingsSyncDoc , dbSettings )
149+ create24HourSyncInterval ( automatedTokenMappingsSyncDoc , dbSettings )
150+ create24HourSyncInterval ( platformIdMappingSyncDoc , dbSettings )
156151userTokenMappingsSyncDoc . onChange ( userMappings => {
157152 coinmarketcapTokenIdMap = {
158153 ...automatedTokenMappingsSyncDoc . doc ,
Original file line number Diff line number Diff line change @@ -2,14 +2,15 @@ import { asNumber, asObject } from 'cleaners'
22import { syncedDocument } from 'edge-server-tools'
33
44import type { NumberMap , RateBuckets , RateProvider } from '../types'
5- import { expandReturnedCryptoRates , reduceRequestedCryptoRates } from '../utils'
5+ import {
6+ create24HourSyncInterval ,
7+ expandReturnedCryptoRates ,
8+ reduceRequestedCryptoRates
9+ } from '../utils'
610import { dbSettings } from './couch'
711
812const constantRateSyncDoc = syncedDocument ( 'constantrates' , asObject ( asNumber ) )
9-
10- constantRateSyncDoc . sync ( dbSettings ) . catch ( e => {
11- console . error ( 'constantRateSyncDoc sync error' , e )
12- } )
13+ create24HourSyncInterval ( constantRateSyncDoc , dbSettings )
1314
1415export const constantRates : RateProvider = {
1516 providerId : 'constantRates' ,
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ import {
1515 type GetRatesParams ,
1616 type IncomingGetRatesParams
1717} from './types'
18- import { toCryptoKey } from './utils'
18+ import { create24HourSyncInterval , toCryptoKey } from './utils'
1919
2020const fixIncomingGetRatesParams = (
2121 rawParams : IncomingGetRatesParams ,
@@ -99,12 +99,8 @@ const automatedCrossChainSyncDoc = syncedDocument(
9999 'crosschain:automated' ,
100100 asCrossChainMapping
101101)
102- defaultCrossChainSyncDoc . sync ( dbSettings ) . catch ( e => {
103- console . error ( 'defaultCrossChainSyncDoc sync error' , e )
104- } )
105- automatedCrossChainSyncDoc . sync ( dbSettings ) . catch ( e => {
106- console . error ( 'automatedCrossChainSyncDoc sync error' , e )
107- } )
102+ create24HourSyncInterval ( defaultCrossChainSyncDoc , dbSettings )
103+ create24HourSyncInterval ( automatedCrossChainSyncDoc , dbSettings )
108104defaultCrossChainSyncDoc . onChange ( defaultMappings => {
109105 crosschainMappings = {
110106 ...automatedCrossChainSyncDoc . doc ,
Original file line number Diff line number Diff line change 1+ import type { SyncedDocument } from 'edge-server-tools'
2+ import type nano from 'nano'
3+
14import { FIVE_MINUTES , ONE_MINUTE , TWENTY_FOUR_HOURS } from './constants'
25import type {
36 CryptoRateMap ,
@@ -304,3 +307,26 @@ export const isCurrent = (
304307}
305308export const isCurrentFiat = ( isoDate : Date , rightNow : Date ) : boolean =>
306309 isCurrent ( isoDate , rightNow , TWENTY_FOUR_HOURS )
310+
311+ /**
312+ * Create an interval to manually refresh the synced document.
313+ * This is a workaround in case we lose the connection to the CouchDB changes feed.
314+ */
315+ export const create24HourSyncInterval = (
316+ syncedDocument : SyncedDocument < unknown > ,
317+ db : nano . DocumentScope < any > ,
318+ label : string = ''
319+ ) : void => {
320+ syncedDocument
321+ . sync ( db )
322+ . then ( ( ) => {
323+ setInterval ( ( ) => {
324+ syncedDocument . sync ( db ) . catch ( e => {
325+ console . error ( 'interval sync error' , e )
326+ } )
327+ } , TWENTY_FOUR_HOURS )
328+ } )
329+ . catch ( e => {
330+ console . error ( 'create24HourSyncInterval error' , e )
331+ } )
332+ }
You can’t perform that action at this time.
0 commit comments