From 66c565a3d7028bb6597d2f3469ebf4538aa8978a Mon Sep 17 00:00:00 2001 From: softsimon Date: Sat, 18 Jul 2020 18:17:24 +0700 Subject: [PATCH] Adding multiple fs.watch-ers to handle Bisq restarts. --- backend/src/api/bisq.ts | 45 ++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/backend/src/api/bisq.ts b/backend/src/api/bisq.ts index 74128ce37..e5f9e11c4 100644 --- a/backend/src/api/bisq.ts +++ b/backend/src/api/bisq.ts @@ -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); }