2023-10-18 04:32:14 -04:00
|
|
|
// Metrics must be initialized before importing anything else
|
|
|
|
require('@overleaf/metrics/initialize')
|
2020-05-06 06:13:10 -04:00
|
|
|
|
2023-10-18 04:32:14 -04:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-05-06 06:13:11 -04:00
|
|
|
const express = require('express')
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.initialize('document-updater')
|
2020-05-06 06:13:10 -04:00
|
|
|
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.logger.addSerializers(require('./app/js/LoggerSerializers'))
|
2020-05-06 06:13:10 -04:00
|
|
|
|
2020-05-08 11:34:01 -04:00
|
|
|
if (Settings.sentry != null && Settings.sentry.dsn != null) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:13:11 -04:00
|
|
|
const RedisManager = require('./app/js/RedisManager')
|
|
|
|
const DispatchManager = require('./app/js/DispatchManager')
|
|
|
|
const DeleteQueueManager = require('./app/js/DeleteQueueManager')
|
|
|
|
const Errors = require('./app/js/Errors')
|
|
|
|
const HttpController = require('./app/js/HttpController')
|
2020-08-25 07:32:16 -04:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-05-06 06:13:11 -04:00
|
|
|
const async = require('async')
|
|
|
|
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
|
|
|
|
Metrics.event_loop.monitor(logger, 100)
|
2023-05-22 06:36:51 -04:00
|
|
|
Metrics.open_sockets.monitor()
|
2020-05-06 06:13:11 -04:00
|
|
|
|
|
|
|
const app = express()
|
2020-05-08 11:53:02 -04:00
|
|
|
app.use(bodyParser.json({ limit: Settings.maxJsonRequestSize }))
|
2020-05-06 06:13:11 -04:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
|
2021-02-02 10:10:04 -05:00
|
|
|
DispatchManager.createAndStartDispatchers(Settings.dispatcherCount)
|
2020-05-06 06:13:11 -04:00
|
|
|
|
2020-05-08 11:28:31 -04:00
|
|
|
app.get('/status', (req, res) => {
|
2020-05-06 06:13:11 -04:00
|
|
|
if (Settings.shuttingDown) {
|
|
|
|
return res.sendStatus(503) // Service unavailable
|
|
|
|
} else {
|
|
|
|
return res.send('document updater is alive')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-10 06:32:04 -05:00
|
|
|
const pubsubClient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 06:13:11 -04:00
|
|
|
Settings.redis.pubsub
|
|
|
|
)
|
2020-05-08 11:28:31 -04:00
|
|
|
app.get('/health_check/redis', (req, res, next) => {
|
2021-07-13 07:04:42 -04:00
|
|
|
pubsubClient.healthCheck(error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.err({ err: error }, 'failed redis health check')
|
|
|
|
return res.sendStatus(500)
|
2020-05-06 06:13:10 -04:00
|
|
|
} else {
|
2020-05-06 06:13:11 -04:00
|
|
|
return res.sendStatus(200)
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
2020-05-06 06:13:11 -04:00
|
|
|
})
|
2020-05-08 11:28:31 -04:00
|
|
|
})
|
2020-05-06 06:13:11 -04:00
|
|
|
|
2020-11-10 06:32:04 -05:00
|
|
|
const docUpdaterRedisClient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 06:13:11 -04:00
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
2020-05-08 11:28:31 -04:00
|
|
|
app.get('/health_check/redis_cluster', (req, res, next) => {
|
2021-07-13 07:04:42 -04:00
|
|
|
docUpdaterRedisClient.healthCheck(error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.err({ err: error }, 'failed redis cluster health check')
|
|
|
|
return res.sendStatus(500)
|
2020-05-06 06:13:10 -04:00
|
|
|
} else {
|
2020-05-06 06:13:11 -04:00
|
|
|
return res.sendStatus(200)
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
2020-05-06 06:13:11 -04:00
|
|
|
})
|
2020-05-08 11:28:31 -04:00
|
|
|
})
|
2020-05-06 06:13:11 -04:00
|
|
|
|
2020-05-08 11:28:31 -04:00
|
|
|
app.get('/health_check', (req, res, next) => {
|
2020-05-06 06:13:11 -04:00
|
|
|
async.series(
|
|
|
|
[
|
2021-07-13 07:04:42 -04:00
|
|
|
cb => {
|
|
|
|
pubsubClient.healthCheck(error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.err({ err: error }, 'failed redis health check')
|
|
|
|
}
|
2020-05-08 11:28:31 -04:00
|
|
|
cb(error)
|
|
|
|
})
|
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
cb => {
|
|
|
|
docUpdaterRedisClient.healthCheck(error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.err({ err: error }, 'failed redis cluster health check')
|
|
|
|
}
|
2020-05-08 11:28:31 -04:00
|
|
|
cb(error)
|
|
|
|
})
|
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
cb => {
|
|
|
|
mongodb.healthCheck(error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
logger.err({ err: error }, 'failed mongo health check')
|
|
|
|
}
|
2020-05-08 11:28:31 -04:00
|
|
|
cb(error)
|
2020-05-06 06:13:11 -04:00
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:13:11 -04:00
|
|
|
],
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-08 11:34:01 -04:00
|
|
|
if (error) {
|
2020-05-06 06:13:11 -04:00
|
|
|
return res.sendStatus(500)
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(200)
|
|
|
|
}
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
2020-05-06 06:13:11 -04:00
|
|
|
)
|
2020-05-08 11:28:31 -04:00
|
|
|
})
|
2020-05-06 06:13:11 -04:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
// record http metrics for the routes below this point
|
|
|
|
app.use(Metrics.http.monitor(logger))
|
|
|
|
|
|
|
|
app.param('project_id', (req, res, next, projectId) => {
|
|
|
|
if (projectId != null && projectId.match(/^[0-9a-f]{24}$/)) {
|
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid project id'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.param('doc_id', (req, res, next, docId) => {
|
|
|
|
if (docId != null && docId.match(/^[0-9a-f]{24}$/)) {
|
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid doc id'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-06-01 03:55:48 -04:00
|
|
|
// Record requests that come in after we've started shutting down - for investigation.
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (Settings.shuttingDown) {
|
|
|
|
logger.warn(
|
|
|
|
{ req, timeSinceShutdown: Date.now() - Settings.shutDownTime },
|
|
|
|
'request received after shutting down'
|
|
|
|
)
|
2023-07-24 06:32:30 -04:00
|
|
|
// We don't want keep-alive connections to be kept open when the server is shutting down.
|
|
|
|
res.set('Connection', 'close')
|
2023-06-01 03:55:48 -04:00
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
|
|
|
|
app.get('/project/:project_id/doc/:doc_id/peek', HttpController.peekDoc)
|
|
|
|
// temporarily keep the GET method for backwards compatibility
|
|
|
|
app.get('/project/:project_id/doc', HttpController.getProjectDocsAndFlushIfOld)
|
|
|
|
// will migrate to the POST method of get_and_flush_if_old instead
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/get_and_flush_if_old',
|
|
|
|
HttpController.getProjectDocsAndFlushIfOld
|
|
|
|
)
|
|
|
|
app.post('/project/:project_id/clearState', HttpController.clearProjectState)
|
|
|
|
app.post('/project/:project_id/doc/:doc_id', HttpController.setDoc)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id/flush',
|
|
|
|
HttpController.flushDocIfLoaded
|
|
|
|
)
|
|
|
|
app.delete('/project/:project_id/doc/:doc_id', HttpController.deleteDoc)
|
|
|
|
app.delete('/project/:project_id', HttpController.deleteProject)
|
|
|
|
app.delete('/project', HttpController.deleteMultipleProjects)
|
|
|
|
app.post('/project/:project_id', HttpController.updateProject)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/history/resync',
|
2022-06-17 04:03:31 -04:00
|
|
|
longerTimeout,
|
2021-09-30 04:28:32 -04:00
|
|
|
HttpController.resyncProjectHistory
|
|
|
|
)
|
|
|
|
app.post('/project/:project_id/flush', HttpController.flushProject)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id/change/:change_id/accept',
|
|
|
|
HttpController.acceptChanges
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id/change/accept',
|
|
|
|
HttpController.acceptChanges
|
|
|
|
)
|
|
|
|
app.delete(
|
|
|
|
'/project/:project_id/doc/:doc_id/comment/:comment_id',
|
|
|
|
HttpController.deleteComment
|
|
|
|
)
|
|
|
|
|
|
|
|
app.get('/flush_all_projects', HttpController.flushAllProjects)
|
|
|
|
app.get('/flush_queued_projects', HttpController.flushQueuedProjects)
|
|
|
|
|
|
|
|
app.get('/total', (req, res, next) => {
|
|
|
|
const timer = new Metrics.Timer('http.allDocList')
|
|
|
|
RedisManager.getCountOfDocsInMemory((err, count) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
timer.done()
|
|
|
|
res.send({ total: count })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-08 11:28:31 -04:00
|
|
|
app.use((error, req, res, next) => {
|
2020-05-06 06:13:11 -04:00
|
|
|
if (error instanceof Errors.NotFoundError) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
} else if (error instanceof Errors.OpRangeNotAvailableError) {
|
|
|
|
return res.sendStatus(422) // Unprocessable Entity
|
2022-07-06 05:50:16 -04:00
|
|
|
} else if (error instanceof Errors.FileTooLargeError) {
|
|
|
|
return res.sendStatus(413)
|
2020-05-06 06:13:11 -04:00
|
|
|
} else if (error.statusCode === 413) {
|
|
|
|
return res.status(413).send('request entity too large')
|
|
|
|
} else {
|
|
|
|
logger.error({ err: error, req }, 'request errored')
|
|
|
|
return res.status(500).send('Oops, something went wrong')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-13 07:04:42 -04:00
|
|
|
const shutdownCleanly = signal => () => {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.info({ signal }, 'received interrupt, cleaning up')
|
2023-06-01 03:55:48 -04:00
|
|
|
if (Settings.shuttingDown) {
|
|
|
|
logger.warn({ signal }, 'already shutting down, ignoring interrupt')
|
|
|
|
return
|
|
|
|
}
|
2020-05-08 11:28:31 -04:00
|
|
|
Settings.shuttingDown = true
|
2023-06-01 03:55:48 -04:00
|
|
|
// record the time we started shutting down
|
|
|
|
Settings.shutDownTime = Date.now()
|
2020-05-08 11:28:31 -04:00
|
|
|
setTimeout(() => {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.info({ signal }, 'shutting down')
|
2020-05-08 11:28:31 -04:00
|
|
|
process.exit()
|
2023-12-07 05:05:50 -05:00
|
|
|
}, Settings.delayShutdownMs)
|
2020-05-08 11:28:31 -04:00
|
|
|
}
|
2020-05-06 06:13:11 -04:00
|
|
|
|
2021-07-13 07:04:42 -04:00
|
|
|
const watchForEvent = eventName => {
|
|
|
|
docUpdaterRedisClient.on(eventName, e => {
|
2020-05-08 11:28:31 -04:00
|
|
|
console.log(`redis event: ${eventName} ${e}`) // eslint-disable-line no-console
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 06:13:11 -04:00
|
|
|
|
|
|
|
const events = ['connect', 'ready', 'error', 'close', 'reconnecting', 'end']
|
2020-05-08 11:21:36 -04:00
|
|
|
for (const eventName of events) {
|
2020-05-06 06:13:11 -04:00
|
|
|
watchForEvent(eventName)
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:13:11 -04:00
|
|
|
const port =
|
2020-05-08 11:32:03 -04:00
|
|
|
Settings.internal.documentupdater.port ||
|
|
|
|
(Settings.api &&
|
|
|
|
Settings.api.documentupdater &&
|
|
|
|
Settings.api.documentupdater.port) ||
|
2020-05-06 06:13:11 -04:00
|
|
|
3003
|
|
|
|
const host = Settings.internal.documentupdater.host || 'localhost'
|
2020-05-08 11:32:03 -04:00
|
|
|
|
2020-05-06 06:13:11 -04:00
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2023-01-10 07:57:21 -05:00
|
|
|
mongodb.mongoClient
|
|
|
|
.connect()
|
2020-08-25 07:32:16 -04:00
|
|
|
.then(() => {
|
|
|
|
app.listen(port, host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
logger.info(
|
|
|
|
`Document-updater starting up, listening on ${host}:${port}`
|
|
|
|
)
|
|
|
|
if (Settings.continuousBackgroundFlush) {
|
|
|
|
logger.info('Starting continuous background flush')
|
|
|
|
DeleteQueueManager.startBackgroundFlush()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
.catch(err => {
|
2020-08-25 07:32:16 -04:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:13:11 -04:00
|
|
|
module.exports = app
|
|
|
|
|
|
|
|
for (const signal of [
|
|
|
|
'SIGINT',
|
|
|
|
'SIGHUP',
|
|
|
|
'SIGQUIT',
|
|
|
|
'SIGUSR1',
|
|
|
|
'SIGUSR2',
|
|
|
|
'SIGTERM',
|
2021-07-13 07:04:42 -04:00
|
|
|
'SIGABRT',
|
2020-05-06 06:13:11 -04:00
|
|
|
]) {
|
|
|
|
process.on(signal, shutdownCleanly(signal))
|
2020-05-06 06:13:10 -04:00
|
|
|
}
|
2022-06-17 04:03:31 -04:00
|
|
|
|
|
|
|
function longerTimeout(req, res, next) {
|
|
|
|
res.setTimeout(6 * 60 * 1000)
|
|
|
|
next()
|
|
|
|
}
|