2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-03-19 12:27:27 -04: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 07:04:45 -04:00
|
|
|
let serviceCloseTime
|
2021-03-22 07:15:59 -04:00
|
|
|
|
2021-03-19 12:27:27 -04:00
|
|
|
function updateDeploymentStatus(fileContent) {
|
2021-03-29 06:13:59 -04:00
|
|
|
const closed = fileContent && !fileContent.includes(deploymentColour)
|
2021-03-19 12:27:27 -04:00
|
|
|
if (closed && !settings.serviceIsClosed) {
|
|
|
|
settings.serviceIsClosed = true
|
2021-03-22 07:15:59 -04:00
|
|
|
serviceCloseTime = Date.now() + 60 * 1000 // delay closing by 1 minute
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info({ fileContent }, 'closing service')
|
2021-03-19 12:27:27 -04:00
|
|
|
} else if (!closed && settings.serviceIsClosed) {
|
|
|
|
settings.serviceIsClosed = false
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info({ fileContent }, 'opening service')
|
2021-03-19 12:27:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-11-08 04:41:21 -05:00
|
|
|
if (settings.serviceIsClosed) {
|
|
|
|
serviceCloseTime = Date.now() // skip closing delay on start up
|
|
|
|
}
|
2021-03-19 12:27:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initialise() {
|
|
|
|
if (statusFile && deploymentColour) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info(
|
2021-03-19 12:27:27 -04:00
|
|
|
{ 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
|
|
|
|
}
|
2021-03-22 07:15:59 -04:00
|
|
|
},
|
|
|
|
deploymentIsClosed() {
|
2021-11-08 04:41:21 -05:00
|
|
|
return settings.serviceIsClosed && Date.now() >= serviceCloseTime
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2021-03-19 12:27:27 -04:00
|
|
|
}
|