Skip to content

Commit 4596394

Browse files
committed
[mining] pool updater is now self contained service
1 parent ae2ed8f commit 4596394

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

backend/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ class Server {
211211
}
212212
});
213213
}
214+
215+
poolsUpdater.$startService();
214216
}
215217

216218
async runMainUpdateLoop(): Promise<void> {
@@ -239,7 +241,6 @@ class Server {
239241
if (config.FIAT_PRICE.ENABLED) {
240242
priceUpdater.$run();
241243
}
242-
await poolsUpdater.updatePoolsJson();
243244

244245
// rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS
245246
const elapsed = Date.now() - start;

backend/src/tasks/pools-updater.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@ import backendInfo from '../api/backend-info';
66
import logger from '../logger';
77
import { SocksProxyAgent } from 'socks-proxy-agent';
88
import * as https from 'https';
9+
import { Common } from '../api/common';
910

1011
/**
1112
* Maintain the most recent version of pools-v2.json
1213
*/
1314
class PoolsUpdater {
15+
tag = 'PoolsUpdater';
16+
1417
lastRun: number = 0;
1518
currentSha: string | null = null;
1619
poolsUrl: string = config.MEMPOOL.POOLS_JSON_URL;
1720
treeUrl: string = config.MEMPOOL.POOLS_JSON_TREE_URL;
1821

22+
public async $startService(): Promise<void> {
23+
while ('Bitcoin is still alive') {
24+
try {
25+
await this.updatePoolsJson();
26+
} catch (e: any) {
27+
logger.info(`Exception ${e} in PoolsUpdater::$startService. Code: ${e.code}. Message: ${e.message}`, this.tag);
28+
}
29+
await Common.sleep$(10000);
30+
}
31+
}
32+
1933
public async updatePoolsJson(): Promise<void> {
2034
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false ||
2135
config.MEMPOOL.ENABLED === false
@@ -40,7 +54,7 @@ class PoolsUpdater {
4054
this.currentSha = await this.getShaFromDb();
4155
}
4256

43-
logger.debug(`pools-v2.json sha | Current: ${this.currentSha} | Github: ${githubSha}`);
57+
logger.debug(`pools-v2.json sha | Current: ${this.currentSha} | Github: ${githubSha}`, this.tag);
4458
if (this.currentSha !== null && this.currentSha === githubSha) {
4559
return;
4660
}
@@ -50,16 +64,16 @@ class PoolsUpdater {
5064
config.MEMPOOL.AUTOMATIC_POOLS_UPDATE !== true && // Automatic pools update is disabled
5165
!process.env.npm_config_update_pools // We're not manually updating mining pool
5266
) {
53-
logger.warn(`Updated mining pools data is available (${githubSha}) but AUTOMATIC_POOLS_UPDATE is disabled`);
54-
logger.info(`You can update your mining pools using the --update-pools command flag. You may want to clear your nginx cache as well if applicable`);
67+
logger.warn(`Updated mining pools data is available (${githubSha}) but AUTOMATIC_POOLS_UPDATE is disabled`, this.tag);
68+
logger.info(`You can update your mining pools using the --update-pools command flag. You may want to clear your nginx cache as well if applicable`, this.tag);
5569
return;
5670
}
5771

5872
const network = config.SOCKS5PROXY.ENABLED ? 'tor' : 'clearnet';
5973
if (this.currentSha === null) {
60-
logger.info(`Downloading pools-v2.json for the first time from ${this.poolsUrl} over ${network}`, logger.tags.mining);
74+
logger.info(`Downloading pools-v2.json for the first time from ${this.poolsUrl} over ${network}`, this.tag);
6175
} else {
62-
logger.warn(`pools-v2.json is outdated, fetching latest from ${this.poolsUrl} over ${network}`, logger.tags.mining);
76+
logger.warn(`pools-v2.json is outdated, fetching latest from ${this.poolsUrl} over ${network}`, this.tag);
6377
}
6478
const poolsJson = await this.query(this.poolsUrl);
6579
if (poolsJson === undefined) {
@@ -68,7 +82,7 @@ class PoolsUpdater {
6882
poolsParser.setMiningPools(poolsJson);
6983

7084
if (config.DATABASE.ENABLED === false) { // Don't run db operations
71-
logger.info(`Mining pools-v2.json (${githubSha}) import completed (no database)`);
85+
logger.info(`Mining pools-v2.json (${githubSha}) import completed (no database)`, this.tag);
7286
return;
7387
}
7488

@@ -78,14 +92,14 @@ class PoolsUpdater {
7892
await this.updateDBSha(githubSha);
7993
await DB.query('COMMIT;');
8094
} catch (e) {
81-
logger.err(`Could not migrate mining pools, rolling back. Exception: ${JSON.stringify(e)}`, logger.tags.mining);
95+
logger.err(`Could not migrate mining pools, rolling back. Exception: ${JSON.stringify(e)}`, this.tag);
8296
await DB.query('ROLLBACK;');
8397
}
84-
logger.info(`Mining pools-v2.json (${githubSha}) import completed`);
98+
logger.info(`Mining pools-v2.json (${githubSha}) import completed`, this.tag);
8599

86100
} catch (e) {
87101
this.lastRun = now - 600; // Try again in 10 minutes
88-
logger.err(`PoolsUpdater failed. Will try again in 10 minutes. Exception: ${JSON.stringify(e)}`, logger.tags.mining);
102+
logger.err(`PoolsUpdater failed. Will try again in 10 minutes. Exception: ${JSON.stringify(e)}`, this.tag);
89103
}
90104
}
91105

@@ -99,7 +113,7 @@ class PoolsUpdater {
99113
await DB.query('DELETE FROM state where name="pools_json_sha"');
100114
await DB.query(`INSERT INTO state VALUES('pools_json_sha', NULL, '${githubSha}')`);
101115
} catch (e) {
102-
logger.err('Cannot save github pools-v2.json sha into the db. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining);
116+
logger.err('Cannot save github pools-v2.json sha into the db. Reason: ' + (e instanceof Error ? e.message : e), this.tag);
103117
}
104118
}
105119
}
@@ -112,7 +126,7 @@ class PoolsUpdater {
112126
const [rows]: any[] = await DB.query('SELECT string FROM state WHERE name="pools_json_sha"');
113127
return (rows.length > 0 ? rows[0].string : null);
114128
} catch (e) {
115-
logger.err('Cannot fetch pools-v2.json sha from db. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining);
129+
logger.err('Cannot fetch pools-v2.json sha from db. Reason: ' + (e instanceof Error ? e.message : e), this.tag);
116130
return null;
117131
}
118132
}
@@ -131,7 +145,7 @@ class PoolsUpdater {
131145
}
132146
}
133147

134-
logger.err(`Cannot find "pools-v2.json" in git tree (${this.treeUrl})`, logger.tags.mining);
148+
logger.err(`Cannot find "pools-v2.json" in git tree (${this.treeUrl})`, this.tag);
135149
return null;
136150
}
137151

@@ -183,7 +197,7 @@ class PoolsUpdater {
183197
}
184198
return data.data;
185199
} catch (e) {
186-
logger.err('Could not connect to Github. Reason: ' + (e instanceof Error ? e.message : e));
200+
logger.err('Could not connect to Github. Reason: ' + (e instanceof Error ? e.message : e), this.tag);
187201
retry++;
188202
}
189203
await setDelay(config.MEMPOOL.EXTERNAL_RETRY_INTERVAL);

0 commit comments

Comments
 (0)