From 04193dbc6ffbad0687267a17b2e71354884df478 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 4 Sep 2019 22:23:44 -0400 Subject: [PATCH] feat: database connection retries on start --- server/core/db.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server/core/db.js b/server/core/db.js index ccbb3636..ede159c8 100644 --- a/server/core/db.js +++ b/server/core/db.js @@ -108,6 +108,7 @@ module.exports = { const models = autoload(path.join(WIKI.SERVERPATH, 'models')) // Set init tasks + let conAttempts = 0 let initTasks = { // -> Migrate DB Schemas async syncSchemas() { @@ -115,10 +116,28 @@ module.exports = { tableName: 'migrations', migrationSource }) + }, + // -> Attempt initial connection + async connect() { + try { + WIKI.logger.info('Connecting to database...') + await self.knex.raw('SELECT 1 + 1;') + WIKI.logger.info('Database Connection Successful [ OK ]') + } catch (err) { + if (conAttempts < 10) { + WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`) + WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`) + await new Promise(resolve => setTimeout(resolve, 3000)) + await initTasks.connect() + } else { + throw err + } + } } } let initTasksQueue = (WIKI.IS_MASTER) ? [ + initTasks.connect, initTasks.syncSchemas ] : [ () => { return Promise.resolve() }