mirror of
https://github.com/mempool/mempool.git
synced 2025-03-03 17:47:01 +01:00
Wait for external assets file writers to complete
This commit is contained in:
parent
e8d6872620
commit
0565471ecf
5 changed files with 63 additions and 51 deletions
|
@ -77,7 +77,7 @@ export class Common {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static sleep(ms: number): Promise<void> {
|
static sleep$(ms: number): Promise<void> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve();
|
resolve();
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import DB from '../database';
|
import DB from '../database';
|
||||||
import logger from '../logger';
|
import logger from '../logger';
|
||||||
|
import { Common } from './common';
|
||||||
const sleep = (ms: number) => new Promise(res => setTimeout(res, ms));
|
|
||||||
|
|
||||||
class DatabaseMigration {
|
class DatabaseMigration {
|
||||||
private static currentVersion = 17;
|
private static currentVersion = 17;
|
||||||
|
@ -25,7 +24,7 @@ class DatabaseMigration {
|
||||||
await this.$createMigrationStateTable();
|
await this.$createMigrationStateTable();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.err('MIGRATIONS: Unable to create `state` table, aborting in 10 seconds. ' + e);
|
logger.err('MIGRATIONS: Unable to create `state` table, aborting in 10 seconds. ' + e);
|
||||||
await sleep(10000);
|
await Common.sleep$(10000);
|
||||||
process.exit(-1);
|
process.exit(-1);
|
||||||
}
|
}
|
||||||
logger.debug('MIGRATIONS: `state` table initialized.');
|
logger.debug('MIGRATIONS: `state` table initialized.');
|
||||||
|
@ -36,7 +35,7 @@ class DatabaseMigration {
|
||||||
databaseSchemaVersion = await this.$getSchemaVersionFromDatabase();
|
databaseSchemaVersion = await this.$getSchemaVersionFromDatabase();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.err('MIGRATIONS: Unable to get current database migration version, aborting in 10 seconds. ' + e);
|
logger.err('MIGRATIONS: Unable to get current database migration version, aborting in 10 seconds. ' + e);
|
||||||
await sleep(10000);
|
await Common.sleep$(10000);
|
||||||
process.exit(-1);
|
process.exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ class DatabaseMigration {
|
||||||
await this.$createMissingTablesAndIndexes(databaseSchemaVersion);
|
await this.$createMissingTablesAndIndexes(databaseSchemaVersion);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.err('MIGRATIONS: Unable to create required tables, aborting in 10 seconds. ' + e);
|
logger.err('MIGRATIONS: Unable to create required tables, aborting in 10 seconds. ' + e);
|
||||||
await sleep(10000);
|
await Common.sleep$(10000);
|
||||||
process.exit(-1);
|
process.exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import config from '../../config';
|
|
||||||
import logger from '../../logger';
|
import logger from '../../logger';
|
||||||
|
|
||||||
class Icons {
|
class Icons {
|
||||||
|
|
|
@ -85,16 +85,16 @@ class Server {
|
||||||
|
|
||||||
this.setUpWebsocketHandling();
|
this.setUpWebsocketHandling();
|
||||||
|
|
||||||
await syncAssets.syncAssets();
|
await syncAssets.syncAssets$();
|
||||||
diskCache.loadMempoolCache();
|
diskCache.loadMempoolCache();
|
||||||
|
|
||||||
if (config.DATABASE.ENABLED) {
|
if (config.DATABASE.ENABLED) {
|
||||||
await DB.checkDbConnection();
|
await DB.checkDbConnection();
|
||||||
try {
|
try {
|
||||||
if (process.env.npm_config_reindex != undefined) { // Re-index requests
|
if (process.env.npm_config_reindex !== undefined) { // Re-index requests
|
||||||
const tables = process.env.npm_config_reindex.split(',');
|
const tables = process.env.npm_config_reindex.split(',');
|
||||||
logger.warn(`Indexed data for "${process.env.npm_config_reindex}" tables will be erased in 5 seconds (using '--reindex')`);
|
logger.warn(`Indexed data for "${process.env.npm_config_reindex}" tables will be erased in 5 seconds (using '--reindex')`);
|
||||||
await Common.sleep(5000);
|
await Common.sleep$(5000);
|
||||||
await databaseMigration.$truncateIndexedData(tables);
|
await databaseMigration.$truncateIndexedData(tables);
|
||||||
}
|
}
|
||||||
await databaseMigration.$initializeOrMigrateDatabase();
|
await databaseMigration.$initializeOrMigrateDatabase();
|
||||||
|
|
|
@ -9,55 +9,69 @@ const PATH = './';
|
||||||
class SyncAssets {
|
class SyncAssets {
|
||||||
constructor() { }
|
constructor() { }
|
||||||
|
|
||||||
public async syncAssets() {
|
public async syncAssets$() {
|
||||||
for (const url of config.MEMPOOL.EXTERNAL_ASSETS) {
|
for (const url of config.MEMPOOL.EXTERNAL_ASSETS) {
|
||||||
await this.downloadFile(url);
|
try {
|
||||||
|
await this.downloadFile$(url);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Failed to download external asset. ` + (e instanceof Error ? e.message : e));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async downloadFile(url: string) {
|
private async downloadFile$(url: string) {
|
||||||
const fileName = url.split('/').slice(-1)[0];
|
return new Promise((resolve, reject) => {
|
||||||
|
const fileName = url.split('/').slice(-1)[0];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (config.SOCKS5PROXY.ENABLED) {
|
if (config.SOCKS5PROXY.ENABLED) {
|
||||||
let socksOptions: any = {
|
const socksOptions: any = {
|
||||||
agentOptions: {
|
agentOptions: {
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
},
|
},
|
||||||
host: config.SOCKS5PROXY.HOST,
|
host: config.SOCKS5PROXY.HOST,
|
||||||
port: config.SOCKS5PROXY.PORT
|
port: config.SOCKS5PROXY.PORT
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config.SOCKS5PROXY.USERNAME && config.SOCKS5PROXY.PASSWORD) {
|
if (config.SOCKS5PROXY.USERNAME && config.SOCKS5PROXY.PASSWORD) {
|
||||||
socksOptions.username = config.SOCKS5PROXY.USERNAME;
|
socksOptions.username = config.SOCKS5PROXY.USERNAME;
|
||||||
socksOptions.password = config.SOCKS5PROXY.PASSWORD;
|
socksOptions.password = config.SOCKS5PROXY.PASSWORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
const agent = new SocksProxyAgent(socksOptions);
|
||||||
|
|
||||||
|
logger.info(`Downloading external asset ${fileName} over the Tor network...`);
|
||||||
|
return axios.get(url, {
|
||||||
|
httpAgent: agent,
|
||||||
|
httpsAgent: agent,
|
||||||
|
responseType: 'stream',
|
||||||
|
timeout: 30000
|
||||||
|
}).then(function (response) {
|
||||||
|
const writer = fs.createWriteStream(PATH + fileName);
|
||||||
|
writer.on('finish', () => {
|
||||||
|
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
||||||
|
resolve(0);
|
||||||
|
});
|
||||||
|
response.data.pipe(writer);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.info(`Downloading external asset ${fileName} over clearnet...`);
|
||||||
|
return axios.get(url, {
|
||||||
|
responseType: 'stream',
|
||||||
|
timeout: 30000
|
||||||
|
}).then(function (response) {
|
||||||
|
const writer = fs.createWriteStream(PATH + fileName);
|
||||||
|
writer.on('finish', () => {
|
||||||
|
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
||||||
|
resolve(0);
|
||||||
|
});
|
||||||
|
response.data.pipe(writer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
} catch (e: any) {
|
||||||
const agent = new SocksProxyAgent(socksOptions);
|
reject(e);
|
||||||
|
|
||||||
logger.info(`Downloading external asset ${fileName} over the Tor network...`);
|
|
||||||
await axios.get(url, {
|
|
||||||
httpAgent: agent,
|
|
||||||
httpsAgent: agent,
|
|
||||||
responseType: 'stream',
|
|
||||||
timeout: 30000
|
|
||||||
}).then(function (response) {
|
|
||||||
response.data.pipe(fs.createWriteStream(PATH + fileName));
|
|
||||||
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
logger.info(`Downloading external asset ${fileName} over clearnet...`);
|
|
||||||
await axios.get(url, {
|
|
||||||
responseType: 'stream',
|
|
||||||
timeout: 30000
|
|
||||||
}).then(function (response) {
|
|
||||||
response.data.pipe(fs.createWriteStream(PATH + fileName));
|
|
||||||
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
});
|
||||||
throw new Error(`Failed to download external asset. ` + e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue