Allow priceUpdater to run without storing to database

This commit is contained in:
softsimon 2023-07-30 15:30:23 +09:00
parent 83a487ecae
commit af4d0b4d3f
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 6 additions and 5 deletions

View File

@ -194,6 +194,7 @@ class Server {
await memPool.$updateMempool(newMempool, pollRate); await memPool.$updateMempool(newMempool, pollRate);
} }
indexer.$run(); indexer.$run();
priceUpdater.$run();
// rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS // rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS
const elapsed = Date.now() - start; const elapsed = Date.now() - start;

View File

@ -106,7 +106,7 @@ class Indexer {
} }
try { try {
await priceUpdater.$run(); await priceUpdater.$run(true);
} catch (e) { } catch (e) {
logger.err(`Running priceUpdater failed. Reason: ` + (e instanceof Error ? e.message : e)); logger.err(`Running priceUpdater failed. Reason: ` + (e instanceof Error ? e.message : e));
} }

View File

@ -72,7 +72,7 @@ class PriceUpdater {
this.latestPrices = await PricesRepository.$getLatestConversionRates(); this.latestPrices = await PricesRepository.$getLatestConversionRates();
} }
public async $run(): Promise<void> { public async $run(storeInDb: boolean = false): Promise<void> {
if (config.MEMPOOL.NETWORK === 'signet' || config.MEMPOOL.NETWORK === 'testnet') { if (config.MEMPOOL.NETWORK === 'signet' || config.MEMPOOL.NETWORK === 'testnet') {
// Coins have no value on testnet/signet, so we want to always show 0 // Coins have no value on testnet/signet, so we want to always show 0
return; return;
@ -89,7 +89,7 @@ class PriceUpdater {
} }
try { try {
await this.$updatePrice(); await this.$updatePrice(storeInDb);
if (this.historyInserted === false && config.DATABASE.ENABLED === true) { if (this.historyInserted === false && config.DATABASE.ENABLED === true) {
await this.$insertHistoricalPrices(); await this.$insertHistoricalPrices();
} }
@ -103,7 +103,7 @@ class PriceUpdater {
/** /**
* Fetch last BTC price from exchanges, average them, and save it in the database once every hour * Fetch last BTC price from exchanges, average them, and save it in the database once every hour
*/ */
private async $updatePrice(): Promise<void> { private async $updatePrice(storeInDb: boolean): Promise<void> {
if (this.lastRun === 0 && config.DATABASE.ENABLED === true) { if (this.lastRun === 0 && config.DATABASE.ENABLED === true) {
this.lastRun = await PricesRepository.$getLatestPriceTime(); this.lastRun = await PricesRepository.$getLatestPriceTime();
} }
@ -146,7 +146,7 @@ class PriceUpdater {
} }
} }
if (config.DATABASE.ENABLED === true) { if (config.DATABASE.ENABLED === true && storeInDb) {
// Save everything in db // Save everything in db
try { try {
const p = 60 * 60 * 1000; // milliseconds in an hour const p = 60 * 60 * 1000; // milliseconds in an hour