From 485cd12d3edba9ad5fc046fb7569aa49086d76e8 Mon Sep 17 00:00:00 2001 From: kenshin-samourai Date: Fri, 2 Oct 2020 22:22:26 +0200 Subject: [PATCH] improve management of timeouts during xpub imports and rescans --- static/admin/dmt/xpubs-tools/xpubs-tools.js | 53 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/static/admin/dmt/xpubs-tools/xpubs-tools.js b/static/admin/dmt/xpubs-tools/xpubs-tools.js index b179079..7f02d1c 100644 --- a/static/admin/dmt/xpubs-tools/xpubs-tools.js +++ b/static/admin/dmt/xpubs-tools/xpubs-tools.js @@ -3,6 +3,7 @@ const screenXpubsToolsScript = { explorerInfo: null, currentXpub: null, isReimport: false, + rescanStatusTimerId: null, initPage: function() { this.getExplorerInfo() @@ -75,7 +76,7 @@ const screenXpubsToolsScript = { }, importXpub: function() { - lib_msg.displayMessage('Processing xpub import...'); + lib_msg.displayMessage('Processing xpub import. Please wait...'); const jsonData = { 'xpub': this.currentXpub, @@ -95,31 +96,73 @@ const screenXpubsToolsScript = { return lib_api.postXpub(jsonData) .then(result => { + // Successful import this._searchXpub(this.currentXpub).then(() => { lib_msg.displayInfo('Import complete') }) }).catch(e => { - lib_errors.processError(e) + // Check if import has timeout'd or if we have an error + if (e['status'] == 502 || e['status'] == 504) { + // Wait for import completion + this.checkRescanStatus(() => { + this._searchXpub(this.currentXpub).then(() => { + lib_msg.displayInfo('Import complete') + }) + }) + } else { + lib_errors.processError(e) + } }) }, rescanXpub: function() { - lib_msg.displayMessage('Processing xpub rescan...'); + lib_msg.displayMessage('Processing xpub rescan. Please wait...'); let startIdx = $('#rescan-start-idx').val() startIdx = (startIdx == null) ? 0 : parseInt(startIdx) let lookahead = $('#rescan-lookahead').val() lookahead = (lookahead == null) ? 100 : parseInt(lookahead) return lib_api.getXpubRescan(this.currentXpub, lookahead, startIdx) - .then(result => { + .then(() => { + // Successful rescan this.hideRescanForm() this._searchXpub(this.currentXpub).then(() => { lib_msg.displayInfo('Rescan complete') }) }).catch(e => { - lib_errors.processError(e) + // Check if rescan has timeout'd or if we have an error + if (e['status'] == 502 || e['status'] == 504) { + // Wait for rescan completion + this.checkRescanStatus(() => { + this.hideRescanForm() + this._searchXpub(this.currentXpub).then(() => { + lib_msg.displayInfo('Rescan complete') + }) + }) + } else { + lib_errors.processError(e) + } }) }, + checkRescanStatus: function(callback) { + this.rescanStatusTimerId = setTimeout(() => { + lib_api.getXpubRescanStatus(this.currentXpub) + .then(result => { + if (result['data']['import_in_progress']) { + lib_msg.displayMessage('Rescan in progress. Please wait...'); + return this.checkRescanStatus(callback) + } else { + clearTimeout(this.rescanStatusTimerId) + return callback() + } + }).catch(e => { + lib_errors.processError(e) + lib_msg.displayMessage('Rescan in progress. Please wait...'); + return this.checkRescanStatus(callback) + }) + }, 10000) + }, + setXpubDetails: function(xpubInfo) { $('tr.tx-row').remove() $('tr.utxo-row').remove()