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

187 lines
5.5 KiB
JavaScript
Raw Normal View History

2014-05-03 23:26:35 +02:00
/**
2015-01-15 18:10:32 +01:00
* Copyright 2014, 2015 IBM Corp.
2014-05-03 23:26:35 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var util = require("util");
2014-02-25 00:35:11 +01:00
var clone = require("clone");
2014-05-03 23:26:35 +02:00
var when = require("when");
2014-05-03 23:26:35 +02:00
var typeRegistry = require("./registry");
var credentials = require("./credentials");
var Flow = require("./Flow");
2014-05-03 23:26:35 +02:00
var log = require("../log");
var events = require("../events");
2014-11-23 23:25:09 +01:00
var redUtil = require("../util");
2014-05-03 23:26:35 +02:00
var storage = null;
var activeFlow = null;
2014-05-03 23:26:35 +02:00
var nodes = {};
2014-02-25 00:35:11 +01:00
var subflows = {};
2014-05-03 23:26:35 +02:00
var activeConfig = [];
2014-11-23 23:25:09 +01:00
var activeConfigNodes = {};
2014-05-03 23:26:35 +02:00
events.on('type-registered',function(type) {
if (activeFlow) {
if (activeFlow.typeRegistered(type)) {
util.log("[red] Missing type registered: "+type);
2014-02-25 00:35:11 +01:00
}
}
});
2014-11-23 23:25:09 +01:00
2014-05-03 23:26:35 +02:00
var flowNodes = module.exports = {
init: function(_storage) {
storage = _storage;
},
2014-07-21 17:07:28 +02:00
/**
* Load the current activeConfig from storage and start it running
* @return a promise for the loading of the config
*/
2014-05-03 23:26:35 +02:00
load: function() {
return storage.getFlows().then(function(flows) {
return credentials.load().then(function() {
activeFlow = new Flow(flows);
flowNodes.startFlows();
2014-05-03 23:26:35 +02:00
});
}).otherwise(function(err) {
util.log("[red] Error loading flows : "+err);
console.log(err.stack);
2014-05-03 23:26:35 +02:00
});
},
2014-07-21 17:07:28 +02:00
/**
* Get a node
* @param i the node id
* @return the node
*/
2014-05-03 23:26:35 +02:00
get: function(i) {
return activeFlow.getNode(i);
2014-05-03 23:26:35 +02:00
},
2014-07-21 17:07:28 +02:00
2015-01-10 23:09:37 +01:00
eachNode: function(cb) {
activeFlow.eachNode(cb);
},
2014-07-21 17:07:28 +02:00
/**
* Stops all active nodes and clears the active set
* @return a promise for the stopping of all active nodes
*/
//clear: function(nodesToStop) {
// var stopList;
// if (nodesToStop == null) {
// stopList = Object.keys(nodes);
// } else {
// stopList = Object.keys(nodesToStop);
// }
// console.log(stopList);
// return when.promise(function(resolve) {
// events.emit("nodes-stopping");
// var promises = [];
// console.log("running nodes:",Object.keys(nodes).length);
// for (var i=0;i<stopList.length;i++) {
// var node = nodes[stopList[i]];
// if (node) {
// try {
// var p = node.close();
// if (p) {
// promises.push(p);
// }
// } catch(err) {
// node.error(err);
// }
// delete nodes[stopList[i]];
// delete activeConfigNodes[stopList[i]];
// }
// }
// console.log("running nodes:",Object.keys(nodes).length);
// when.settle(promises).then(function() {
// events.emit("nodes-stopped");
// resolve();
// });
// });
//},
2014-07-21 17:07:28 +02:00
/**
* @return the active configuration
*/
2014-05-03 23:26:35 +02:00
getFlows: function() {
return activeFlow.getFlow();
2014-05-03 23:26:35 +02:00
},
2014-07-21 17:07:28 +02:00
/**
* Sets the current active config.
* @param config the configuration to enable
* @param type the type of deployment to do: full (default), nodes, flows
2014-07-21 17:07:28 +02:00
* @return a promise for the starting of the new flow
*/
setFlows: function (config,type) {
type = type||"full";
2014-11-23 23:25:09 +01:00
var credentialsChanged = false;
2014-11-23 23:25:09 +01:00
var credentialSavePromise = null;
config.forEach(function(node) {
2014-07-21 16:56:38 +02:00
if (node.credentials) {
credentials.extract(node);
credentialsChanged = true;
2014-11-23 23:25:09 +01:00
}
});
if (credentialsChanged) {
2015-01-10 00:21:50 +01:00
credentialSavePromise = credentials.save();
} else {
credentialSavePromise = when.resolve();
2014-11-23 23:25:09 +01:00
}
if (type=="full") {
return credentialSavePromise
.then(function() { return storage.saveFlows(config);})
.then(function() { return flowNodes.stopFlows(); })
.then(function() { activeFlow = new Flow(config); flowNodes.startFlows();});
} else {
return credentialSavePromise
.then(function() { return storage.saveFlows(config);})
.then(function() { return activeFlow.applyConfig(config,type); });
}
},
startFlows: function() {
util.log("[red] Starting flows");
try {
activeFlow.start();
} catch(err) {
var missingTypes = activeFlow.getMissingTypes();
if (missingTypes.length > 0) {
util.log("[red] Waiting for missing types to be registered:");
2015-01-10 23:09:37 +01:00
for (var i=0;i<missingTypes.length;i++) {
util.log("[red] - "+missingTypes[i]);
2014-11-23 23:25:09 +01:00
}
}
}
2014-05-03 23:26:35 +02:00
},
stopFlows: function() {
util.log("[red] Stopping flows");
return activeFlow.stop();
}
2014-07-14 22:46:36 +02:00
};
var activeFlow = null;