From d46e649877854a257ef15f5a00ba58cab3426fd7 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 15 May 2024 13:48:44 +0300 Subject: [PATCH 1/7] Add interrupter param to BfxApiRouter --- workers/loc.api/bfx.api.router/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workers/loc.api/bfx.api.router/index.js b/workers/loc.api/bfx.api.router/index.js index 083aff11..8badc274 100644 --- a/workers/loc.api/bfx.api.router/index.js +++ b/workers/loc.api/bfx.api.router/index.js @@ -4,7 +4,7 @@ const { decorateInjectable } = require('../di/utils') class BfxApiRouter { // Method does an idle job to be overridden in framework mode - route (methodName, method) { + route (methodName, method, interrupter) { return method() } } From 97c82ed04edf521e4717f344578f53ecbee46898 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 15 May 2024 13:50:54 +0300 Subject: [PATCH 2/7] Provide interrupter into getRest service --- workers/loc.api/helpers/get-rest.js | 41 ++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/workers/loc.api/helpers/get-rest.js b/workers/loc.api/helpers/get-rest.js index a4d98c47..85fa8e57 100644 --- a/workers/loc.api/helpers/get-rest.js +++ b/workers/loc.api/helpers/get-rest.js @@ -41,18 +41,25 @@ const _bfxFactory = (conf) => { }) } -const _route = (bfxApiRouter, methodName, args) => { +const _route = (bfxApiRouter, methodName, args, interrupter) => { if (!(bfxApiRouter instanceof BfxApiRouter)) { return Reflect.apply(...args) } return bfxApiRouter.route( methodName, - () => Reflect.apply(...args) + () => Reflect.apply(...args), + interrupter ) } -const _asyncApplyHook = async (bfxApiRouter, incomingRes, propKey, ...args) => { +const _asyncApplyHook = async ( + bfxApiRouter, + incomingRes, + propKey, + args, + interrupter +) => { let attemptsCount = 0 let caughtErr = null @@ -70,7 +77,8 @@ const _asyncApplyHook = async (bfxApiRouter, incomingRes, propKey, ...args) => { const res = await _route( bfxApiRouter, propKey, - args + args, + interrupter ) return res @@ -99,7 +107,11 @@ const _isNotPromiseOrBluebird = (instance) => ( ) ) -const _getRestProxy = (rest, bfxApiRouter) => { +const _getRestProxy = (rest, deps) => { + const { + bfxApiRouter, + interrupter + } = deps return new Proxy(rest, { get (target, propKey) { if (typeof target[propKey] !== 'function') { @@ -121,14 +133,21 @@ const _getRestProxy = (rest, bfxApiRouter) => { const res = _route( bfxApiRouter, propKey, - args + args, + interrupter ) if (_isNotPromiseOrBluebird(res)) { return res } - return _asyncApplyHook(bfxApiRouter, res, propKey, ...args) + return _asyncApplyHook( + bfxApiRouter, + res, + propKey, + args, + interrupter + ) } catch (err) { if (isNonceSmallError(err)) { attemptsCount += 1 @@ -165,7 +184,8 @@ module.exports = (conf, bfxApiRouter) => { authToken: _authToken = '' } = auth const { - timeout = 90000 + timeout = 90000, + interrupter } = opts ?? {} /* @@ -185,7 +205,10 @@ module.exports = (conf, bfxApiRouter) => { } const rest = bfxInstance.rest(2, restOpts) - const proxy = _getRestProxy(rest, bfxApiRouter) + const proxy = _getRestProxy(rest, { + bfxApiRouter, + interrupter + }) return proxy } From 7bc59dc22bea1ee1838c495910922d88d7236ae8 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 15 May 2024 13:52:06 +0300 Subject: [PATCH 3/7] Pass interrupter via get-data-from-api module --- workers/loc.api/helpers/get-data-from-api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/workers/loc.api/helpers/get-data-from-api.js b/workers/loc.api/helpers/get-data-from-api.js index 21011e17..f2fee277 100644 --- a/workers/loc.api/helpers/get-data-from-api.js +++ b/workers/loc.api/helpers/get-data-from-api.js @@ -88,6 +88,7 @@ module.exports = ( * due to an internet connection issue */ _args.shouldNotBeLoggedToStdErrorStream = true + _args.interrupter = _interrupter if ( typeof getData === 'string' && From 9a994d1ec0cbfab1f6d94b7fa44cf93f99b9ca43 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 16 May 2024 13:15:21 +0300 Subject: [PATCH 4/7] Turn off interrupter for csv --- workers/loc.api/queue/write-data-to-stream/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/workers/loc.api/queue/write-data-to-stream/index.js b/workers/loc.api/queue/write-data-to-stream/index.js index 5e128f94..1f9bd2e8 100644 --- a/workers/loc.api/queue/write-data-to-stream/index.js +++ b/workers/loc.api/queue/write-data-to-stream/index.js @@ -49,7 +49,8 @@ module.exports = ( const _res = await getDataFromApi({ getData, args: currIterationArgs, - callerName: 'REPORT_FILE_WRITER' + callerName: 'REPORT_FILE_WRITER', + shouldNotInterrupt: true }) const isGetWalletsMethod = method === 'getWallets' From 9627bdc9cc9a1063eaf5a58c690cd2f5b5ee81c3 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 16 May 2024 13:16:37 +0300 Subject: [PATCH 5/7] Turn off interrupter for weighted averages report csv writer --- .../csv-writer/weighted-averages-report-csv-writer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/workers/loc.api/generate-report-file/csv-writer/weighted-averages-report-csv-writer.js b/workers/loc.api/generate-report-file/csv-writer/weighted-averages-report-csv-writer.js index 15134bdf..99288db6 100644 --- a/workers/loc.api/generate-report-file/csv-writer/weighted-averages-report-csv-writer.js +++ b/workers/loc.api/generate-report-file/csv-writer/weighted-averages-report-csv-writer.js @@ -41,7 +41,8 @@ module.exports = ( const { res } = await getDataFromApi({ getData: rService[name].bind(rService), args, - callerName: 'CSV_WRITER' + callerName: 'CSV_WRITER', + shouldNotInterrupt: true }) wStream.setMaxListeners(50) From 2393d220ac2bb23626f001f9d01c8c2107e4cac9 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 16 May 2024 13:18:56 +0300 Subject: [PATCH 6/7] Provide interrupter for get-rest service --- workers/loc.api/service.report.js | 98 ++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/workers/loc.api/service.report.js b/workers/loc.api/service.report.js index 5c1399b2..090a7413 100644 --- a/workers/loc.api/service.report.js +++ b/workers/loc.api/service.report.js @@ -26,7 +26,9 @@ class ReportService extends Api { } _generateToken (args, opts) { - const rest = this._getREST(args?.auth) + const rest = this._getREST(args?.auth, { + interrupter: args?.interrupter + }) return rest.generateToken({ ttl: opts?.ttl ?? 3600, @@ -38,26 +40,34 @@ class ReportService extends Api { } _invalidateAuthToken (args) { - const rest = this._getREST(args?.auth) + const rest = this._getREST(args?.auth, { + interrupter: args?.interrupter + }) const { authToken } = args?.params ?? {} return rest.invalidateAuthToken({ authToken }) } _getUserInfo (args) { - const rest = this._getREST(args.auth) + const rest = this._getREST(args.auth, { + interrupter: args?.interrupter + }) return rest.userInfo() } - _getSymbols () { - const rest = this._getREST({}) + _getSymbols (args) { + const rest = this._getREST({}, { + interrupter: args?.interrupter + }) return rest.symbols() } - _getFutures () { - const rest = this._getREST({}) + _getFutures (args) { + const rest = this._getREST({}, { + interrupter: args?.interrupter + }) return rest.futures() } @@ -68,41 +78,46 @@ class ReportService extends Api { return rest.currencies() } - _getInactiveSymbols () { - const rest = this._getREST({}) + _getInactiveSymbols (args) { + const rest = this._getREST({}, { + interrupter: args?.interrupter + }) return rest.inactiveSymbols() } - async _getConf (opts) { - const { keys: _keys } = opts ?? {} + async _getConf (args) { + const { keys: _keys, interrupter } = args ?? {} const keys = Array.isArray(_keys) ? _keys : [_keys] - const rest = this._getREST({}) + const rest = this._getREST({}, { interrupter }) const res = await rest.conf({ keys }) return Array.isArray(res) ? res : [] } - async _getMapSymbols () { + async _getMapSymbols (args) { const [res] = await this._getConf({ - keys: 'pub:map:pair:sym' + keys: 'pub:map:pair:sym', + interrupter: args?.interrupter }) return Array.isArray(res) ? res : [] } - async _getInactiveCurrencies () { + async _getInactiveCurrencies (args) { const [res] = await this._getConf({ - keys: 'pub:list:currency:inactive' + keys: 'pub:list:currency:inactive', + interrupter: args?.interrupter }) return Array.isArray(res) ? res : [] } - async _getMarginCurrencyList () { + async _getMarginCurrencyList (args) { const [res] = await this._getConf({ - keys: 'pub:list:currency:margin' + keys: 'pub:list:currency:margin', + interrupter: args?.interrupter }) return Array.isArray(res) ? res : [] @@ -111,7 +126,9 @@ class ReportService extends Api { _getWeightedAveragesReportFromApi (args) { const { auth, params } = args ?? {} - const rest = this._getREST(auth) + const rest = this._getREST(auth, { + interrupter: args?.interrupter + }) return rest.getWeightedAverages(params) } @@ -218,13 +235,13 @@ class ReportService extends Api { inactiveCurrencies, marginCurrencyList ] = await Promise.all([ - this._getSymbols(), - this._getFutures(), - this._getCurrencies(), - this._getInactiveSymbols(), - this._getMapSymbols(), - this._getInactiveCurrencies(), - this._getMarginCurrencyList() + this._getSymbols(args), + this._getFutures(args), + this._getCurrencies(args), + this._getInactiveSymbols(args), + this._getMapSymbols(args), + this._getInactiveCurrencies(args), + this._getMarginCurrencyList(args) ]) const res = prepareSymbolResponse({ @@ -248,7 +265,9 @@ class ReportService extends Api { const { auth, params } = args ?? {} const { keys = [] } = params ?? {} - const rest = this._getREST(auth) + const rest = this._getREST(auth, { + interrupter: args?.interrupter + }) return rest.getSettings({ keys }) }, 'getSettings', args, cb) @@ -259,7 +278,9 @@ class ReportService extends Api { const { auth, params } = args ?? {} const { settings = {} } = params ?? {} - const rest = this._getREST(auth) + const rest = this._getREST(auth, { + interrupter: args?.interrupter + }) return rest.updateSettings({ settings }) }, 'updateSettings', args, cb) @@ -311,7 +332,9 @@ class ReportService extends Api { getActivePositions (space, args, cb) { return this._responder(async () => { - const rest = this._getREST(args.auth) + const rest = this._getREST(args.auth, { + interrupter: args?.interrupter + }) const positions = omitPrivateModelFields( await rest.positions() ) @@ -341,7 +364,9 @@ class ReportService extends Api { return this._responder(async () => { checkParams(args, 'paramsSchemaForWallets') - const rest = this._getREST(args.auth) + const rest = this._getREST(args.auth, { + interrupter: args?.interrupter + }) return omitPrivateModelFields(await rest.wallets()) }, 'getWallets', args, cb) @@ -510,7 +535,9 @@ class ReportService extends Api { getActiveOrders (space, args, cb) { return this._responder(async () => { - const rest = this._getREST(args.auth) + const rest = this._getREST(args.auth, { + interrupter: args?.interrupter + }) const _res = omitPrivateModelFields( await rest.activeOrders() @@ -537,8 +564,8 @@ class ReportService extends Api { return this._responder(async () => { checkParams(args, 'paramsSchemaForMovementInfo', ['id']) - const { auth, params } = args ?? {} - const rest = this._getREST(auth) + const { auth, params, interrupter } = args ?? {} + const rest = this._getREST(auth, { interrupter }) const res = omitPrivateModelFields( await rest.movementInfo({ id: params?.id }) @@ -604,7 +631,10 @@ class ReportService extends Api { getAccountSummary (space, args, cb) { return this._responder(async () => { const { auth } = { ...args } - const rest = this._getREST(auth, { timeout: 30000 }) + const rest = this._getREST(auth, { + timeout: 30000, + interrupter: args?.interrupter + }) const res = omitPrivateModelFields( await rest.accountSummary() From 2303e7e335c99287625f206cc31c9939e33d1727 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 16 May 2024 13:19:41 +0300 Subject: [PATCH 7/7] Provide interrupter for prepare-response service --- workers/loc.api/helpers/prepare-response/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/helpers/prepare-response/index.js b/workers/loc.api/helpers/prepare-response/index.js index b9d5ffa5..a3817bee 100644 --- a/workers/loc.api/helpers/prepare-response/index.js +++ b/workers/loc.api/helpers/prepare-response/index.js @@ -22,9 +22,10 @@ const _requestToApi = ( getREST, apiMethodName, params, - auth + auth, + interrupter ) => { - const rest = getREST(auth) + const rest = getREST(auth, { interrupter }) const bfxApiMethodName = getBfxApiMethodName(apiMethodName) const fn = rest[bfxApiMethodName].bind(rest) @@ -55,7 +56,8 @@ const _getResAndParams = async ( getREST, apiMethodName, queryParams, - args.auth + args.auth, + args?.interrupter ) return {