From 2892bfa1d81299474ddb3de265a1ecf785b1736d Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 4 Aug 2023 13:23:09 +0900 Subject: [PATCH] Fixing cycle reset at top of the hour --- backend/src/tasks/price-updater.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/src/tasks/price-updater.ts b/backend/src/tasks/price-updater.ts index bbfb7d167..79173ad37 100644 --- a/backend/src/tasks/price-updater.ts +++ b/backend/src/tasks/price-updater.ts @@ -28,6 +28,7 @@ class PriceUpdater { private timeBetweenUpdatesMs = 3600000 / config.MEMPOOL.PRICE_UPDATES_PER_HOUR; private cyclePosition = -1; private firstRun = true; + private lastTime = -1; private lastHistoricalRun = 0; private running = false; private feeds: PriceFeed[] = []; @@ -136,7 +137,13 @@ class PriceUpdater { } const millisecondsSinceBeginningOfHour = this.getMillisecondsSinceBeginningOfHour(); - if (millisecondsSinceBeginningOfHour < this.timeBetweenUpdatesMs * this.cyclePosition && !forceUpdate) { + + // Reset the cycle on new hour + if (this.lastTime > millisecondsSinceBeginningOfHour) { + this.cyclePosition = 0; + } + this.lastTime = millisecondsSinceBeginningOfHour; + if (millisecondsSinceBeginningOfHour < this.timeBetweenUpdatesMs * this.cyclePosition && !forceUpdate && this.cyclePosition !== 0) { return; } @@ -190,9 +197,6 @@ class PriceUpdater { if (!forceUpdate) { this.cyclePosition++; - if (this.cyclePosition > config.MEMPOOL.PRICE_UPDATES_PER_HOUR) { - this.cyclePosition = 0; - } } if (this.latestPrices.USD === -1) {