mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add some comments to Flow and Subflow classes
This commit is contained in:
@@ -24,7 +24,18 @@ var events = require("../../events");
|
||||
|
||||
var nodeCloseTimeout = 15000;
|
||||
|
||||
/**
|
||||
* This class represents a flow within the runtime. It is responsible for
|
||||
* creating, starting and stopping all nodes within the flow.
|
||||
*/
|
||||
class Flow {
|
||||
|
||||
/**
|
||||
* Create a Flow object.
|
||||
* @param {[type]} parent The parent flow
|
||||
* @param {[type]} globalFlow The global flow definition
|
||||
* @param {[type]} flow This flow's definition
|
||||
*/
|
||||
constructor(parent,globalFlow,flow) {
|
||||
this.TYPE = 'flow';
|
||||
this.parent = parent;
|
||||
@@ -43,6 +54,11 @@ class Flow {
|
||||
this.statusNodes = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a debug-level message from this flow
|
||||
* @param {[type]} msg [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
debug(msg) {
|
||||
Log.log({
|
||||
id: this.id||"global",
|
||||
@@ -52,6 +68,11 @@ class Flow {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a info-level message from this flow
|
||||
* @param {[type]} msg [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
log(msg) {
|
||||
Log.log({
|
||||
id: this.id||"global",
|
||||
@@ -61,6 +82,11 @@ class Flow {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a trace-level message from this flow
|
||||
* @param {[type]} msg [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
trace(msg) {
|
||||
Log.log({
|
||||
id: this.id||"global",
|
||||
@@ -71,6 +97,13 @@ class Flow {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start this flow.
|
||||
* The `diff` argument helps define what needs to be started in the case
|
||||
* of a modified-nodes/flows type deploy.
|
||||
* @param {[type]} msg [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
start(diff) {
|
||||
this.trace("start "+this.TYPE);
|
||||
var node;
|
||||
@@ -185,6 +218,14 @@ class Flow {
|
||||
// this.dump();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop this flow.
|
||||
* The `stopList` argument helps define what needs to be stopped in the case
|
||||
* of a modified-nodes/flows type deploy.
|
||||
* @param {[type]} stopList [description]
|
||||
* @param {[type]} removedList [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
stop(stopList, removedList) {
|
||||
return new Promise((resolve,reject) => {
|
||||
this.trace("stop "+this.TYPE);
|
||||
@@ -216,7 +257,7 @@ class Flow {
|
||||
if (this.subflowInstanceNodes[stopList[i]]) {
|
||||
try {
|
||||
var subflow = this.subflowInstanceNodes[stopList[i]];
|
||||
promises.push(this.stopNode(node,false).then(() => { subflow.stop() }));
|
||||
promises.push(stopNode(node,false).then(() => { subflow.stop() }));
|
||||
} catch(err) {
|
||||
node.error(err);
|
||||
}
|
||||
@@ -224,7 +265,7 @@ class Flow {
|
||||
} else {
|
||||
try {
|
||||
var removed = removedMap[stopList[i]];
|
||||
promises.push(this.stopNode(node,removed));
|
||||
promises.push(stopNode(node,removed));
|
||||
} catch(err) {
|
||||
node.error(err);
|
||||
}
|
||||
@@ -237,31 +278,25 @@ class Flow {
|
||||
});
|
||||
}
|
||||
|
||||
stopNode(node,removed) {
|
||||
return when.promise(function(resolve, reject) {
|
||||
var start;
|
||||
when.promise(function(resolve) {
|
||||
Log.trace("Stopping node "+node.type+":"+node.id+(removed?" removed":""));
|
||||
start = Date.now();
|
||||
resolve(node.close(removed));
|
||||
}).timeout(nodeCloseTimeout).then(function(){
|
||||
var delta = Date.now() - start;
|
||||
Log.trace("Stopped node "+node.type+":"+node.id+" ("+delta+"ms)" );
|
||||
resolve(delta);
|
||||
},function(err) {
|
||||
var delta = Date.now() - start;
|
||||
node.error(Log._("nodes.flows.stopping-error",{message:err}));
|
||||
Log.debug(err.stack);
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the flow definition. This doesn't change anything that is running.
|
||||
* This should be called after `stop` and before `start`.
|
||||
* @param {[type]} _global [description]
|
||||
* @param {[type]} _flow [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
update(_global,_flow) {
|
||||
this.global = _global;
|
||||
this.flow = _flow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a node instance from this flow. If the node is not known to this
|
||||
* flow, pass the request up to the parent.
|
||||
* @param {[type]} id [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
getNode(id) {
|
||||
// console.log('getNode',id,!!this.activeNodes[id])
|
||||
if (!id) {
|
||||
@@ -279,14 +314,32 @@ class Flow {
|
||||
return this.parent.getNode(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the nodes instantiated within this flow
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
getActiveNodes() {
|
||||
return this.activeNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a flow setting value. This currently automatically defers to the parent
|
||||
* flow which, as defined in ./index.js returns `process.env[key]`.
|
||||
* This lays the groundwork for Subflow to have instance-specific settings
|
||||
* @param {[type]} key [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
getSetting(key) {
|
||||
return this.parent.getSetting(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a status event from a node within this flow. If there are no Status
|
||||
* nodes within this flow, pass the request to the parent flow.
|
||||
* @param {[type]} node [description]
|
||||
* @param {[type]} statusMessage [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
handleStatus(node,statusMessage) {
|
||||
events.emit("node-status",{
|
||||
id: node.id,
|
||||
@@ -320,6 +373,14 @@ class Flow {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an error event from a node within this flow. If there are no Catch
|
||||
* nodes within this flow, pass the event to the parent flow.
|
||||
* @param {[type]} node [description]
|
||||
* @param {[type]} logMessage [description]
|
||||
* @param {[type]} msg [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
handleError(node,logMessage,msg) {
|
||||
// console.log("HE",logMessage);
|
||||
var count = 1;
|
||||
@@ -386,6 +447,34 @@ class Flow {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop an individual node within this flow.
|
||||
*
|
||||
* @param {[type]} node [description]
|
||||
* @param {[type]} removed [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
function stopNode(node,removed) {
|
||||
return when.promise(function(resolve, reject) {
|
||||
var start;
|
||||
when.promise(function(resolve) {
|
||||
Log.trace("Stopping node "+node.type+":"+node.id+(removed?" removed":""));
|
||||
start = Date.now();
|
||||
resolve(node.close(removed));
|
||||
}).timeout(nodeCloseTimeout).then(function(){
|
||||
var delta = Date.now() - start;
|
||||
Log.trace("Stopped node "+node.type+":"+node.id+" ("+delta+"ms)" );
|
||||
resolve(delta);
|
||||
},function(err) {
|
||||
var delta = Date.now() - start;
|
||||
node.error(Log._("nodes.flows.stopping-error",{message:err}));
|
||||
Log.debug(err.stack);
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
init: function(runtime) {
|
||||
nodeCloseTimeout = runtime.settings.nodeCloseTimeout || 15000;
|
||||
|
Reference in New Issue
Block a user