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

Delay node updates to added flush control for queuing

This commit is contained in:
Dave Conway-Jones 2021-07-08 12:13:23 +01:00
parent b2f5a259ab
commit 41c8ca8ab4
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
4 changed files with 82 additions and 63 deletions

View File

@ -141,17 +141,6 @@
return this._("delay.label.limitTopic")+" "+rate; return this._("delay.label.limitTopic")+" "+rate;
} }
} }
// var units = '';
// if (this.nbRateUnits > 1) {
// units = this.nbRateUnits + ' ' + this._("delay.label.units." + this.rateUnits + ".plural");
// } else {
// units = this._("delay.label.units." + this.rateUnits + ".singular");
// }
// return this.name || this.rate + " " + this._("delay.label.timed") + ' ' + units;
// } else {
// var units = this.rateUnits ? (this.nbRateUnits > 1 ? this.nbRateUnits : '') + this.rateUnits.charAt(0) : "s";
// return this.name || this._("delay.label.queue")+" "+this.rate+" msg/"+units;
// }
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";

View File

@ -112,31 +112,35 @@ module.exports = function(RED) {
} }
if (node.buffer.length > 0) { if (node.buffer.length > 0) {
const msgInfo = node.buffer.shift(); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg); if (Object.keys(msgInfo.msg).length > 1) {
msgInfo.done(); msgInfo.send(msgInfo.msg);
msgInfo.done();
}
} }
node.reportDepth(); node.reportDepth();
} }
var clearDelayList = function(s) { var clearDelayList = function(s) {
for (var i=0; i<node.idList.length; i++ ) { node.idList[i].clear(); } var len = node.idList.length;
for (var i=0; i<len; i++ ) { node.idList[i].clear(); }
node.idList = []; node.idList = [];
if (s) { node.status({text:"reset"}); } if (s) { node.status({fill:"blue",shape:"ring",text:0}); }
else { node.status({}); } else { node.status({}); }
} }
var flushDelayList = function() { var flushDelayList = function(n) {
var len = node.idList.length; var len = node.idList.length;
if (typeof(n) == 'number') { len = Math.min(Math.floor(n),len); }
for (var i=0; i<len; i++ ) { node.idList[0].trigger(); } for (var i=0; i<len; i++ ) { node.idList[0].trigger(); }
node.idList = []; node.status({fill:"blue",shape:"dot",text:node.idList.length});
node.status({text:"flushed"});
} }
node.reportDepth = function() { node.reportDepth = function() {
if (!node.busy) { if (!node.busy) {
node.busy = setTimeout(function() { node.busy = setTimeout(function() {
if (node.buffer.length > 0) { node.status({text:node.buffer.length}); } // if (node.buffer.length > 0) { node.status({text:node.buffer.length}); }
else { node.status({}); } // else { node.status({}); }
node.status({fill:"blue",shape:"dot",text:node.buffer.length});
node.busy = null; node.busy = null;
}, 500); }, 500);
} }
@ -154,16 +158,18 @@ module.exports = function(RED) {
node.on("input", function(msg, send, done) { node.on("input", function(msg, send, done) {
var id = ourTimeout(function() { var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
if (node.idList.length === 0) { node.status({}); } if (node.timeout > 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
send(msg); send(msg);
done(); done();
}, node.timeout, () => done()); }, node.timeout, () => done());
if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); } if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); }
else { node.idList.push(id); } else { node.idList.push(id); }
if (msg.hasOwnProperty("reset")) { clearDelayList(true); } if (msg.hasOwnProperty("reset")) { clearDelayList(true); }
if (msg.hasOwnProperty("flush")) { flushDelayList(); done(); } else if (msg.hasOwnProperty("flush")) { flushDelayList(msg.flush); done(); }
if ((node.timeout > 1000) && (node.idList.length !== 0)) { else if (node.timeout > 1000) {
node.status({fill:"blue",shape:"dot",text:" "}); node.status({fill:"blue",shape:"dot",text:node.idList.length});
} }
}); });
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
@ -179,13 +185,16 @@ module.exports = function(RED) {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
if (node.idList.length === 0) { node.status({}); } if (node.idList.length === 0) { node.status({}); }
send(msg); send(msg);
if (delayvar >= 0) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
done(); done();
}, delayvar, () => done()); }, delayvar, () => done());
node.idList.push(id); node.idList.push(id);
if (msg.hasOwnProperty("reset")) { clearDelayList(true); } if (msg.hasOwnProperty("reset")) { clearDelayList(true); }
if (msg.hasOwnProperty("flush")) { flushDelayList(); done(); } if (msg.hasOwnProperty("flush")) { flushDelayList(msg.flush); done(); }
if ((delayvar >= 0) && (node.idList.length !== 0)) { if (delayvar >= 0) {
node.status({fill:"blue",shape:"dot",text:delayvar/1000+"s"}); node.status({fill:"blue",shape:"dot",text:node.idList.length});
} }
}); });
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });
@ -200,7 +209,7 @@ module.exports = function(RED) {
delete node.lastSent; delete node.lastSent;
node.buffer = []; node.buffer = [];
node.rate = node.fixedrate; node.rate = node.fixedrate;
node.status({text:"reset"}); node.status(node.status({fill:"blue",shape:"ring",text:0}));
done(); done();
return; return;
} }
@ -208,37 +217,48 @@ module.exports = function(RED) {
if (!node.drop) { if (!node.drop) {
var m = RED.util.cloneMessage(msg); var m = RED.util.cloneMessage(msg);
delete m.flush; delete m.flush;
if (node.intervalID !== -1) { if (Object.keys(m).length > 1) {
if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) { if (node.intervalID !== -1) {
node.rate = msg.rate; if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate)) && node.rate !== msg.rate) {
clearInterval(node.intervalID); node.rate = msg.rate;
node.intervalID = setInterval(sendMsgFromBuffer, node.rate); clearInterval(node.intervalID);
node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
}
var max_msgs = maxKeptMsgsCount(node);
if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) {
node.buffer = [];
node.error(RED._("delay.errors.too-many"), msg);
} else {
node.buffer.push({msg: m, send: send, done: done});
node.reportDepth();
}
} }
var max_msgs = maxKeptMsgsCount(node); else {
if ((max_msgs > 0) && (node.buffer.length >= max_msgs)) { if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) {
node.buffer = []; node.rate = msg.rate;
node.error(RED._("delay.errors.too-many"), msg); }
} else { send(m);
node.buffer.push({msg: m, send: send, done: done});
node.reportDepth(); node.reportDepth();
node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
done();
} }
} }
else {
if (node.allowrate && msg.hasOwnProperty("rate") && !isNaN(parseFloat(msg.rate))) {
node.rate = msg.rate;
}
send(m);
node.reportDepth();
node.intervalID = setInterval(sendMsgFromBuffer, node.rate);
done();
}
if (msg.hasOwnProperty("flush")) { if (msg.hasOwnProperty("flush")) {
while (node.buffer.length > 0) { var len = node.buffer.length;
if (typeof(msg.flush) == 'number') { len = Math.min(Math.floor(msg.flush),len); }
while (len > 0) {
const msgInfo = node.buffer.shift(); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg); if (Object.keys(msgInfo.msg).length > 1) {
msgInfo.done(); node.send(msgInfo.msg);
msgInfo.done();
}
len = len - 1;
} }
node.status({}); if (node.buffer.length === 0) {
clearInterval(node.intervalID);
node.intervalID = -1;
}
node.status({fill:"blue",shape:"dot",text:node.buffer.length});
done(); done();
} }
} }
@ -340,10 +360,16 @@ module.exports = function(RED) {
done(); done();
} }
if (msg.hasOwnProperty("flush")) { if (msg.hasOwnProperty("flush")) {
while (node.buffer.length > 0) { var len = node.buffer.length;
if (typeof(msg.flush) == 'number') { len = Math.min(Math.floor(msg.flush,len)); }
while (len > 0) {
const msgInfo = node.buffer.shift(); const msgInfo = node.buffer.shift();
msgInfo.send(msgInfo.msg); delete msgInfo.msg.flush;
msgInfo.done(); if (Object.keys(msgInfo.msg).length > 2) {
node.send(msgInfo.msg);
msgInfo.done();
}
len = len - 1;
} }
node.status({}); node.status({});
done(); done();
@ -365,15 +391,17 @@ module.exports = function(RED) {
var id = ourTimeout(function() { var id = ourTimeout(function() {
node.idList.splice(node.idList.indexOf(id),1); node.idList.splice(node.idList.indexOf(id),1);
send(msg); send(msg);
node.status({}); if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:node.idList.length});
}
done(); done();
}, wait, () => done()); }, wait, () => done());
if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); } if (Object.keys(msg).length === 2 && msg.hasOwnProperty("flush")) { id.clear(); }
else { node.idList.push(id); } else { node.idList.push(id); }
if (msg.hasOwnProperty("reset")) { clearDelayList(true); } if (msg.hasOwnProperty("reset")) { clearDelayList(true); }
if (msg.hasOwnProperty("flush")) { flushDelayList(); done(); } if (msg.hasOwnProperty("flush")) { flushDelayList(msg.flush); done(); }
if ((node.timeout >= 1000) && (node.idList.length !== 0)) { if (node.timeout >= 1000) {
node.status({fill:"blue",shape:"dot",text:parseInt(wait/10)/100+"s"}); node.status({fill:"blue",shape:"dot",text:node.idList.length});
} }
}); });
node.on("close", function() { clearDelayList(); }); node.on("close", function() { clearDelayList(); });

View File

@ -32,7 +32,8 @@
<dd>If the received message has this property set to any value, all <dd>If the received message has this property set to any value, all
outstanding messages held by the node are cleared without being sent.</dd> outstanding messages held by the node are cleared without being sent.</dd>
<dt class="optional">flush</dt> <dt class="optional">flush</dt>
<dd>If the received message has this property set to any value, all <dd>If the received message has this property set to a numeric value then that many messages
will be released immediately. If set to any other type (e.g. boolean), then all
outstanding messages held by the node are sent immediately.</dd> outstanding messages held by the node are sent immediately.</dd>
</dl> </dl>
<h3>Details</h3> <h3>Details</h3>

View File

@ -809,8 +809,9 @@ describe('delay Node', function() {
{msg:{payload:3,flush:true}, avr:0, var:100}]); {msg:{payload:3,flush:true}, avr:0, var:100}]);
}); });
it('calls done when queued messages are sent (queue)', function(done) { it('calls done when queued messages are sent (queue)', function(done) {
mapiDoneTestHelper(done, "queue", false, [{msg:{payload:1,topic:"a"}, avr:500, var:700}, this.timeout(3000);
{msg:{payload:2, topic:"b"}, avr:1500, var:700}]); mapiDoneTestHelper(done, "queue", false, [{msg:{payload:1,topic:"A"}, avr:1000, var:700},
{msg:{payload:2,topic:"B"}, avr:2000, var:700}]);
}); });
it('calls done when queued messages are sent (timed)', function(done) { it('calls done when queued messages are sent (timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:500, var:700}, mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:500, var:700},
@ -819,7 +820,7 @@ describe('delay Node', function() {
it('calls done when queue is reset (queue/timed)', function(done) { it('calls done when queue is reset (queue/timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500}, mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500},
{msg:{payload:2,reset:true}, avr:0, var:500}]); {msg:{payload:2,reset:true}, avr:0, var:500}]);
}); });
it('calls done when queue is flushed (queue/timed)', function(done) { it('calls done when queue is flushed (queue/timed)', function(done) {
mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500}, mapiDoneTestHelper(done, "timed", false, [{msg:{payload:1,topic:"a"}, avr:0, var:500},
{msg:{payload:2,flush:true}, avr:0, var:500}]); {msg:{payload:2,flush:true}, avr:0, var:500}]);