From 4a079accbf070151abd382bb384b736aa17b243a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 10 Sep 2019 09:43:42 +0200 Subject: [PATCH] Status Reports Events Syncronized even whe dependent is run first that dependency --- StatusReport.js | 47 ++++++++--------------------------------------- server.js | 7 ++++++- 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/StatusReport.js b/StatusReport.js index 1c6d4e497..46c4b8e05 100644 --- a/StatusReport.js +++ b/StatusReport.js @@ -61,15 +61,13 @@ let key = owner.devTeam + "-" + owner.bot + "-" + owner.process let callerId = bot.devTeam + "-" + bot.codeName + "-" + bot.process - let eventSubscriptionIdStatusReport - let eventSubscriptionIdProcessTerminated + let subscriptionIdStatusReport - eventSubscriptionIdStatusReport = global.SYSTEM_EVENT_HANDLER.listenToEvent(key, 'Status Report Updated', undefined, callerId, responseCallBackStatusReport, eventsCallBackStatusReport) + subscriptionIdStatusReport = global.SYSTEM_EVENT_HANDLER.listenToEvent(key, 'Status Report Updated', undefined, callerId, responseCallBack, eventsCallBack) - function responseCallBackStatusReport(message) { + function responseCallBack(message) { if (message.result !== global.DEFAULT_OK_RESPONSE.result) { logger.write(MODULE_NAME, "[ERROR] initialize -> Could not register event listener for event 'Status Report Updated' -> Message = " + message); - stopListeningAll() let event = { reason: 'This process depends on ' + key + ' which is currently not running.' } @@ -78,44 +76,15 @@ } } - function eventsCallBackStatusReport() { + function eventsCallBack() { /* We continue the normal flow after we learn the dependent process has updated its status report. */ - stopListeningAll() + stopListening() callBackFunction(global.DEFAULT_OK_RESPONSE); } - eventSubscriptionIdProcessTerminated = global.SYSTEM_EVENT_HANDLER.listenToEvent(key, 'Process Terminated', undefined, callerId, responseCallBackProcessTerminated, eventsCallBackProcessTerminated) - - function responseCallBackProcessTerminated(message) { - if (message.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write(MODULE_NAME, "[ERROR] initialize -> Could not register event listener for event 'Process Terminated' -> Message = " + message); - stopListeningAll() - let event = { - reason: 'This process depends on ' + key + ' which is currently not running.' - } - global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Process Terminated', event) - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - function eventsCallBackProcessTerminated() { - /* If the process we depend on to start was terminated, there is no point to continue waiting. We need to cloese everything al terminate this process too. */ - logger.write(MODULE_NAME, "[ERROR] initialize -> Received event 'Process Terminated' from a dependency we are waiting for. -> Message = " + message); - - stopListeningAll() - let event = { - reason: 'This process depends on ' + key + ' which has been terminated.' - } - global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Process Terminated', event) - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - function stopListeningAll() { - if (eventSubscriptionIdStatusReport) { - stopListening(key, 'Status Report Updated', eventSubscriptionIdStatusReport) - } - if (eventSubscriptionIdProcessTerminated) { - stopListening(key, 'Process Terminated', eventSubscriptionIdStatusReport) + function stopListening() { + if (subscriptionIdStatusReport) { + stopListening(key, 'Status Report Updated', subscriptionIdStatusReport) } } diff --git a/server.js b/server.js index 95a2b25f8..3e7984bc6 100644 --- a/server.js +++ b/server.js @@ -115,6 +115,8 @@ function bootLoader() { heartBeatInterval = setInterval(hearBeat, 1000) function hearBeat() { + + /* The heartbeat event is raised at the event handler of the instance of this task, created at the UI. */ let key = global.USER_DEFINITION.name + '-' + global.USER_DEFINITION.type + '-' + global.USER_DEFINITION.id let event = { seconds: (new Date()).getSeconds() @@ -172,7 +174,10 @@ function startSequence () { /* Delete the event handler for each process. */ let key = code.devTeam + "-" + code.codeName + "-" + code.process - global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Process Terminated') + let event = { + reason: 'Signal Received to Terminate this Process.' + } + global.SYSTEM_EVENT_HANDLER.raiseEvent(key, 'Process Terminated', event) global.SYSTEM_EVENT_HANDLER.deleteEventHandler(key) }