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

155 lines
4.4 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.
**/
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)) {
2015-02-03 23:02:26 +01:00
log.info("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) {
2015-02-03 23:02:26 +01:00
log.warn("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
/**
* @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;
// Clone config and extract credentials prior to saving
// Original config needs to retain credentials so that flow.applyConfig
// knows which nodes have had changes.
var cleanConfig = clone(config);
cleanConfig.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(cleanConfig);})
.then(function() { return flowNodes.stopFlows(); })
.then(function() { activeFlow = new Flow(config); flowNodes.startFlows();});
} else {
return credentialSavePromise
.then(function() { return storage.saveFlows(cleanConfig);})
.then(function() { return activeFlow.applyConfig(config,type); });
}
},
startFlows: function() {
2015-02-03 23:02:26 +01:00
log.info("Starting flows");
try {
activeFlow.start();
log.info("Started flows");
} catch(err) {
var missingTypes = activeFlow.getMissingTypes();
if (missingTypes.length > 0) {
2015-02-03 23:02:26 +01:00
log.info("Waiting for missing types to be registered:");
2015-01-10 23:09:37 +01:00
for (var i=0;i<missingTypes.length;i++) {
2015-02-03 23:02:26 +01:00
log.info(" - "+missingTypes[i]);
2014-11-23 23:25:09 +01:00
}
}
}
2014-05-03 23:26:35 +02:00
},
stopFlows: function() {
2015-02-03 23:02:26 +01:00
log.info("Stopping flows");
return activeFlow.stop().then(function() {
log.info("Stopped flows");
return;
});
}
2014-07-14 22:46:36 +02:00
};
var activeFlow = null;