diff --git a/app.js b/app.js index d37a883c8..45e70c8b5 100644 --- a/app.js +++ b/app.js @@ -309,9 +309,15 @@ process.on('uncaughtException', function (err) { process.exit(1) }) +let alreadyHandlingTermSignals = false // install exit handler function handleTermSignals () { + if (alreadyHandlingTermSignals) { + logger.info('Forcefully exiting.') + process.exit(1) + } logger.info('HedgeDoc has been killed by signal, try to exit gracefully...') + alreadyHandlingTermSignals = true realtime.maintenance = true // disconnect all socket.io clients Object.keys(io.sockets.sockets).forEach(function (key) { @@ -331,17 +337,28 @@ function handleTermSignals () { } }) } + const maxCleanTries = 30 + let currentCleanTry = 1 const checkCleanTimer = setInterval(function () { if (realtime.isReady()) { models.Revision.checkAllNotesRevision(function (err, notes) { - if (err) return logger.error(err) + if (err) { + logger.error('Error while saving note revisions: ' + err) + if (currentCleanTry <= maxCleanTries) { + logger.warn(`Trying again. Try ${currentCleanTry} of ${maxCleanTries}`) + currentCleanTry++ + return null + } + logger.error(`Could not save note revisions after ${maxCleanTries} tries! Exiting.`) + process.exit(1) + } if (!notes || notes.length <= 0) { clearInterval(checkCleanTimer) - return process.exit(0) + process.exit(0) } }) } - }, 100) + }, 200) } process.on('SIGINT', handleTermSignals) process.on('SIGTERM', handleTermSignals) diff --git a/public/docs/release-notes.md b/public/docs/release-notes.md index 58b945fea..7134a3d92 100644 --- a/public/docs/release-notes.md +++ b/public/docs/release-notes.md @@ -11,6 +11,7 @@ ### Bugfixes - Fix crash when trying to read the current Git commit on startup +- Fix endless loop on shutdown when HedgeDoc can't connect to the database ## 1.8.2 2021-05-11