mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Add disconnect timeout of 2s to avoid "Error stopping node"
This commit is contained in:
parent
6d517ceef0
commit
4cf1216845
@ -231,6 +231,20 @@ module.exports = function(RED) {
|
|||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
node.disconnect = function(callback) {
|
node.disconnect = function(callback) {
|
||||||
|
const waitDisconnect = (client, timeout) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// Set flag to avoid race conditions for disconnect as every node tries to call it directly or indirectly using deregister
|
||||||
|
node.closing = true;
|
||||||
|
const t = setTimeout(() => {
|
||||||
|
reject();
|
||||||
|
}, timeout);
|
||||||
|
client.disconnect(() => {
|
||||||
|
clearTimeout(t);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!node.client) {
|
if (!node.client) {
|
||||||
node.warn("Can't disconnect, connection not initialized.");
|
node.warn("Can't disconnect, connection not initialized.");
|
||||||
callback();
|
callback();
|
||||||
@ -238,10 +252,11 @@ module.exports = function(RED) {
|
|||||||
// Disconnection already in progress
|
// Disconnection already in progress
|
||||||
callback();
|
callback();
|
||||||
} else {
|
} else {
|
||||||
node.closing = true;
|
waitDisconnect(node.client, 2000).then(() => {
|
||||||
node.client.disconnect(function() {
|
|
||||||
node.log("Disconnected from STOMP server", {sessionId: node.sessionId, url: `${node.options.address}:${node.options.port}`, protocolVersion: node.options.protocolVersion})
|
node.log("Disconnected from STOMP server", {sessionId: node.sessionId, url: `${node.options.address}:${node.options.port}`, protocolVersion: node.options.protocolVersion})
|
||||||
|
}).catch(() => {
|
||||||
|
node.log("Disconnect timeout closing node...");
|
||||||
|
}).finally(() => {
|
||||||
node.sessionId = null;
|
node.sessionId = null;
|
||||||
node.subscribtionIndex = 1;
|
node.subscribtionIndex = 1;
|
||||||
node.subscriptionIds = {};
|
node.subscriptionIds = {};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user