From 5125f95a1ee3551f515b490d82921ff9d58bf3ec Mon Sep 17 00:00:00 2001 From: Khalid Ibrahim Date: Tue, 14 May 2019 17:48:56 +0800 Subject: [PATCH 1/5] added exponential backoff retry for contract refresh --- src/botPage/view/TradeInfoPanel/TradeTable.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/botPage/view/TradeInfoPanel/TradeTable.js b/src/botPage/view/TradeInfoPanel/TradeTable.js index adb7881c28..ade7921094 100644 --- a/src/botPage/view/TradeInfoPanel/TradeTable.js +++ b/src/botPage/view/TradeInfoPanel/TradeTable.js @@ -107,9 +107,28 @@ export default class TradeTable extends Component { }); } registerTimeout = (api, contract) => { - setTimeout(() => { + const exponentialBackoff = (max, delay) => { + const { accountID } = this.props; + const rows = this.state[accountID].rows.slice(); const contractID = contract.contract_id; + this.refreshContract(api, contractID); + + let currentStatus = ''; + rows.forEach(row => { + if (row.contract_id === contractID) { + currentStatus = row.contract_status; + } + }); + + if (currentStatus !== translate('Settled') && max > 0) { + setTimeout(() => { + exponentialBackoff(max - 1, delay * 2); + }, delay * 1000); + } + }; + setTimeout(() => { + exponentialBackoff(5, 1); }, 3000); }; refreshContract(api, contractID) { From f2e1d1a55b3a4d226a3a7931d2473de43a40936b Mon Sep 17 00:00:00 2001 From: Khalid Ibrahim Date: Thu, 16 May 2019 13:56:09 +0800 Subject: [PATCH 2/5] implemented Aaron's changes --- src/botPage/view/TradeInfoPanel/TradeTable.js | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/botPage/view/TradeInfoPanel/TradeTable.js b/src/botPage/view/TradeInfoPanel/TradeTable.js index ade7921094..ffc77e2e02 100644 --- a/src/botPage/view/TradeInfoPanel/TradeTable.js +++ b/src/botPage/view/TradeInfoPanel/TradeTable.js @@ -85,8 +85,9 @@ export default class TradeTable extends Component { const trade = { ...tradeObj, - profit : getProfit(tradeObj), - contract_status: translate('Pending'), + profit : getProfit(tradeObj), + contract_status : translate('Pending'), + contract_settled: false, }; const accountStat = this.getAccountStat(accountID); @@ -103,36 +104,41 @@ export default class TradeTable extends Component { } }); globalObserver.register('contract.settled', contract => { - this.registerTimeout(api, contract); + const contractID = contract.contract_id; + this.settleContract(api, contractID); }); } - registerTimeout = (api, contract) => { - const exponentialBackoff = (max, delay) => { - const { accountID } = this.props; - const rows = this.state[accountID].rows.slice(); - const contractID = contract.contract_id; - this.refreshContract(api, contractID); + async settleContract(api, contractID) { + let settled = false; + let delay = 3000; - let currentStatus = ''; - rows.forEach(row => { - if (row.contract_id === contractID) { - currentStatus = row.contract_status; - } - }); + const sleep = d => new Promise(resolve => setTimeout(() => resolve(), d)); + + while (!settled) { + await sleep(delay); - if (currentStatus !== translate('Settled') && max > 0) { - setTimeout(() => { - exponentialBackoff(max - 1, delay * 2); - }, delay * 1000); + try { + alert('Calling API now...'); + await this.refreshContract(api, contractID); + + const { accountID } = this.props; + const rows = this.state[accountID].rows.slice(); + const contractRow = rows.find(row => row.contract_id === contractID); + + if (contractRow && contractRow.contract_settled) { + settled = true; + } + } catch (e) { + // Do nothing. Loop again. + } finally { + delay *= 1.5; } - }; - setTimeout(() => { - exponentialBackoff(5, 1); - }, 3000); - }; + } + } + refreshContract(api, contractID) { - api.getContractInfo(contractID).then(r => { + return api.getContractInfo(contractID).then(r => { const contract = r.proposal_open_contract; const timestamp = getTimestamp(contract.date_start); const tradeObj = { reference: contract.transaction_ids.buy, ...contract, timestamp }; @@ -151,7 +157,8 @@ export default class TradeTable extends Component { const { reference } = row; if (reference === trade.reference) { return { - contract_status: translate('Settled'), + contract_status : translate('Settled'), + contract_settled: true, reference, ...trade, }; From 2f2a6b631b0b9018963dc59a21095115821e6773 Mon Sep 17 00:00:00 2001 From: Khalid Ibrahim Date: Thu, 16 May 2019 14:02:17 +0800 Subject: [PATCH 3/5] removed eslint rule --- src/botPage/view/TradeInfoPanel/TradeTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/botPage/view/TradeInfoPanel/TradeTable.js b/src/botPage/view/TradeInfoPanel/TradeTable.js index ffc77e2e02..e347d5075c 100644 --- a/src/botPage/view/TradeInfoPanel/TradeTable.js +++ b/src/botPage/view/TradeInfoPanel/TradeTable.js @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ import json2csv from 'json2csv'; import React, { Component } from 'react'; import ReactDataGrid from 'react-data-grid'; @@ -119,7 +120,6 @@ export default class TradeTable extends Component { await sleep(delay); try { - alert('Calling API now...'); await this.refreshContract(api, contractID); const { accountID } = this.props; From f0071a7dc1ce177d22f11e34a8bf798474359ee3 Mon Sep 17 00:00:00 2001 From: Khalid Ibrahim Date: Thu, 16 May 2019 14:47:47 +0800 Subject: [PATCH 4/5] Apply suggestions from code review Co-Authored-By: Aaron --- src/botPage/view/TradeInfoPanel/TradeTable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/botPage/view/TradeInfoPanel/TradeTable.js b/src/botPage/view/TradeInfoPanel/TradeTable.js index e347d5075c..572e566a93 100644 --- a/src/botPage/view/TradeInfoPanel/TradeTable.js +++ b/src/botPage/view/TradeInfoPanel/TradeTable.js @@ -114,10 +114,10 @@ export default class TradeTable extends Component { let settled = false; let delay = 3000; - const sleep = d => new Promise(resolve => setTimeout(() => resolve(), d)); + const sleep = () => new Promise(resolve => setTimeout(() => resolve(), delay); while (!settled) { - await sleep(delay); + await sleep(); try { await this.refreshContract(api, contractID); From 4ade7937636eaa6f5eaafb284efecd7d672a8841 Mon Sep 17 00:00:00 2001 From: Khalid Ibrahim Date: Thu, 16 May 2019 14:58:16 +0800 Subject: [PATCH 5/5] typo fix --- src/botPage/view/TradeInfoPanel/TradeTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/botPage/view/TradeInfoPanel/TradeTable.js b/src/botPage/view/TradeInfoPanel/TradeTable.js index 572e566a93..1ea1347b36 100644 --- a/src/botPage/view/TradeInfoPanel/TradeTable.js +++ b/src/botPage/view/TradeInfoPanel/TradeTable.js @@ -114,7 +114,7 @@ export default class TradeTable extends Component { let settled = false; let delay = 3000; - const sleep = () => new Promise(resolve => setTimeout(() => resolve(), delay); + const sleep = () => new Promise(resolve => setTimeout(() => resolve(), delay)); while (!settled) { await sleep();