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

Let Mongo node auto retry initial connection

and add status indicator
to close #221
This commit is contained in:
Dave Conway-Jones 2016-08-16 11:43:36 +01:00
parent eb17e6533e
commit eb6e35957b
3 changed files with 35 additions and 19 deletions

View File

@ -59,13 +59,17 @@ module.exports = function(RED) {
this.multi = n.multi || false; this.multi = n.multi || false;
this.operation = n.operation; this.operation = n.operation;
this.mongoConfig = RED.nodes.getNode(this.mongodb); this.mongoConfig = RED.nodes.getNode(this.mongodb);
this.status({fill:"grey",shape:"ring",text:RED._("mongodbstatus.connecting")});
if (this.mongoConfig) {
var node = this; var node = this;
MongoClient.connect(this.mongoConfig.url, function(err, db) {
var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err, db) {
if (err) { if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
node.error(err); node.error(err);
node.tout = setTimeout(connectToDB(), 10000);
} else { } else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db; node.clientDb = db;
var coll; var coll;
if (node.collection) { if (node.collection) {
@ -148,31 +152,37 @@ module.exports = function(RED) {
}); });
} }
}); });
} else {
this.error(RED._("mongodb.errors.missingconfig"));
} }
this.on("close", function() { if (node.mongoConfig) { connectToDB(); }
if (this.clientDb) { else { node.error(RED._("mongodb.errors.missingconfig")); }
this.clientDb.close();
} node.on("close", function() {
node.status({});
if (node.tout) { clearTimeout(node.tout); }
if (node.clientDb) { node.clientDb.close(); }
}); });
} }
RED.nodes.registerType("mongodb out",MongoOutNode); RED.nodes.registerType("mongodb out",MongoOutNode);
function MongoInNode(n) { function MongoInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.collection = n.collection; this.collection = n.collection;
this.mongodb = n.mongodb; this.mongodb = n.mongodb;
this.operation = n.operation || "find"; this.operation = n.operation || "find";
this.mongoConfig = RED.nodes.getNode(this.mongodb); this.mongoConfig = RED.nodes.getNode(this.mongodb);
this.status({fill:"grey",shape:"ring",text:RED._("mongodb.status.connecting")});
if (this.mongoConfig) {
var node = this; var node = this;
MongoClient.connect(this.mongoConfig.url, function(err,db) {
var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err,db) {
if (err) { if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
node.error(err); node.error(err);
node.tout = setTimeout(connectToDB(), 10000);
} else { } else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db; node.clientDb = db;
var coll; var coll;
if (node.collection) { if (node.collection) {
@ -236,14 +246,15 @@ module.exports = function(RED) {
}); });
} }
}); });
} else {
this.error(RED._("mongodb.errors.missingconfig"));
} }
this.on("close", function() { if (node.mongoConfig) { connectToDB(); }
if (this.clientDb) { else { node.error(RED._("mongodb.errors.missingconfig")); }
this.clientDb.close();
} node.on("close", function() {
node.status({});
if (node.tout) { clearTimeout(node.tout); }
if (node.clientDb) { node.clientDb.close(); }
}); });
} }
RED.nodes.registerType("mongodb in",MongoInNode); RED.nodes.registerType("mongodb in",MongoInNode);

View File

@ -22,6 +22,11 @@
"count": "count", "count": "count",
"aggregate": "aggregate" "aggregate": "aggregate"
}, },
"status": {
"connecting": "connecting",
"connected": "connected",
"error": "error"
},
"tip": "<b> Tip:</b> If no collection is set, ensure <code>msg.collection</code> will contain the collection name", "tip": "<b> Tip:</b> If no collection is set, ensure <code>msg.collection</code> will contain the collection name",
"errors": { "errors": {
"nocollection": "No collection defined", "nocollection": "No collection defined",

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-mongodb", "name" : "node-red-node-mongodb",
"version" : "0.0.6", "version" : "0.0.7",
"description" : "Node-RED nodes to talk to an Mongo database", "description" : "Node-RED nodes to talk to an Mongo database",
"dependencies" : { "dependencies" : {
"mongodb" : "^2.2.5" "mongodb" : "^2.2.5"