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

Added proper database reconnects... and more error handling, that should help address Issue #87 in main node-red Issue list.

This commit is contained in:
Dave C-J 2013-11-22 10:24:08 +00:00
parent 631dea47b7
commit 39da31eaaa
2 changed files with 31 additions and 11 deletions

View File

@ -29,7 +29,7 @@
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="icon-briefcase"></i> Password</label>
<input type="text" id="node-config-input-pass" placeholder="mySecret">
<input type="password" id="node-config-input-pass" placeholder="mySecret">
</div>
<div class="form-row">
<label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label>
@ -72,6 +72,8 @@
<p><b>msg.topic</b> must hold the <i>query</i> for the database, and the result is returned in <b>msg.payload</b>.</p>
<p>Typically the returned payload will be an array of the result rows.</p>
<p>If nothing is found for the key then <i>null</i> is returned,</p>
<p>The reconnect timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
<pre>mysqlReconnectTime: 30000,</pre></p>
</script>
<script type="text/javascript">

View File

@ -15,6 +15,7 @@
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var reconnect = RED.settings.mysqlReconnectTime || 30000;
var mysqldb = require('mysql');
function MySQLNode(n) {
@ -26,19 +27,36 @@ function MySQLNode(n) {
this.dbname = n.db;
var node = this;
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.user,
password : node.password,
database : node.dbname
});
function doConnect() {
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.user,
password : node.password,
database : node.dbname,
insecureAuth: true
});
node.connection.connect(function(err) {
if (err) node.error(err);
});
node.connection.connect(function(err) {
if (err) {
node.warn("mysql: "+err);
node.tick = setTimeout(doConnect, reconnect);
}
});
node.connection.on('error', function(err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
} else {
node.error(err);
doConnect();
}
});
}
doConnect();
node.on('close', function () {
if (node.tick) { clearTimeout(node.tick); }
node.connection.end(function(err) {
if (err) node.error(err);
});