From b56f110f28099e8e6f9c9d6ef4131c56141e08ba Mon Sep 17 00:00:00 2001 From: nymkappa Date: Sat, 30 Apr 2022 17:54:49 +0900 Subject: [PATCH] Run hashrate indexing after midnight --- backend/src/api/mining.ts | 22 ++++++++++--------- backend/src/index.ts | 4 ++-- .../src/repositories/HashratesRepository.ts | 18 +++++++-------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index 94191a09f..2c7530643 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -139,10 +139,13 @@ class Mining { try { this.weeklyHashrateIndexingStarted = true; + const lastestRunDate = await HashratesRepository.$getLatestRun('last_weekly_hashrates_indexing'); - // We only run this once a week - const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_weekly_hashrates_indexing') * 1000; - if (now.getTime() - latestTimestamp < 604800000) { + // Run only if: + // * lastestRunDate is set to 0 (node backend restart, reorg) + // * we started a new week (around Monday midnight) + const runIndexing = lastestRunDate === 0 || now.getUTCDay() === 1 && lastestRunDate !== now.getUTCDate(); + if (!runIndexing) { this.weeklyHashrateIndexingStarted = false; return; } @@ -226,7 +229,7 @@ class Mining { ++totalIndexed; } this.weeklyHashrateIndexingStarted = false; - await HashratesRepository.$setLatestRunTimestamp('last_weekly_hashrates_indexing'); + await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', new Date().getUTCDate()); if (newlyIndexed > 0) { logger.info(`Indexed ${newlyIndexed} pools weekly hashrate`); } @@ -244,14 +247,13 @@ class Mining { return; } - const now = new Date().getTime(); - try { this.hashrateIndexingStarted = true; - // We only run this once a day - const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_hashrates_indexing') * 1000; - if (now - latestTimestamp < 86400000) { + // We only run this once a day around midnight + const latestRunDate = await HashratesRepository.$getLatestRun('last_hashrates_indexing'); + const now = new Date().getUTCDate(); + if (now === latestRunDate) { this.hashrateIndexingStarted = false; return; } @@ -339,7 +341,7 @@ class Mining { newlyIndexed += hashrates.length; await HashratesRepository.$saveHashrates(hashrates); - await HashratesRepository.$setLatestRunTimestamp('last_hashrates_indexing'); + await HashratesRepository.$setLatestRun('last_hashrates_indexing', new Date().getUTCDate()); this.hashrateIndexingStarted = false; if (newlyIndexed > 0) { logger.info(`Indexed ${newlyIndexed} day of network hashrate`); diff --git a/backend/src/index.ts b/backend/src/index.ts index 7bf213a81..b44bc6369 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -174,8 +174,8 @@ class Server { async $resetHashratesIndexingState() { try { - await HashratesRepository.$setLatestRunTimestamp('last_hashrates_indexing', 0); - await HashratesRepository.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); + await HashratesRepository.$setLatestRun('last_hashrates_indexing', 0); + await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', 0); } catch (e) { logger.err(`Cannot reset hashrate indexing timestamps. Reason: ` + (e instanceof Error ? e.message : e)); } diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index 3ec1a8d28..661535aa3 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -147,13 +147,13 @@ class HashratesRepository { /** * Set latest run timestamp */ - public async $setLatestRunTimestamp(key: string, val: any = null) { + public async $setLatestRun(key: string, val: number) { const query = `UPDATE state SET number = ? WHERE name = ?`; try { - await DB.query(query, (val === null) ? [Math.round(new Date().getTime() / 1000), key] : [val, key]); + await DB.query(query, [val, key]); } catch (e) { - logger.err(`Cannot set last indexing timestamp for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err(`Cannot set last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); throw e; } } @@ -161,7 +161,7 @@ class HashratesRepository { /** * Get latest run timestamp */ - public async $getLatestRunTimestamp(key: string): Promise { + public async $getLatestRun(key: string): Promise { const query = `SELECT number FROM state WHERE name = ?`; try { @@ -172,7 +172,7 @@ class HashratesRepository { } return rows[0]['number']; } catch (e) { - logger.err(`Cannot retreive last indexing timestamp for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err(`Cannot retrieve last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); throw e; } } @@ -189,8 +189,8 @@ class HashratesRepository { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp = ?`, [row.timestamp]); } // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRunTimestamp('last_hashrates_indexing', 0); - await this.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); + await this.$setLatestRun('last_hashrates_indexing', 0); + await this.$setLatestRun('last_weekly_hashrates_indexing', 0); } catch (e) { logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); } @@ -205,8 +205,8 @@ class HashratesRepository { try { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp >= FROM_UNIXTIME(?)`, [timestamp]); // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRunTimestamp('last_hashrates_indexing', 0); - await this.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); + await this.$setLatestRun('last_hashrates_indexing', 0); + await this.$setLatestRun('last_weekly_hashrates_indexing', 0); } catch (e) { logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); }