Catch SIGINT (ctrl-C/break) and try to close nodes prior to exit.

This commit is contained in:
Dave C-J 2013-10-12 22:00:34 +01:00
parent 8c6aa07d91
commit 1fb185d081
2 changed files with 18 additions and 25 deletions

8
red.js
View File

@ -56,7 +56,7 @@ app.use(settings.httpRoot,red);
RED.start(); RED.start();
server.listen(settings.uiPort,function() { server.listen(settings.uiPort,function() {
util.log('[red] Server now running at http'+(settings.https?'s':'')+'://127.0.0.1:'+settings.uiPort+settings.httpRoot); util.log('[red] Server now running at http'+(settings.https?'s':'')+'://127.0.0.1:'+settings.uiPort+settings.httpRoot);
}); });
process.on('uncaughtException',function(err) { process.on('uncaughtException',function(err) {
@ -70,4 +70,8 @@ process.on('uncaughtException',function(err) {
process.exit(1); process.exit(1);
}); });
process.on('SIGINT', function () {
RED.nodes.closedown();
console.log("Exiting Node-RED. Thank you.");
process.exit();
});

View File

@ -42,7 +42,6 @@ function getCallerFilename(type) {
return stack[0].getFileName(); return stack[0].getFileName();
} }
var registry = (function() { var registry = (function() {
var nodes = {}; var nodes = {};
var logHandlers = []; var logHandlers = [];
@ -91,7 +90,6 @@ var node_type_registry = (function() {
var obj = { var obj = {
register: function(type,node) { register: function(type,node) {
util.inherits(node, Node); util.inherits(node, Node);
var callerFilename = getCallerFilename(type); var callerFilename = getCallerFilename(type);
if (callerFilename == null) { if (callerFilename == null) {
util.log("["+type+"] unable to determine filename"); util.log("["+type+"] unable to determine filename");
@ -117,7 +115,6 @@ var node_type_registry = (function() {
result += node_configs[nt]; result += node_configs[nt];
} }
return result; return result;
} }
} }
return obj; return obj;
@ -176,7 +173,6 @@ Node.prototype.send = function(msg) {
} }
module.exports.Node = Node; module.exports.Node = Node;
Node.prototype.receive = function(msg) { Node.prototype.receive = function(msg) {
this.emit("input",msg); this.emit("input",msg);
} }
@ -197,9 +193,6 @@ Node.prototype.error = function(msg) {
this.emit("log",o); this.emit("log",o);
} }
var credentials = {}; var credentials = {};
var credentialsFile = "credentials.json"; var credentialsFile = "credentials.json";
if (fs.existsSync(credentialsFile)) { if (fs.existsSync(credentialsFile)) {
@ -225,8 +218,6 @@ module.exports.deleteCredentials = function(id) {
delete credentials[id]; delete credentials[id];
saveCredentialsFile(); saveCredentialsFile();
} }
module.exports.createNode = function(node,def) { module.exports.createNode = function(node,def) {
Node.call(node,def); Node.call(node,def);
} }
@ -257,12 +248,9 @@ module.exports.load = function() {
}); });
} }
loadNodes(__dirname+"/../nodes"); loadNodes(__dirname+"/../nodes");
//events.emit("nodes-loaded"); //events.emit("nodes-loaded");
} }
var activeConfig = null; var activeConfig = null;
var missingTypes = []; var missingTypes = [];
@ -279,10 +267,15 @@ events.on('type-registered',function(type) {
} }
}); });
module.exports.getNode = function(nid) { module.exports.getNode = function(nid) {
return registry.get(nid); return registry.get(nid);
} }
module.exports.closedown = function() {
util.log("[red] Closing Down Nodes");
registry.clear();
}
module.exports.setConfig = function(conf) { module.exports.setConfig = function(conf) {
if (activeConfig&&activeConfig.length > 0) { if (activeConfig&&activeConfig.length > 0) {
util.log("[red] Stopping flows"); util.log("[red] Stopping flows");
@ -293,7 +286,6 @@ module.exports.setConfig = function(conf) {
} }
var parseConfig = function() { var parseConfig = function() {
missingTypes = []; missingTypes = [];
for (var i in activeConfig) { for (var i in activeConfig) {
var type = activeConfig[i].type; var type = activeConfig[i].type;
@ -307,7 +299,6 @@ var parseConfig = function() {
for (var i in missingTypes) { for (var i in missingTypes) {
util.log("[red] - "+missingTypes[i]); util.log("[red] - "+missingTypes[i]);
} }
return; return;
} }
@ -317,19 +308,18 @@ var parseConfig = function() {
var nn = null; var nn = null;
var nt = node_type_registry.get(activeConfig[i].type); var nt = node_type_registry.get(activeConfig[i].type);
if (nt) { if (nt) {
try { try {
nn = new nt(activeConfig[i]); nn = new nt(activeConfig[i]);
} }
catch (err) { catch (err) {
util.log("[red] "+activeConfig[i].type+" : "+err); util.log("[red] "+activeConfig[i].type+" : "+err);
} }
} }
// console.log(nn); // console.log(nn);
if (nn == null) { if (nn == null) {
util.log("[red] unknown type: "+activeConfig[i].type); util.log("[red] unknown type: "+activeConfig[i].type);
} }
} }
// Clean up any orphaned credentials // Clean up any orphaned credentials
var deletedCredentials = false; var deletedCredentials = false;
for (var c in credentials) { for (var c in credentials) {
@ -343,5 +333,4 @@ var parseConfig = function() {
saveCredentialsFile(); saveCredentialsFile();
} }
events.emit("nodes-started"); events.emit("nodes-started");
} }