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

View File

@ -22,6 +22,11 @@
"count": "count",
"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",
"errors": {
"nocollection": "No collection defined",

View File

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