Run hashrate indexing after midnight

This commit is contained in:
nymkappa 2022-04-30 17:54:49 +09:00
parent bf314be7eb
commit b56f110f28
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
3 changed files with 23 additions and 21 deletions

View File

@ -139,10 +139,13 @@ class Mining {
try { try {
this.weeklyHashrateIndexingStarted = true; this.weeklyHashrateIndexingStarted = true;
const lastestRunDate = await HashratesRepository.$getLatestRun('last_weekly_hashrates_indexing');
// We only run this once a week // Run only if:
const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_weekly_hashrates_indexing') * 1000; // * lastestRunDate is set to 0 (node backend restart, reorg)
if (now.getTime() - latestTimestamp < 604800000) { // * we started a new week (around Monday midnight)
const runIndexing = lastestRunDate === 0 || now.getUTCDay() === 1 && lastestRunDate !== now.getUTCDate();
if (!runIndexing) {
this.weeklyHashrateIndexingStarted = false; this.weeklyHashrateIndexingStarted = false;
return; return;
} }
@ -226,7 +229,7 @@ class Mining {
++totalIndexed; ++totalIndexed;
} }
this.weeklyHashrateIndexingStarted = false; this.weeklyHashrateIndexingStarted = false;
await HashratesRepository.$setLatestRunTimestamp('last_weekly_hashrates_indexing'); await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', new Date().getUTCDate());
if (newlyIndexed > 0) { if (newlyIndexed > 0) {
logger.info(`Indexed ${newlyIndexed} pools weekly hashrate`); logger.info(`Indexed ${newlyIndexed} pools weekly hashrate`);
} }
@ -244,14 +247,13 @@ class Mining {
return; return;
} }
const now = new Date().getTime();
try { try {
this.hashrateIndexingStarted = true; this.hashrateIndexingStarted = true;
// We only run this once a day // We only run this once a day around midnight
const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_hashrates_indexing') * 1000; const latestRunDate = await HashratesRepository.$getLatestRun('last_hashrates_indexing');
if (now - latestTimestamp < 86400000) { const now = new Date().getUTCDate();
if (now === latestRunDate) {
this.hashrateIndexingStarted = false; this.hashrateIndexingStarted = false;
return; return;
} }
@ -339,7 +341,7 @@ class Mining {
newlyIndexed += hashrates.length; newlyIndexed += hashrates.length;
await HashratesRepository.$saveHashrates(hashrates); await HashratesRepository.$saveHashrates(hashrates);
await HashratesRepository.$setLatestRunTimestamp('last_hashrates_indexing'); await HashratesRepository.$setLatestRun('last_hashrates_indexing', new Date().getUTCDate());
this.hashrateIndexingStarted = false; this.hashrateIndexingStarted = false;
if (newlyIndexed > 0) { if (newlyIndexed > 0) {
logger.info(`Indexed ${newlyIndexed} day of network hashrate`); logger.info(`Indexed ${newlyIndexed} day of network hashrate`);

View File

@ -174,8 +174,8 @@ class Server {
async $resetHashratesIndexingState() { async $resetHashratesIndexingState() {
try { try {
await HashratesRepository.$setLatestRunTimestamp('last_hashrates_indexing', 0); await HashratesRepository.$setLatestRun('last_hashrates_indexing', 0);
await HashratesRepository.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', 0);
} catch (e) { } catch (e) {
logger.err(`Cannot reset hashrate indexing timestamps. Reason: ` + (e instanceof Error ? e.message : e)); logger.err(`Cannot reset hashrate indexing timestamps. Reason: ` + (e instanceof Error ? e.message : e));
} }

View File

@ -147,13 +147,13 @@ class HashratesRepository {
/** /**
* Set latest run timestamp * 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 = ?`; const query = `UPDATE state SET number = ? WHERE name = ?`;
try { try {
await DB.query(query, (val === null) ? [Math.round(new Date().getTime() / 1000), key] : [val, key]); await DB.query(query, [val, key]);
} catch (e) { } 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; throw e;
} }
} }
@ -161,7 +161,7 @@ class HashratesRepository {
/** /**
* Get latest run timestamp * Get latest run timestamp
*/ */
public async $getLatestRunTimestamp(key: string): Promise<number> { public async $getLatestRun(key: string): Promise<number> {
const query = `SELECT number FROM state WHERE name = ?`; const query = `SELECT number FROM state WHERE name = ?`;
try { try {
@ -172,7 +172,7 @@ class HashratesRepository {
} }
return rows[0]['number']; return rows[0]['number'];
} catch (e) { } 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; throw e;
} }
} }
@ -189,8 +189,8 @@ class HashratesRepository {
await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp = ?`, [row.timestamp]); await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp = ?`, [row.timestamp]);
} }
// Re-run the hashrate indexing to fill up missing data // Re-run the hashrate indexing to fill up missing data
await this.$setLatestRunTimestamp('last_hashrates_indexing', 0); await this.$setLatestRun('last_hashrates_indexing', 0);
await this.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); await this.$setLatestRun('last_weekly_hashrates_indexing', 0);
} catch (e) { } catch (e) {
logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e));
} }
@ -205,8 +205,8 @@ class HashratesRepository {
try { try {
await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp >= FROM_UNIXTIME(?)`, [timestamp]); await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp >= FROM_UNIXTIME(?)`, [timestamp]);
// Re-run the hashrate indexing to fill up missing data // Re-run the hashrate indexing to fill up missing data
await this.$setLatestRunTimestamp('last_hashrates_indexing', 0); await this.$setLatestRun('last_hashrates_indexing', 0);
await this.$setLatestRunTimestamp('last_weekly_hashrates_indexing', 0); await this.$setLatestRun('last_weekly_hashrates_indexing', 0);
} catch (e) { } catch (e) {
logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e));
} }