From 44ebf12d257e4e1fcfb709ea0727748eeff012fc Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 4 May 2021 18:25:30 +0200 Subject: [PATCH 1/2] Automatically retry DB connection on startup This adds retry logic to the initial DB connection on startup. HedgeDoc now tries connecting to the database up to 30 times, waiting one second after each try. This gives a database that was simultaneously started (e.g. via docker-compose) enough time to get ready to accept connections. Signed-off-by: David Mehren --- app.js | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index beb0743b9..da1c8e9c0 100644 --- a/app.js +++ b/app.js @@ -272,21 +272,38 @@ function startListen () { } } -// sync db then start listen -models.sequelize.authenticate().then(function () { - models.runMigrations().then(() => { - sessionStore.sync() - // check if realtime is ready - if (realtime.isReady()) { - models.Revision.checkAllNotesRevision(function (err, notes) { - if (err) throw new Error(err) - if (!notes || notes.length <= 0) return startListen() - }) +const maxDBTries = 30 +let currentDBTry = 1 +function syncAndListen () { + // sync db then start listen + models.sequelize.authenticate().then(function () { + models.runMigrations().then(() => { + sessionStore.sync() + // check if realtime is ready + if (realtime.isReady()) { + models.Revision.checkAllNotesRevision(function (err, notes) { + if (err) throw new Error(err) + if (!notes || notes.length <= 0) return startListen() + }) + } else { + logger.error('server still not ready after db synced') + process.exit(1) + } + }) + }).catch(() => { + if (currentDBTry < maxDBTries) { + logger.warn(`Database cannot be reached. Try ${currentDBTry} of ${maxDBTries}.`) + currentDBTry++ + setTimeout(function () { + syncAndListen() + }, 1000) } else { - throw new Error('server still not ready after db synced') + logger.error('Cannot reach database! Exiting.') + process.exit(1) } }) -}) +} +syncAndListen() // log uncaught exception process.on('uncaughtException', function (err) { From 4ad5c705c42c8b67475dab1265a41cb95f48c2b7 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 14 Aug 2021 17:21:45 +0200 Subject: [PATCH 2/2] Add changelog entry for DB auto-reconnect Signed-off-by: David Mehren --- public/docs/release-notes.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/docs/release-notes.md b/public/docs/release-notes.md index 6490601c1..58b945fea 100644 --- a/public/docs/release-notes.md +++ b/public/docs/release-notes.md @@ -5,7 +5,10 @@ they were repeatedly used to exploit security vulnerabilities. If you want to continue using Google Analytics or Disqus, you can re-enable them in the config. See [the docs](https://docs.hedgedoc.org/configuration/#web-security-aspects) for details. - + +### Features +- HedgeDoc now automatically retries connecting to the database up to 30 times on startup. + ### Bugfixes - Fix crash when trying to read the current Git commit on startup