|
1 | | -import { |
2 | | - asArray, |
3 | | - asBoolean, |
4 | | - asMaybe, |
5 | | - asObject, |
6 | | - asOptional, |
7 | | - asString |
8 | | -} from 'cleaners' |
9 | | -import { syncedDocument } from 'edge-server-tools' |
| 1 | +import { asBoolean, asMaybe, asObject, asOptional, asString } from 'cleaners' |
10 | 2 | import nano from 'nano' |
11 | 3 | import promisify from 'promisify-node' |
12 | 4 | import { createClient } from 'redis' |
@@ -153,80 +145,11 @@ export const getFromDb = async ( |
153 | 145 | export const wrappedGetFromDb = async (dates: string[]): Promise<DbDoc[]> => |
154 | 146 | getFromDb(dbRates, dates) |
155 | 147 |
|
156 | | -const asCurrencyCodeMapsCleaner = asObject({ |
157 | | - constantCurrencyCodes: asMaybe(asObject(asString)), |
158 | | - zeroRates: asMaybe(asObject(asString)), |
159 | | - fallbackConstantRates: asMaybe(asObject(asString)), |
160 | | - coinMarketCap: asMaybe(asObject(asString)), |
161 | | - coincap: asMaybe(asObject(asString)), |
162 | | - coingecko: asMaybe(asObject(asString)), |
163 | | - allEdgeCurrencies: asMaybe(asArray(asString)), |
164 | | - fiatCurrencyCodes: asMaybe(asArray(asString)) |
165 | | -}) |
166 | | - |
167 | | -// Pass the defaults json through the cleaner so they're typed |
168 | | -const defaultCurrencyCodeMaps = asCurrencyCodeMapsCleaner(currencyCodeMaps) |
169 | | - |
170 | | -const asCurrencyCodeMaps = asMaybe( |
171 | | - asCurrencyCodeMapsCleaner, |
172 | | - defaultCurrencyCodeMaps |
173 | | -) |
174 | | - |
175 | | -const syncedCurrencyCodeMaps = syncedDocument( |
176 | | - 'currencyCodeMaps', |
177 | | - asCurrencyCodeMaps |
178 | | -) |
179 | | - |
180 | | -// Only run sync in background engine processes, not web server instances |
181 | | -if (process.env.ENABLE_BACKGROUND_SYNC !== 'false') { |
182 | | - syncedCurrencyCodeMaps.onChange(currencyCodeMaps => { |
183 | | - const timestamp = new Date().toISOString() |
184 | | - logger( |
185 | | - `[${timestamp}] SYNC TRIGGERED: Syncing currency code maps with redis cache...` |
186 | | - ) |
187 | | - logger( |
188 | | - `[${timestamp}] SYNC TRIGGER: onChange fired for currencyCodeMaps document (PID: ${process.pid})` |
189 | | - ) |
190 | | - for (const key of Object.keys(currencyCodeMaps)) { |
191 | | - const tempKey = `${key}_temp_${Date.now()}` |
192 | | - |
193 | | - // Write to temporary key first, then atomically rename |
194 | | - const writeToTemp = async (): Promise<void> => { |
195 | | - if (Array.isArray(currencyCodeMaps[key])) { |
196 | | - await hsetAsync(tempKey, Object.assign({}, currencyCodeMaps[key])) |
197 | | - } else { |
198 | | - await hsetAsync(tempKey, currencyCodeMaps[key]) |
199 | | - } |
200 | | - } |
201 | | - |
202 | | - const updateKey = async (): Promise<void> => { |
203 | | - try { |
204 | | - await writeToTemp() |
205 | | - // Atomically replace the old key with the new data |
206 | | - await renameAsync(tempKey, key) |
207 | | - logger(`Successfully updated Redis key: ${key}`) |
208 | | - } catch (e) { |
209 | | - logger('syncedCurrencyCodeMaps failed to update', key, e) |
210 | | - // Clean up temporary key on failure |
211 | | - try { |
212 | | - await delAsync(tempKey) |
213 | | - } catch (cleanupError) { |
214 | | - logger('Failed to cleanup temp key', tempKey, cleanupError) |
215 | | - } |
216 | | - } |
217 | | - } |
218 | | - |
219 | | - // Fire and forget - don't await in the callback |
220 | | - updateKey().catch(e => logger('Unhandled error in updateKey', key, e)) |
221 | | - } |
222 | | - }) |
223 | | -} |
224 | | - |
225 | 148 | export const ratesDbSetup = { |
226 | 149 | name: 'db_rates', |
227 | 150 | options: { partitioned: false }, |
228 | 151 | templates: { currencyCodeMaps }, |
229 | | - syncedDocuments: [syncedCurrencyCodeMaps] |
| 152 | + syncedDocuments: [] // Empty array since sync is now handled in indexEngines |
230 | 153 | } |
231 | 154 |
|
232 | 155 | export const getEdgeAssetDoc = memoize( |
|
0 commit comments