From 9fffb96c2aef8e32d33a3158e85afad4f1f54ea0 Mon Sep 17 00:00:00 2001 From: siouxhacker <50530417+siouxhacker@users.noreply.github.com> Date: Wed, 6 May 2020 08:16:06 -0500 Subject: [PATCH] Update gateway.js Added logic to determine if a node has been modified by an event and if so, only update the node. --- gateway.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/gateway.js b/gateway.js index 53ca4fe..972887b 100644 --- a/gateway.js +++ b/gateway.js @@ -1186,20 +1186,31 @@ global.schedule = function(node, eventKey) { node.events[eventKey].executeDateTime = new Date(Date.now() + nextRunTimeout); //actual datetime when this is scheduled to execute //save to DB - db.findOne({_id:node._id}, function (err, dbNode) { - dbNode.events = node.events; - db.update({_id:dbNode._id}, { $set : dbNode }, {}, function (err, numReplaced) { console.info(`[${dbNode._id}] DB-Updates:${numReplaced}`);}); - io.sockets.emit('UPDATENODE', dbNode); //push updated node to client sockets - }); + if (node.modifiedByEvent) + { + node.modifiedByEvent = false; + db.update({ _id:node._id }, { $set : node}, {}, function (err, numReplaced) { console.info(`[${node._id}] DB-Updates:${numReplaced}`);}); + } + else + { + db.findOne({_id:node._id}, function (err, dbNode) { + dbNode.events = node.events; + db.update({_id:dbNode._id}, { $set : dbNode }, {}, function (err, numReplaced) { console.info(`[${dbNode._id}] DB-Updates:${numReplaced}`);}); + io.sockets.emit('UPDATENODE', dbNode); //push updated node to client sockets + }); + } } //run a scheduled event and reschedule it global.runAndReschedule = function(functionToExecute, node, eventKey) { console.info(`**** RUNNING SCHEDULED EVENT - nodeId:${node._id} event:${eventKey}...`); db.findOne({_id:node._id}, function (err, dbNode) { + + var eventNode = null; + try { - functionToExecute(dbNode, eventKey); + eventNode = functionToExecute(dbNode, eventKey); } catch (ex) { @@ -1207,6 +1218,14 @@ global.runAndReschedule = function(functionToExecute, node, eventKey) { console.error(msg); io.sockets.emit('LOG', msg); } + + if (null != eventNode) + { + // The node was modified by the event. + // Pass the modified event to schedule. + dbNode = eventNode; + } + schedule(dbNode, eventKey); }); } @@ -1257,4 +1276,4 @@ setInterval(function(){ io.sockets.emit('UPDATENODE', entries[i]); //push updated node to clients } }); -}, 10000); \ No newline at end of file +}, 10000);