mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-29 00:04:30 -05:00
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 <git@herrmehren.de>
This commit is contained in:
parent
936b87f3b4
commit
44ebf12d25
1 changed files with 29 additions and 12 deletions
19
app.js
19
app.js
|
@ -272,6 +272,9 @@ function startListen () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxDBTries = 30
|
||||||
|
let currentDBTry = 1
|
||||||
|
function syncAndListen () {
|
||||||
// sync db then start listen
|
// sync db then start listen
|
||||||
models.sequelize.authenticate().then(function () {
|
models.sequelize.authenticate().then(function () {
|
||||||
models.runMigrations().then(() => {
|
models.runMigrations().then(() => {
|
||||||
|
@ -283,10 +286,24 @@ models.sequelize.authenticate().then(function () {
|
||||||
if (!notes || notes.length <= 0) return startListen()
|
if (!notes || notes.length <= 0) return startListen()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
throw new Error('server still not ready after db synced')
|
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 {
|
||||||
|
logger.error('Cannot reach database! Exiting.')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
syncAndListen()
|
||||||
|
|
||||||
// log uncaught exception
|
// log uncaught exception
|
||||||
process.on('uncaughtException', function (err) {
|
process.on('uncaughtException', function (err) {
|
||||||
|
|
Loading…
Reference in a new issue