Adding multiple fs.watch-ers to handle Bisq restarts.

This commit is contained in:
softsimon 2020-07-18 18:17:24 +07:00
parent 3df36cd3b8
commit 66c565a3d7
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7

View File

@ -20,25 +20,16 @@ class Bisq {
};
private price: number = 0;
private priceUpdateCallbackFunction: ((price: number) => void) | undefined;
private subdirectoryWatcher: fs.FSWatcher | undefined;
constructor() {}
startBisqService(): void {
this.loadBisqDumpFile();
let fsWait: NodeJS.Timeout | null = null;
fs.watch(config.BSQ_BLOCKS_DATA_PATH, { recursive: true }, () => {
if (fsWait) {
clearTimeout(fsWait);
}
fsWait = setTimeout(() => {
console.log(`Change detected in the Bisq data folder.`);
this.loadBisqDumpFile();
}, 1000);
});
setInterval(this.updatePrice.bind(this), 1000 * 60 * 60);
this.updatePrice();
this.startTopLevelDirectoryWatcher();
this.restartSubDirectoryWatcher();
}
getTransaction(txId: string): BisqTransaction | undefined {
@ -73,6 +64,36 @@ class Bisq {
return this.latestBlockHeight;
}
private startTopLevelDirectoryWatcher() {
let fsWait: NodeJS.Timeout | null = null;
fs.watch(config.BSQ_BLOCKS_DATA_PATH, () => {
if (fsWait) {
clearTimeout(fsWait);
}
fsWait = setTimeout(() => {
console.log(`Change detected in the top level Bisq data folder. Resetting inner watcher.`);
this.restartSubDirectoryWatcher();
}, 15000);
});
}
private restartSubDirectoryWatcher() {
if (this.subdirectoryWatcher) {
this.subdirectoryWatcher.close();
}
let fsWait: NodeJS.Timeout | null = null;
this.subdirectoryWatcher = fs.watch(config.BSQ_BLOCKS_DATA_PATH + '/all', () => {
if (fsWait) {
clearTimeout(fsWait);
}
fsWait = setTimeout(() => {
console.log(`Change detected in the Bisq data folder.`);
this.loadBisqDumpFile();
}, 2000);
});
}
private updatePrice() {
request('https://markets.bisq.network/api/trades/?market=bsq_btc', { json: true }, (err, res, trades: BisqTrade[]) => {
if (err) { return console.log(err); }