overleaf/services/real-time/app/js/DeploymentManager.js
Brian Gough 534cb1c5ab Merge pull request #5661 from overleaf/bg-fix-status-on-restart
[real-time] skip 60s closing delay on start up

GitOrigin-RevId: 5fbb3b4f2940505c2ab6e5cfe6a757ee2501bf2d
2021-11-09 09:03:34 +00:00

62 lines
1.9 KiB
JavaScript

const logger = require('logger-sharelatex')
const settings = require('@overleaf/settings')
const fs = require('fs')
// Monitor a status file (e.g. /etc/real_time_status) periodically and close the
// service if the file contents don't contain the matching deployment colour.
const FILE_CHECK_INTERVAL = 5000
const statusFile = settings.deploymentFile
const deploymentColour = settings.deploymentColour
let serviceCloseTime
function updateDeploymentStatus(fileContent) {
const closed = fileContent && !fileContent.includes(deploymentColour)
if (closed && !settings.serviceIsClosed) {
settings.serviceIsClosed = true
serviceCloseTime = Date.now() + 60 * 1000 // delay closing by 1 minute
logger.info({ fileContent }, 'closing service')
} else if (!closed && settings.serviceIsClosed) {
settings.serviceIsClosed = false
logger.info({ fileContent }, 'opening service')
}
}
function pollStatusFile() {
fs.readFile(statusFile, { encoding: 'utf8' }, (err, fileContent) => {
if (err) {
logger.error(
{ file: statusFile, fsErr: err },
'error reading service status file'
)
return
}
updateDeploymentStatus(fileContent)
})
}
function checkStatusFileSync() {
// crash on start up if file does not exist
const content = fs.readFileSync(statusFile, { encoding: 'utf8' })
updateDeploymentStatus(content)
if (settings.serviceIsClosed) {
serviceCloseTime = Date.now() // skip closing delay on start up
}
}
module.exports = {
initialise() {
if (statusFile && deploymentColour) {
logger.info(
{ statusFile, deploymentColour, interval: FILE_CHECK_INTERVAL },
'monitoring deployment status file'
)
checkStatusFileSync() // perform an initial synchronous check at start up
setInterval(pollStatusFile, FILE_CHECK_INTERVAL) // continue checking periodically
}
},
deploymentIsClosed() {
return settings.serviceIsClosed && Date.now() >= serviceCloseTime
},
}