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:
parent
631dea47b7
commit
39da31eaaa
@ -29,7 +29,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-pass"><i class="icon-briefcase"></i> Password</label>
|
<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>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label>
|
<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><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>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>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>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||||
|
var reconnect = RED.settings.mysqlReconnectTime || 30000;
|
||||||
var mysqldb = require('mysql');
|
var mysqldb = require('mysql');
|
||||||
|
|
||||||
function MySQLNode(n) {
|
function MySQLNode(n) {
|
||||||
@ -26,19 +27,36 @@ function MySQLNode(n) {
|
|||||||
this.dbname = n.db;
|
this.dbname = n.db;
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
node.connection = mysqldb.createConnection({
|
function doConnect() {
|
||||||
host : node.host,
|
node.connection = mysqldb.createConnection({
|
||||||
port : node.port,
|
host : node.host,
|
||||||
user : node.user,
|
port : node.port,
|
||||||
password : node.password,
|
user : node.user,
|
||||||
database : node.dbname
|
password : node.password,
|
||||||
});
|
database : node.dbname,
|
||||||
|
insecureAuth: true
|
||||||
|
});
|
||||||
|
|
||||||
node.connection.connect(function(err) {
|
node.connection.connect(function(err) {
|
||||||
if (err) node.error(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 () {
|
node.on('close', function () {
|
||||||
|
if (node.tick) { clearTimeout(node.tick); }
|
||||||
node.connection.end(function(err) {
|
node.connection.end(function(err) {
|
||||||
if (err) node.error(err);
|
if (err) node.error(err);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user