1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Simplify flow api implementation and add logging messages

This commit is contained in:
Nick O'Leary 2015-12-10 17:02:09 +00:00
parent ca62e720b5
commit da0ce9fe0d
2 changed files with 57 additions and 127 deletions

View File

@ -89,6 +89,9 @@
"stopped-modified-flows": "Stopped modified flows", "stopped-modified-flows": "Stopped modified flows",
"stopped-flows": "Stopped flows", "stopped-flows": "Stopped flows",
"stopped": "Stopped", "stopped": "Stopped",
"added-flow": "Adding flow: __label__",
"updated-flow": "Updated flow: __label__",
"removed-flow": "Removed flow: __label__",
"missing-types": "Waiting for missing types to be registered:", "missing-types": "Waiting for missing types to be registered:",
"missing-type-provided": " - __type__ (provided by npm module __module__)", "missing-type-provided": " - __type__ (provided by npm module __module__)",
"missing-type-install-1": "To install any of these missing modules, run:", "missing-type-install-1": "To install any of these missing modules, run:",

View File

@ -76,7 +76,7 @@ function load() {
}); });
} }
function setConfig(_config,type) { function setConfig(_config,type,muteLog) {
var config = clone(_config); var config = clone(_config);
type = type||"full"; type = type||"full";
@ -115,8 +115,8 @@ function setConfig(_config,type) {
activeFlowConfig = newFlowConfig; activeFlowConfig = newFlowConfig;
return credentials.clean(activeConfig).then(function() { return credentials.clean(activeConfig).then(function() {
if (started) { if (started) {
return stop(type,diff).then(function() { return stop(type,diff,muteLog).then(function() {
start(type,diff); start(type,diff,muteLog);
}).otherwise(function(err) { }).otherwise(function(err) {
}) })
} }
@ -199,7 +199,7 @@ function handleStatus(node,statusMessage) {
} }
function start(type,diff) { function start(type,diff,muteLog) {
//dumpActiveNodes(); //dumpActiveNodes();
type = type||"full"; type = type||"full";
started = true; started = true;
@ -225,10 +225,12 @@ function start(type,diff) {
} }
return when.resolve(); return when.resolve();
} }
if (diff) { if (!muteLog) {
log.info(log._("nodes.flows.starting-modified-"+type)); if (diff) {
} else { log.info(log._("nodes.flows.starting-modified-"+type));
log.info(log._("nodes.flows.starting-flows")); } else {
log.info(log._("nodes.flows.starting-flows"));
}
} }
var id; var id;
if (!diff) { if (!diff) {
@ -270,20 +272,24 @@ function start(type,diff) {
} }
} }
events.emit("nodes-started"); events.emit("nodes-started");
if (diff) { if (!muteLog) {
log.info(log._("nodes.flows.started-modified-"+type)); if (diff) {
} else { log.info(log._("nodes.flows.started-modified-"+type));
log.info(log._("nodes.flows.started-flows")); } else {
log.info(log._("nodes.flows.started-flows"));
}
} }
return when.resolve(); return when.resolve();
} }
function stop(type,diff) { function stop(type,diff,muteLog) {
type = type||"full"; type = type||"full";
if (diff) { if (!muteLog) {
log.info(log._("nodes.flows.stopping-modified-"+type)); if (diff) {
} else { log.info(log._("nodes.flows.stopping-modified-"+type));
log.info(log._("nodes.flows.stopping-flows")); } else {
log.info(log._("nodes.flows.stopping-flows"));
}
} }
started = false; started = false;
var promises = []; var promises = [];
@ -321,10 +327,12 @@ function stop(type,diff) {
// can do... so cheat by wiping the map knowing it'll be rebuilt // can do... so cheat by wiping the map knowing it'll be rebuilt
// in start() // in start()
subflowInstanceNodeMap = {}; subflowInstanceNodeMap = {};
if (diff) { if (!muteLog) {
log.info(log._("nodes.flows.stopped-modified-"+type)); if (diff) {
} else { log.info(log._("nodes.flows.stopped-modified-"+type));
log.info(log._("nodes.flows.stopped-flows")); } else {
log.info(log._("nodes.flows.stopped-flows"));
}
} }
resolve(); resolve();
}); });
@ -376,34 +384,18 @@ function updateMissingTypes() {
} }
} }
// function dumpActiveNodes() {
// console.log("--------")
// for (var i in activeFlowConfig.allNodes) {
// console.log(i,activeFlowConfig.allNodes[i].type,activeFlowConfig.allNodes[i].z)
// }
// console.log("--------")
// }
function addFlow(flow) { function addFlow(flow) {
//dumpActiveNodes();
/*
{
id:'',
label:'',
nodes:[]
}
*/ var i,node;
// flow.id should not exist - it will be assigned by the runtime
// all flow.{subflows|configs|nodes}.z will be set to flow.id
// check all known types - fail if otherwise?
//
// resolves with generated flow id
var i,id,node;
flow.id = redUtil.generateId(); flow.id = redUtil.generateId();
var nodes = [{
type:'tab',
label:flow.label,
id:flow.id
}];
for (i=0;i<flow.nodes.length;i++) { for (i=0;i<flow.nodes.length;i++) {
node = flow.nodes[i]; node = flow.nodes[i];
if (activeFlowConfig.allNodes[node.id]) { if (activeFlowConfig.allNodes[node.id]) {
@ -411,6 +403,7 @@ function addFlow(flow) {
return when.reject(new Error('duplicate id')); return when.reject(new Error('duplicate id'));
} }
node.z = flow.id; node.z = flow.id;
nodes.push(node);
} }
if (flow.configs) { if (flow.configs) {
for (i=0;i<flow.configs.length;i++) { for (i=0;i<flow.configs.length;i++) {
@ -420,61 +413,15 @@ function addFlow(flow) {
return when.reject(new Error('duplicate id')); return when.reject(new Error('duplicate id'));
} }
node.z = flow.id; node.z = flow.id;
nodes.push(node);
} }
} }
var tabNode = { var newConfig = clone(activeConfig);
type:'tab', newConfig = newConfig.concat(nodes);
label:flow.label,
id:flow.id
}
var nodes = [tabNode].concat(flow.nodes||[]).concat(flow.configs||[]);
var credentialSavePromise;
var credentialsChanged = false;
nodes.forEach(function(node) {
if (node.credentials) {
credentials.extract(node);
credentialsChanged = true;
}
});
if (credentialsChanged) {
credentialSavePromise = credentials.save();
} else {
credentialSavePromise = when.resolve();
}
var parsedConfig = flowUtil.parseConfig(clone(nodes)); return setConfig(newConfig,'flows',true).then(function() {
parsedConfig.missingTypes.forEach(function(type) { log.info(log._("nodes.flows.added-flow",{label:(flow.label?flow.label+" ":"")+"["+flow.id+"]"}));
if (activeFlowConfig.missingTypes.indexOf(type) == -1) { return flow.id;
activeFlowConfig.missingTypes.push(type);
}
})
activeFlowConfig.allNodes[tabNode.id] = tabNode;
for (id in parsedConfig.flows[flow.id].nodes) {
if (parsedConfig.flows[flow.id].nodes.hasOwnProperty(id)) {
activeFlowConfig.allNodes[id] = parsedConfig.flows[flow.id].nodes[id];
}
}
if (parsedConfig.flows[flow.id].configs) {
for (id in parsedConfig.flows[flow.id].configs) {
if (parsedConfig.flows[flow.id].configs.hasOwnProperty(id)) {
activeFlowConfig.allNodes[id] = parsedConfig.flows[flow.id].configs[id];
}
}
}
activeFlowConfig.flows[flow.id] = parsedConfig.flows[flow.id];
activeConfig = activeConfig.concat(nodes);
// TODO: extract creds
return credentialSavePromise.then(function() {
return storage.saveFlows(activeConfig).then(function() {
return start("flows",{added:flow.nodes.map(function(n) { return n.id})}).then(function() {
//dumpActiveNodes();
// console.log(activeFlowConfig);
return flow.id;
})
})
}); });
} }
@ -528,6 +475,7 @@ function getFlow(id) {
} }
return result; return result;
} }
function updateFlow(id,newFlow) { function updateFlow(id,newFlow) {
if (id === 'global') { if (id === 'global') {
// TODO: handle global update // TODO: handle global update
@ -556,11 +504,11 @@ function updateFlow(id,newFlow) {
}); });
newConfig = newConfig.concat(nodes); newConfig = newConfig.concat(nodes);
return setConfig(newConfig,'flows'); return setConfig(newConfig,'flows',true).then(function() {
log.info(log._("nodes.flows.updated-flow",{label:(flow.label?flow.label+" ":"")+"["+flow.id+"]"}));
// filter activeConfig to remove nodes })
} }
function removeFlow(id) { function removeFlow(id) {
if (id === 'global') { if (id === 'global') {
// TODO: nls + error code // TODO: nls + error code
@ -573,37 +521,16 @@ function removeFlow(id) {
throw e; throw e;
} }
var diff = { var newConfig = clone(activeConfig);
removed: [id].concat(Object.keys(flow.nodes)).concat(Object.keys(flow.configs)), newConfig = newConfig.filter(function(node) {
linked:[],
changed:[]
}
delete activeFlowConfig.flows[id];
diff.removed.forEach(function(id) {
delete activeFlowConfig.allNodes[id];
});
activeConfig = activeConfig.filter(function(node) {
return node.z !== id && node.id !== id; return node.z !== id && node.id !== id;
}); });
var missingTypeCount = activeFlowConfig.missingTypes.length; return setConfig(newConfig,'flows',true).then(function() {
updateMissingTypes(); log.info(log._("nodes.flows.removed-flow",{label:(flow.label?flow.label+" ":"")+"["+flow.id+"]"}));
});
return credentials.clean(activeConfig).then(function() {
storage.saveFlows(activeConfig).then(function() {
stop("flows",diff).then(function() {
if (missingTypeCount > 0 && activeFlowConfig.missingTypes.length === 0) {
return start();
}
//dumpActiveNodes();
});
});
})
} }
module.exports = { module.exports = {
init: init, init: init,