diff --git a/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js b/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js index 7f67c7cdf..b13a52c9d 100644 --- a/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js +++ b/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js @@ -181,8 +181,12 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { calculateRecordPropertiesNodePathMap() getEndpointQueryParameters() getEndpointPathParameters() - await fetchAllPages() - await saveDataReceived() + + if (await fetchAllPages() !== 'RETRYING') { + await saveDataReceived() + } else { + return + } function getApiEndpointAndSchema() { /* @@ -361,8 +365,21 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { Async call to the API Server */ await sleep(apiMap.config.millisecondsBetweenCalls) - await fetchAPIData() - dataReceivedObject = apiData + let fetchResult = await fetchAPIData() + + if (fetchResult === 'NO_CONNECTION') { + /* + When there is not Internet Connection or the server can not be reached + we will return requesting a retry later. + */ + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] start -> fetchAllPages -> Server not found or no Internet Connection. Requesting a Retry. ") + callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) + return 'RETRYING' + } else { + dataReceivedObject = apiData + } + } else { /* There is a paging mechanism at this Endpoint, so we will iterate through all @@ -390,6 +407,16 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { let fetchResult = await fetchAPIData() switch (fetchResult) { + case 'NO_CONNECTION': { + /* + When there is not Internet Connection or the server can not be reached + we will return requesting a retry later. + */ + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] start -> fetchAllPages -> Server not found or no Internet Connection. Requesting a Retry. ") + callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_RETRY_RESPONSE) + return 'RETRYING' + } case 'UNEXPECTED_API_RESPONSE': { /* Any unexpected response will abort this loop and allow the process to continue, @@ -455,12 +482,9 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[INFO] start -> startProcess -> fetchAPIData -> httpClient.get -> url = " + url) - httpClient.get(url, onResponse) + httpClient.get(url, (response) => { - function onResponse(response) { const chunks = [] - response.on('data', onDataArrived) - response.on('end', onEnd) let apiResponseSchemaNode let errorCodeReceived @@ -487,10 +511,6 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { ) } - function onDataArrived(chunk) { - chunks.push(chunk) - } - function getApiResponseSchema(responseCode) { /* The API Response Schema we will depend on the response code received from the @@ -523,7 +543,11 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { } } - function onEnd() { + response.on('data', (chunk) => { + chunks.push(chunk) + }); + + response.on('end', (d) => { apiResponseReceivedText = Buffer.concat(chunks).toString('utf8') /* If we received an errror code, we abort the processing at this point. @@ -579,8 +603,18 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { } else { resolve('PAGE_FETCHED') } - } - } + }); + + }).on('error', (err) => { + console.error(err); + TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).UNEXPECTED_ERROR = err + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> httpClient.get -> err = " + err.stack) + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[ERROR] start -> httpClient.get -> Connection to the API Server Failed. Maybe there is no Internet connection. Retrying later. ") + resolve('NO_CONNECTION') + }); + } ) diff --git a/Projects/Superalgos/UI/Spaces/Floating-Space/UiObject.js b/Projects/Superalgos/UI/Spaces/Floating-Space/UiObject.js index 80380a401..c17d83b09 100644 --- a/Projects/Superalgos/UI/Spaces/Floating-Space/UiObject.js +++ b/Projects/Superalgos/UI/Spaces/Floating-Space/UiObject.js @@ -1833,12 +1833,12 @@ function newUiObject() { if (thisObject.isOnFocus === true) { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + radius * 2 / 3 + lineSeparator * 4 + 30 + y: position.y + radius * 2 / 3 + lineSeparator * 5 + 30 } } else { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 4 + y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 5 } } } @@ -1887,12 +1887,12 @@ function newUiObject() { if (thisObject.isOnFocus === true) { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + radius * 2 / 3 + lineSeparator * 5 + 30 + y: position.y + radius * 2 / 3 + lineSeparator * 6 + 30 } } else { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 5 + y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 6 } } @@ -1939,12 +1939,12 @@ function newUiObject() { if (thisObject.isOnFocus === true) { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + radius * 2 / 3 + lineSeparator * 3 + 30 + y: position.y + radius * 2 / 3 + lineSeparator * 4 + 30 } } else { labelPoint = { x: position.x - getTextWidth(label) / 2, - y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 3 + y: position.y + thisObject.payload.floatingObject.currentImageSize / 2 + lineSeparator * 4 } }