2020-05-06 10:13:11 +00:00
|
|
|
const Metrics = require('metrics-sharelatex')
|
|
|
|
Metrics.initialize('doc-updater')
|
2020-05-06 10:13:10 +00:00
|
|
|
|
2020-05-06 10:13:11 +00:00
|
|
|
const express = require('express')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
logger.initialize('document-updater')
|
2020-05-06 10:13:10 +00:00
|
|
|
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.logger.addSerializers(require('./app/js/LoggerSerializers'))
|
2020-05-06 10:13:10 +00:00
|
|
|
|
2020-05-08 15:34:01 +00:00
|
|
|
if (Settings.sentry != null && Settings.sentry.dsn != null) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:13:11 +00: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')
|
|
|
|
const mongojs = require('./app/js/mongojs')
|
|
|
|
const async = require('async')
|
|
|
|
|
|
|
|
const Path = require('path')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
|
|
|
|
Metrics.mongodb.monitor(
|
2020-04-28 09:57:58 +00:00
|
|
|
Path.resolve(__dirname, '/node_modules/mongojs/node_modules/mongodb'),
|
2020-05-06 10:13:11 +00:00
|
|
|
logger
|
|
|
|
)
|
|
|
|
Metrics.event_loop.monitor(logger, 100)
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
app.use(Metrics.http.monitor(logger))
|
2020-05-08 15:53:02 +00:00
|
|
|
app.use(bodyParser.json({ limit: Settings.maxJsonRequestSize }))
|
2020-05-06 10:13:11 +00:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
|
|
|
|
DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10)
|
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.param('project_id', (req, res, next, projectId) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (projectId != null && projectId.match(/^[0-9a-f]{24}$/)) {
|
2020-05-06 10:13:11 +00:00
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid project id'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.param('doc_id', (req, res, next, docId) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (docId != null && docId.match(/^[0-9a-f]{24}$/)) {
|
2020-05-06 10:13:11 +00:00
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid doc id'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
|
2020-05-06 10:13:10 +00:00
|
|
|
// temporarily keep the GET method for backwards compatibility
|
2020-05-06 10:13:11 +00:00
|
|
|
app.get('/project/:project_id/doc', HttpController.getProjectDocsAndFlushIfOld)
|
2020-05-06 10:13:10 +00:00
|
|
|
// will migrate to the POST method of get_and_flush_if_old instead
|
2020-05-06 10:13:11 +00:00
|
|
|
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',
|
|
|
|
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)
|
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.get('/total', (req, res, next) => {
|
2020-05-06 10:13:11 +00:00
|
|
|
const timer = new Metrics.Timer('http.allDocList')
|
2020-05-08 15:28:31 +00:00
|
|
|
RedisManager.getCountOfDocsInMemory((err, count) => {
|
2020-04-28 09:57:58 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
timer.done()
|
2020-05-08 15:28:31 +00:00
|
|
|
res.send({ total: count })
|
2020-05-06 10:13:11 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.get('/status', (req, res) => {
|
2020-05-06 10:13:11 +00:00
|
|
|
if (Settings.shuttingDown) {
|
|
|
|
return res.sendStatus(503) // Service unavailable
|
|
|
|
} else {
|
|
|
|
return res.send('document updater is alive')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const pubsubClient = require('redis-sharelatex').createClient(
|
|
|
|
Settings.redis.pubsub
|
|
|
|
)
|
2020-05-08 15:28:31 +00:00
|
|
|
app.get('/health_check/redis', (req, res, next) => {
|
|
|
|
pubsubClient.healthCheck((error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.err({ err: error }, 'failed redis health check')
|
|
|
|
return res.sendStatus(500)
|
2020-05-06 10:13:10 +00:00
|
|
|
} else {
|
2020-05-06 10:13:11 +00:00
|
|
|
return res.sendStatus(200)
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
})
|
2020-05-08 15:28:31 +00:00
|
|
|
})
|
2020-05-06 10:13:11 +00:00
|
|
|
|
|
|
|
const docUpdaterRedisClient = require('redis-sharelatex').createClient(
|
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
2020-05-08 15:28:31 +00:00
|
|
|
app.get('/health_check/redis_cluster', (req, res, next) => {
|
|
|
|
docUpdaterRedisClient.healthCheck((error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.err({ err: error }, 'failed redis cluster health check')
|
|
|
|
return res.sendStatus(500)
|
2020-05-06 10:13:10 +00:00
|
|
|
} else {
|
2020-05-06 10:13:11 +00:00
|
|
|
return res.sendStatus(200)
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
})
|
2020-05-08 15:28:31 +00:00
|
|
|
})
|
2020-05-06 10:13:11 +00:00
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.get('/health_check', (req, res, next) => {
|
2020-05-06 10:13:11 +00:00
|
|
|
async.series(
|
|
|
|
[
|
2020-05-08 15:28:31 +00:00
|
|
|
(cb) => {
|
|
|
|
pubsubClient.healthCheck((error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.err({ err: error }, 'failed redis health check')
|
|
|
|
}
|
2020-05-08 15:28:31 +00:00
|
|
|
cb(error)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
(cb) => {
|
|
|
|
docUpdaterRedisClient.healthCheck((error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.err({ err: error }, 'failed redis cluster health check')
|
|
|
|
}
|
2020-05-08 15:28:31 +00:00
|
|
|
cb(error)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
(cb) => {
|
|
|
|
mongojs.healthCheck((error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.err({ err: error }, 'failed mongo health check')
|
|
|
|
}
|
2020-05-08 15:28:31 +00:00
|
|
|
cb(error)
|
2020-05-06 10:13:11 +00:00
|
|
|
})
|
2020-05-08 15:28:31 +00:00
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
],
|
2020-05-08 15:28:31 +00:00
|
|
|
(error) => {
|
2020-05-08 15:34:01 +00:00
|
|
|
if (error) {
|
2020-05-06 10:13:11 +00:00
|
|
|
return res.sendStatus(500)
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(200)
|
|
|
|
}
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
)
|
2020-05-08 15:28:31 +00:00
|
|
|
})
|
2020-05-06 10:13:11 +00:00
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
app.use((error, req, res, next) => {
|
2020-05-06 10:13:11 +00:00
|
|
|
if (error instanceof Errors.NotFoundError) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
} else if (error instanceof Errors.OpRangeNotAvailableError) {
|
|
|
|
return res.sendStatus(422) // Unprocessable Entity
|
|
|
|
} 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')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
const shutdownCleanly = (signal) => () => {
|
|
|
|
logger.log({ signal }, 'received interrupt, cleaning up')
|
|
|
|
Settings.shuttingDown = true
|
|
|
|
setTimeout(() => {
|
|
|
|
logger.log({ signal }, 'shutting down')
|
|
|
|
process.exit()
|
|
|
|
}, 10000)
|
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
|
2020-05-08 15:28:31 +00:00
|
|
|
const watchForEvent = (eventName) => {
|
|
|
|
docUpdaterRedisClient.on(eventName, (e) => {
|
|
|
|
console.log(`redis event: ${eventName} ${e}`) // eslint-disable-line no-console
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 10:13:11 +00:00
|
|
|
|
|
|
|
const events = ['connect', 'ready', 'error', 'close', 'reconnecting', 'end']
|
2020-05-08 15:21:36 +00:00
|
|
|
for (const eventName of events) {
|
2020-05-06 10:13:11 +00:00
|
|
|
watchForEvent(eventName)
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:13:11 +00:00
|
|
|
const port =
|
2020-05-08 15:32:03 +00:00
|
|
|
Settings.internal.documentupdater.port ||
|
|
|
|
(Settings.api &&
|
|
|
|
Settings.api.documentupdater &&
|
|
|
|
Settings.api.documentupdater.port) ||
|
2020-05-06 10:13:11 +00:00
|
|
|
3003
|
|
|
|
const host = Settings.internal.documentupdater.host || 'localhost'
|
2020-05-08 15:32:03 +00:00
|
|
|
|
2020-05-06 10:13:11 +00:00
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-05-08 15:28:31 +00:00
|
|
|
app.listen(port, host, () => {
|
2020-05-06 10:13:11 +00:00
|
|
|
logger.info(`Document-updater starting up, listening on ${host}:${port}`)
|
|
|
|
if (Settings.continuousBackgroundFlush) {
|
|
|
|
logger.info('Starting continuous background flush')
|
2020-05-08 15:28:31 +00:00
|
|
|
DeleteQueueManager.startBackgroundFlush()
|
2020-05-06 10:13:11 +00:00
|
|
|
}
|
|
|
|
})
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:13:11 +00:00
|
|
|
module.exports = app
|
|
|
|
|
|
|
|
for (const signal of [
|
|
|
|
'SIGINT',
|
|
|
|
'SIGHUP',
|
|
|
|
'SIGQUIT',
|
|
|
|
'SIGUSR1',
|
|
|
|
'SIGUSR2',
|
|
|
|
'SIGTERM',
|
|
|
|
'SIGABRT'
|
|
|
|
]) {
|
|
|
|
process.on(signal, shutdownCleanly(signal))
|
2020-05-06 10:13:10 +00:00
|
|
|
}
|