From 6c9a125ba5a3edab3c286ba4ba2227c341373755 Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Fri, 21 Jul 2023 15:38:38 +0000 Subject: [PATCH 1/4] Fix_for_OrionCB --- CHANGES_NEXT_RELEASE | 3 ++- lib/request-shim.js | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 82a711788..7b7f8b78b 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,4 @@ +- Fix: Payload measures are lost after reconnection with Orion (#1407) - Fix: check array access in extractVariables of jexlPlugin when bidirectionalPlugin is enabled - Fix: explicitAttrs of device was tainted even if not defined - Fix: do not include static, lazy and commands from group to device to avoid duplicate them in device (#1377) @@ -7,4 +8,4 @@ - Fix: use 'options=upsert' when update ngsiv2 CB entities and appendMode is enabled (#956) - Fix: do not propagate group config (timestamp and explicitAttrs) to autoprovisioned devices (at database level) (#1377) - Fix: appendMode at general level (config.js / env var) changes its default from false to true -- Fix: remove sensitive MongoDB connection parameters from log traces (remove 'option' object from logs) \ No newline at end of file +- Fix: remove sensitive MongoDB connection parameters from log traces (remove 'option' object from logs) diff --git a/lib/request-shim.js b/lib/request-shim.js index f1aab860e..d470297b1 100644 --- a/lib/request-shim.js +++ b/lib/request-shim.js @@ -52,7 +52,7 @@ function getOptions(options) { searchParams: options.searchParams || options.qs, headers: options.headers, throwHttpErrors: options.throwHttpErrors || false, - retry: options.retry || 0, + retry: options.retry || 5, responseType: options.responseType || 'json' }; @@ -95,18 +95,36 @@ function getOptions(options) { * */ +let retry_count = 0; //default retry_count value function request(options, callback) { const httpOptions = getOptions(options); logger.debug(context, 'Options: %s', JSON.stringify(options, null, 4)); got(options.url || options.uri, httpOptions) .then((response) => { logger.debug(context, 'Response %s', JSON.stringify(response.body, null, 4)); + retry_count = 0; return callback(null, response, response.body); }) .catch((error) => { - logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); - return callback(error); + if (retry_count == 0) { + logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); + } + if (error.code == 'ECONNREFUSED') { + if (retry_count < httpOptions.retry) { + retry_count++; + console.log('Retrying connection', JSON.stringify(retry_count)); + return setTimeout(request, 2000, options, callback); + } + //retrun the error + if (retry_count == httpOptions.retry) { + retry_count = 0; + return callback(error); + } + } + else{ + return callback(error); + } }); } -module.exports = request; +module.exports = request; \ No newline at end of file From 8c011dadb8ed585e7f86ca1fabb0a11388723c6f Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Fri, 11 Aug 2023 14:47:53 +0530 Subject: [PATCH 2/4] Fix_done --- config.js | 4 +++- lib/request-shim.js | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/config.js b/config.js index 9b93b86ad..5303526af 100644 --- a/config.js +++ b/config.js @@ -77,7 +77,9 @@ var config = { providerUrl: 'http://192.168.56.1:4041', deviceRegistrationDuration: 'P1M', defaultType: 'Thing', - appendMode: true + appendMode: true, + ORION_DEFAULT_RETRIES: 5, + ORION_DEFAULT_RETRY_TIME: 5 }; module.exports = config; diff --git a/lib/request-shim.js b/lib/request-shim.js index d470297b1..08dd449a4 100644 --- a/lib/request-shim.js +++ b/lib/request-shim.js @@ -20,7 +20,7 @@ * For those usages not covered by the GNU Affero General Public License * please contact with::daniel.moranjimenez@telefonica.com */ - +const config = require('../config'); const got = require('got'); const logger = require('logops'); const context = { @@ -52,7 +52,7 @@ function getOptions(options) { searchParams: options.searchParams || options.qs, headers: options.headers, throwHttpErrors: options.throwHttpErrors || false, - retry: options.retry || 5, + retry: options.retry || 0, responseType: options.responseType || 'json' }; @@ -95,29 +95,31 @@ function getOptions(options) { * */ -let retry_count = 0; //default retry_count value +let retryCount = 0; //default retry_count value function request(options, callback) { + retryTime = config.ORION_DEFAULT_RETRY_TIME; + retries = config.ORION_DEFAULT_RETRIES; const httpOptions = getOptions(options); logger.debug(context, 'Options: %s', JSON.stringify(options, null, 4)); got(options.url || options.uri, httpOptions) .then((response) => { logger.debug(context, 'Response %s', JSON.stringify(response.body, null, 4)); - retry_count = 0; + retryCount = 0; return callback(null, response, response.body); }) .catch((error) => { - if (retry_count == 0) { + if (retryCount === 0) { logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); } - if (error.code == 'ECONNREFUSED') { - if (retry_count < httpOptions.retry) { - retry_count++; - console.log('Retrying connection', JSON.stringify(retry_count)); - return setTimeout(request, 2000, options, callback); + if (error.code === 'ECONNREFUSED') { + if (retryCount < retries) { + retryCount++; + console.log('Retrying connection', JSON.stringify(retryCount)); + return setTimeout(request, retryTime * 1000 , options, callback); } //retrun the error - if (retry_count == httpOptions.retry) { - retry_count = 0; + if ( retryCount === retries) { + retryCount = 0; return callback(error); } } @@ -127,4 +129,4 @@ function request(options, callback) { }); } -module.exports = request; \ No newline at end of file +module.exports = request; From 7c644184e51d49f5216011d5d4813fcce86656cf Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Mon, 14 Aug 2023 11:08:45 +0000 Subject: [PATCH 3/4] RemoveLintError --- lib/request-shim.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/request-shim.js b/lib/request-shim.js index 08dd449a4..08a6ab21f 100644 --- a/lib/request-shim.js +++ b/lib/request-shim.js @@ -97,6 +97,8 @@ function getOptions(options) { let retryCount = 0; //default retry_count value function request(options, callback) { + let retryTime; + let retries; retryTime = config.ORION_DEFAULT_RETRY_TIME; retries = config.ORION_DEFAULT_RETRIES; const httpOptions = getOptions(options); @@ -108,9 +110,6 @@ function request(options, callback) { return callback(null, response, response.body); }) .catch((error) => { - if (retryCount === 0) { - logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); - } if (error.code === 'ECONNREFUSED') { if (retryCount < retries) { retryCount++; @@ -120,10 +119,12 @@ function request(options, callback) { //retrun the error if ( retryCount === retries) { retryCount = 0; + logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); return callback(error); } } else{ + logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); return callback(error); } }); From d1086d2bda2ca0f67f72721de3bb9efcea159523 Mon Sep 17 00:00:00 2001 From: Keshav-NEC Date: Tue, 22 Aug 2023 09:56:27 +0000 Subject: [PATCH 4/4] RebasedAndErrorFix --- lib/request-shim.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/request-shim.js b/lib/request-shim.js index 08a6ab21f..e76f6213b 100644 --- a/lib/request-shim.js +++ b/lib/request-shim.js @@ -97,10 +97,8 @@ function getOptions(options) { let retryCount = 0; //default retry_count value function request(options, callback) { - let retryTime; - let retries; - retryTime = config.ORION_DEFAULT_RETRY_TIME; - retries = config.ORION_DEFAULT_RETRIES; + const retryTime = config.ORION_DEFAULT_RETRY_TIME; + const retries = config.ORION_DEFAULT_RETRIES; const httpOptions = getOptions(options); logger.debug(context, 'Options: %s', JSON.stringify(options, null, 4)); got(options.url || options.uri, httpOptions) @@ -117,7 +115,7 @@ function request(options, callback) { return setTimeout(request, retryTime * 1000 , options, callback); } //retrun the error - if ( retryCount === retries) { + else { retryCount = 0; logger.debug(context, 'Error: %s', JSON.stringify(util.inspect(error), null, 4)); return callback(error);