From d7da3c636cfdc998e802ceba58cee59a46f78601 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 1 Apr 2023 16:28:52 -0400 Subject: [PATCH] Update SQL helper function to retry the database if it's unreachable --- libStorage.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/libStorage.js b/libStorage.js index 90f429f..e80c0b0 100644 --- a/libStorage.js +++ b/libStorage.js @@ -25,17 +25,42 @@ var Connection = mysql.createPool({ }); // Helper Functions -// Function to run and handle SQL errors +/** + * Function to run and handle SQL errors + * @param {string} sqlQuery The SQL query string + * @param {*} connection The SQL connection to be used to query + * @param {function} callback The callback function to be called with an error or the results + * @param {object} params Hidden parameters + * @param {object} params.retry Retry value to increase retry time + */ function runSQL(sqlQuery, connection, callback = (err, rows) => { log.ERROR(err); throw err; -}) { +}, {retry = 0}) { // Start the MySQL Connection if (!connection) connection = Connection; connection.query(sqlQuery, (err, rows) => { if (err) { - log.ERROR("SQL Error on query:", sqlQuery, err); - return callback(err, undefined); + if (err.code === "EHOSTUNREACH") { + // DB Connection is unavailable + let retryTimeout; + switch(retry){ + case 0: + retryTimeout = 30000; + break; + case retry < 15: + retryTimeout = 30000 + retry * 15000; + break; + default: + log.ERROR("Retried Database 15 times over, please check connection status and restart the app", sqlQuery, err); + return callback(err, undefined); + } + log.WARN(`Database connection is unavailable, waiting ${ retryTimeout / 1000 } seconds...`); + retry += 1 + // Wait for the retry timeout before trying the query again + setTimeout(runSQL(sqlQuery, connection, callback, { retry: retry })); + } + else return callback(err, undefined); } log.VERBOSE(`SQL result for query '${sqlQuery}':`, rows); return callback(undefined, rows);