overleaf/services/real-time/app/js/DeploymentManager.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-03-19 16:27:27 +00:00
const logger = require('logger-sharelatex')
const settings = require('@overleaf/settings')
2021-03-19 16:27:27 +00:00
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
2021-07-13 11:04:45 +00:00
let serviceCloseTime
2021-03-19 16:27:27 +00:00
function updateDeploymentStatus(fileContent) {
2021-03-29 10:13:59 +00:00
const closed = fileContent && !fileContent.includes(deploymentColour)
2021-03-19 16:27:27 +00:00
if (closed && !settings.serviceIsClosed) {
settings.serviceIsClosed = true
serviceCloseTime = Date.now() + 60 * 1000 // delay closing by 1 minute
2021-03-19 16:27:27 +00:00
logger.warn({ fileContent }, 'closing service')
} else if (!closed && settings.serviceIsClosed) {
settings.serviceIsClosed = false
logger.warn({ 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)
}
module.exports = {
initialise() {
if (statusFile && deploymentColour) {
logger.log(
{ 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
2021-07-13 11:04:45 +00:00
},
2021-03-19 16:27:27 +00:00
}