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

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

6
red.js
View File

@ -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;
} }
@ -329,7 +320,6 @@ var parseConfig = function() {
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");
} }