Handle errors gracefully.

This commit is contained in:
softsimon 2021-09-20 01:02:07 +04:00
parent 0d67bc36ee
commit f3c8e2134b
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7

View File

@ -6,24 +6,30 @@ import { DB } from '../../database';
import logger from '../../logger'; import logger from '../../logger';
class ElementsParser { class ElementsParser {
isRunning = false; private isRunning = false;
constructor() { } constructor() { }
public async $parse() { public async $parse() {
if (this.isRunning) { if (this.isRunning) {
return; return;
} }
this.isRunning = true; try {
const result = await bitcoinClient.getChainTips(); this.isRunning = true;
const tip = result[0].height; const result = await bitcoinClient.getChainTips();
const latestBlock = await this.$getLatestBlockFromDatabase(); const tip = result[0].height;
for (let height = latestBlock.block + 1; height <= tip; height++) { const latestBlock = await this.$getLatestBlockFromDatabase();
const blockHash: IBitcoinApi.ChainTips = await bitcoinClient.getBlockHash(height); for (let height = latestBlock.block + 1; height <= tip; height++) {
const block: IBitcoinApi.Block = await bitcoinClient.getBlock(blockHash, 2); const blockHash: IBitcoinApi.ChainTips = await bitcoinClient.getBlockHash(height);
await this.$parseBlock(block); const block: IBitcoinApi.Block = await bitcoinClient.getBlock(blockHash, 2);
await this.$saveLatestBlockToDatabase(block.height, block.time, block.hash); await this.$parseBlock(block);
await this.$saveLatestBlockToDatabase(block.height, block.time, block.hash);
}
this.isRunning = false;
} catch (e) {
this.isRunning = false;
throw new Error(e instanceof Error ? e.message : 'Error');
} }
this.isRunning = false;
} }
public async $getPegDataByMonth(): Promise<any> { public async $getPegDataByMonth(): Promise<any> {