From 2f386cbdef66c25a3c5409253acdfa11a08903bb Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 23 Aug 2020 09:40:18 +0200 Subject: [PATCH] Network Error Improvements --- CanvasApp/DesignSpace/Workspace/Workspace.js | 28 +++++++++++++++---- CanvasApp/EventsServerClient.js | 26 +++++++++++++++-- .../LowFrequencyTrading/TradingProcess.js | 6 +++- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/CanvasApp/DesignSpace/Workspace/Workspace.js b/CanvasApp/DesignSpace/Workspace/Workspace.js index 7008cb873..5b56eb6b7 100644 --- a/CanvasApp/DesignSpace/Workspace/Workspace.js +++ b/CanvasApp/DesignSpace/Workspace/Workspace.js @@ -67,6 +67,7 @@ function newWorkspace() { thisObject.nodeChildren = newNodeChildren() + let savingWorkspaceIntervalId let isInitialized = false let workingAtTask = 0 let circularProgressBar = newBusyProgressBar() @@ -118,7 +119,7 @@ function newWorkspace() { thisObject.enabled = true canvas.cockpitSpace.initializePosition() canvas.splashScreen.initialize() - setInterval(saveWorkspace, 60000) + savingWorkspaceIntervalId = setInterval(saveWorkspace, 60000) isInitialized = true } } catch (err) { @@ -162,7 +163,7 @@ function newWorkspace() { if (host === undefined) { host = 'localhost' } if (webSocketsPort === undefined) { webSocketsPort = '8080' } - let eventsServerClient = newEventsServerClient(host, webSocketsPort, networkNode.name) + let eventsServerClient = newEventsServerClient(host, webSocketsPort, networkNode.name, networkNode) eventsServerClient.initialize() thisObject.eventsServerClients.set(networkNode.id, eventsServerClient) @@ -233,6 +234,14 @@ function newWorkspace() { } } + function finalizeEventsServerClients() { + thisObject.eventsServerClients.forEach(finalize) + + function finalize(eventServerClient) { + eventServerClient.finalize() + } + } + function replacingWorkspacePhysics() { if (thisObject.enabled !== true) { return } @@ -249,21 +258,30 @@ function newWorkspace() { workingAtTask++ break case 3: + finalizeEventsServerClients() + thisObject.eventsServerClients = new Map() + workingAtTask++ + break + case 4: + clearInterval(savingWorkspaceIntervalId) + workingAtTask++ + break + case 5: thisObject.workspaceNode = loadedWorkspaceNode loadedWorkspaceNode = undefined workingAtTask++ break - case 4: + case 6: functionLibraryUiObjectsFromNodes.recreateWorkspace(thisObject.workspaceNode) setupEventsServerClients() runTasksAndSessions(true) workingAtTask++ break - case 5: + case 7: canvas.chartingSpace.reset() workingAtTask++ break - case 6: + case 8: workingAtTask = 0 circularProgressBar.visible = false break diff --git a/CanvasApp/EventsServerClient.js b/CanvasApp/EventsServerClient.js index 2956a6916..5dad4e0c2 100644 --- a/CanvasApp/EventsServerClient.js +++ b/CanvasApp/EventsServerClient.js @@ -1,5 +1,5 @@ -function newEventsServerClient(host, port, hostName) { +function newEventsServerClient(host, port, hostName, networkNode) { /* Web Sockets Connection */ const MODULE_NAME = 'System Event Handler' @@ -17,6 +17,7 @@ function newEventsServerClient(host, port, hostName) { let thisObject = { isConnected: isConnected, initialize: initialize, + finalize: finalize, physics: physics, createEventHandler: createEventHandler, deleteEventHandler: deleteEventHandler, @@ -37,6 +38,21 @@ function newEventsServerClient(host, port, hostName) { setuptWebSockets(callBackFunction) } + function finalize() { + WEB_SOCKETS_CONNECTION.close() + WEB_SOCKETS_CONNECTION.onerror = undefined + WEB_SOCKETS_CONNECTION.onmessage = undefined + WEB_SOCKETS_CONNECTION.onopen = undefined + WEB_SOCKETS_CONNECTION = undefined + + eventListeners = undefined + responseWaiters = undefined + commandsWaitingConfirmation = undefined + commandsSentByTimestamp = undefined + nonce = undefined + thisObject = undefined + } + function isConnected() { if (WEB_SOCKETS_CONNECTION.readyState === 1) { return true @@ -156,7 +172,13 @@ function newEventsServerClient(host, port, hostName) { } if (thisObject.isConnected() !== true) { - canvas.cockpitSpace.setStatus('Connecting to Superalgos Backend at ' + hostName + ' Network Node. Please wait until the connection is established.', 100, canvas.cockpitSpace.statusTypes.WARNING) + if (networkNode !== undefined) { + if (networkNode.payload !== undefined) { + if (networkNode.payload.uiObject !== undefined) { + networkNode.payload.uiObject.setErrorMessage('Connecting to Superalgos Backend at ' + hostName + ' Network Node') + } + } + } } else { retryCommandsPhysics() } diff --git a/TaskServer/LowFrequencyTrading/TradingProcess.js b/TaskServer/LowFrequencyTrading/TradingProcess.js index a8f4ff95a..b8c971057 100644 --- a/TaskServer/LowFrequencyTrading/TradingProcess.js +++ b/TaskServer/LowFrequencyTrading/TradingProcess.js @@ -349,6 +349,10 @@ function trimDataFile(dataFile, recordDefinition) { /* Here we will discard all the records in a file that are outside of the current time range. + We will include the las element previous to the begining of the time range. This is needed + because during the simulation, the current period is not the open one, but the previous to + the open, and if we do not include the previous to the initial datetime there will be no + current objects at the begining of the simulation for many time frames. */ let beginIndex let endIndex @@ -366,7 +370,7 @@ let dataRecord = dataFile[i] let begin = dataRecord[beginIndex] let end = dataRecord[endIndex] - if (end < bot.SESSION.parameters.timeRange.config.initialDatetime - 1) { continue } // /1 because we need the previous closed element + if (end + timeFrame < bot.SESSION.parameters.timeRange.config.initialDatetime - 1) { continue } // /1 because we need the previous closed element if (begin > bot.SESSION.parameters.timeRange.config.finalDatetime) { continue } result.push(dataRecord) }