Run hashrate indexing after midnight
This commit is contained in:
		
							parent
							
								
									bf314be7eb
								
							
						
					
					
						commit
						b56f110f28
					
				@ -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`);
 | 
			
		||||
 | 
			
		||||
@ -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));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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<number> {
 | 
			
		||||
  public async $getLatestRun(key: string): Promise<number> {
 | 
			
		||||
    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));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user