Merge pull request #1543 from hedgedoc/feat/db_reconnect

This commit is contained in:
David Mehren 2021-08-14 22:27:29 +02:00 committed by GitHub
commit 84b3504911
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

41
app.js
View file

@ -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) {

View file

@ -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