From b44629c84e5e1d01997a22bc38a69cbc6bc112a1 Mon Sep 17 00:00:00 2001 From: Skylar Sadlier Date: Thu, 3 Feb 2022 10:15:52 -0700 Subject: [PATCH] - When checking authentication we should retry startup if the error we receive isn't related to invalid auth token (M_UNKNOWN_TOKEN) --- src/matrix-server-config.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/matrix-server-config.js b/src/matrix-server-config.js index 6a95cac..bd8fd3c 100644 --- a/src/matrix-server-config.js +++ b/src/matrix-server-config.js @@ -38,6 +38,8 @@ module.exports = function(RED) { localStorage = new LocalStorage(localStorageDir), initialSetup = false; + let retryStartTimeout = null; + if(!this.credentials.accessToken) { node.log("Matrix connection failed: missing access token."); } else if(!this.url) { @@ -121,6 +123,10 @@ module.exports = function(RED) { node.matrixClient.stopClient(); node.setConnected(false); } + + if(retryStartTimeout) { + clearTimeout(retryStartTimeout); + } } node.on('close', function(done) { @@ -290,6 +296,7 @@ module.exports = function(RED) { // httpStatus: 401 // } + console.log("Authentication failure: ", errorObj); node.error("Authentication failure: " + errorObj); stopClient(); }); @@ -312,13 +319,25 @@ module.exports = function(RED) { // do an authed request and only continue if we don't get an error // this prevent the matrix client from crashing Node-RED on invalid auth token - node.matrixClient.getAccountDataFromServer() - .then( - function() { - run().catch((error) => node.error(error)); - }, - function(err) {} - ); + (function checkAuthTokenThenStart() { + if(node.matrixClient.clientRunning) { + return; + } + + node.matrixClient.getAccountDataFromServer() + .then( + function() { + run().catch((error) => node.error(error)); + }, + function(err) { + // if the error isn't authentication related retry in a little bit + if(err.code !== "M_UNKNOWN_TOKEN") { + retryStartTimeout = setTimeout(checkAuthTokenThenStart, 15000); + node.error("Auth check failed: " + err); + } + } + ) + })(); } } @@ -344,7 +363,8 @@ module.exports = function(RED) { const matrixClient = sdk.createClient({ baseUrl: baseUrl, - deviceId: deviceId + deviceId: deviceId, + localTimeoutMs: '30000' }); matrixClient.login(