2018-11-27 08:41:10 -05:00
|
|
|
Metrics = require "metrics-sharelatex"
|
2019-02-12 08:27:13 -05:00
|
|
|
Metrics.initialize("doc-updater")
|
2018-11-27 08:41:10 -05:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
express = require('express')
|
|
|
|
http = require("http")
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require('logger-sharelatex')
|
2019-02-01 15:04:43 -05:00
|
|
|
logger.initialize("document-updater")
|
2019-07-31 10:25:54 -04:00
|
|
|
|
|
|
|
logger.logger.addSerializers(require("./app/js/LoggerSerializers"))
|
|
|
|
|
2017-02-14 09:34:28 -05:00
|
|
|
if Settings.sentry?.dsn?
|
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
|
|
|
|
2014-08-07 06:45:19 -04:00
|
|
|
RedisManager = require('./app/js/RedisManager')
|
2014-08-11 09:16:05 -04:00
|
|
|
DispatchManager = require('./app/js/DispatchManager')
|
2019-09-30 10:35:05 -04:00
|
|
|
DeleteQueueManager = require('./app/js/DeleteQueueManager')
|
2014-02-12 05:40:42 -05:00
|
|
|
Errors = require "./app/js/Errors"
|
|
|
|
HttpController = require "./app/js/HttpController"
|
2019-08-07 11:25:23 -04:00
|
|
|
mongojs = require "./app/js/mongojs"
|
|
|
|
async = require "async"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2014-05-08 04:28:13 -04:00
|
|
|
Path = require "path"
|
2020-03-19 11:20:32 -04:00
|
|
|
bodyParser = require "body-parser"
|
2018-11-27 08:41:10 -05:00
|
|
|
|
2014-05-08 04:28:13 -04:00
|
|
|
Metrics.mongodb.monitor(Path.resolve(__dirname + "/node_modules/mongojs/node_modules/mongodb"), logger)
|
2016-08-23 04:53:17 -04:00
|
|
|
Metrics.event_loop.monitor(logger, 100)
|
2014-05-08 04:28:13 -04:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
app = express()
|
2020-03-19 11:20:32 -04:00
|
|
|
app.use(Metrics.http.monitor(logger));
|
2020-03-19 11:39:57 -04:00
|
|
|
app.use bodyParser.json({limit: (Settings.max_doc_length + 64 * 1024)})
|
2019-01-04 04:22:09 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2014-08-11 09:16:05 -04:00
|
|
|
DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-09-02 09:03:38 -04:00
|
|
|
app.param 'project_id', (req, res, next, project_id) ->
|
|
|
|
if project_id?.match /^[0-9a-f]{24}$/
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
next new Error("invalid project id")
|
|
|
|
|
|
|
|
app.param 'doc_id', (req, res, next, doc_id) ->
|
|
|
|
if doc_id?.match /^[0-9a-f]{24}$/
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
next new Error("invalid doc id")
|
|
|
|
|
2017-05-04 10:32:54 -04:00
|
|
|
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
|
2017-10-11 10:44:35 -04:00
|
|
|
# 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
|
2017-10-11 10:29:57 -04:00
|
|
|
app.post '/project/:project_id/get_and_flush_if_old', HttpController.getProjectDocsAndFlushIfOld
|
2017-09-12 06:39:18 -04:00
|
|
|
app.post '/project/:project_id/clearState', HttpController.clearProjectState
|
2017-05-04 10:32:54 -04:00
|
|
|
app.post '/project/:project_id/doc/:doc_id', HttpController.setDoc
|
|
|
|
app.post '/project/:project_id/doc/:doc_id/flush', HttpController.flushDocIfLoaded
|
2020-03-07 08:11:18 -05:00
|
|
|
app.delete '/project/:project_id/doc/:doc_id', HttpController.deleteDoc
|
2017-05-04 10:32:54 -04:00
|
|
|
app.delete '/project/:project_id', HttpController.deleteProject
|
2019-09-25 11:42:49 -04:00
|
|
|
app.delete '/project', HttpController.deleteMultipleProjects
|
2018-02-28 12:31:43 -05:00
|
|
|
app.post '/project/:project_id', HttpController.updateProject
|
2018-03-07 08:29:09 -05:00
|
|
|
app.post '/project/:project_id/history/resync', HttpController.resyncProjectHistory
|
2017-05-04 10:32:54 -04:00
|
|
|
app.post '/project/:project_id/flush', HttpController.flushProject
|
2017-05-05 10:12:06 -04:00
|
|
|
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
|
2020-03-19 11:39:57 -04:00
|
|
|
app.delete '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2019-05-02 11:30:36 -04:00
|
|
|
app.get '/flush_all_projects', HttpController.flushAllProjects
|
2019-09-25 11:42:49 -04:00
|
|
|
app.get '/flush_queued_projects', HttpController.flushQueuedProjects
|
2019-05-02 11:30:36 -04:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
app.get '/total', (req, res)->
|
2020-03-19 11:20:32 -04:00
|
|
|
timer = new Metrics.Timer("http.allDocList")
|
2014-02-12 05:40:42 -05:00
|
|
|
RedisManager.getCountOfDocsInMemory (err, count)->
|
|
|
|
timer.done()
|
|
|
|
res.send {total:count}
|
2020-03-19 11:20:32 -04:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
app.get '/status', (req, res)->
|
2014-05-07 05:05:07 -04:00
|
|
|
if Settings.shuttingDown
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 503 # Service unavailable
|
2014-05-07 05:05:07 -04:00
|
|
|
else
|
|
|
|
res.send('document updater is alive')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2019-07-03 05:21:25 -04:00
|
|
|
pubsubClient = require("redis-sharelatex").createClient(Settings.redis.pubsub)
|
2019-02-07 10:10:40 -05:00
|
|
|
app.get "/health_check/redis", (req, res, next) ->
|
2019-07-03 05:21:25 -04:00
|
|
|
pubsubClient.healthCheck (error) ->
|
2019-02-07 10:10:40 -05:00
|
|
|
if error?
|
|
|
|
logger.err {err: error}, "failed redis health check"
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 500
|
2019-02-07 10:10:40 -05:00
|
|
|
else
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 200
|
2020-03-19 11:20:32 -04:00
|
|
|
|
2019-02-07 10:53:26 -05:00
|
|
|
docUpdaterRedisClient = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
|
2016-06-23 13:00:03 -04:00
|
|
|
app.get "/health_check/redis_cluster", (req, res, next) ->
|
2017-04-12 09:53:03 -04:00
|
|
|
docUpdaterRedisClient.healthCheck (error) ->
|
2016-06-23 13:00:03 -04:00
|
|
|
if error?
|
|
|
|
logger.err {err: error}, "failed redis cluster health check"
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 500
|
2016-06-23 13:00:03 -04:00
|
|
|
else
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 200
|
2014-11-20 06:40:52 -05:00
|
|
|
|
2019-08-07 11:25:23 -04:00
|
|
|
app.get "/health_check", (req, res, next) ->
|
|
|
|
async.series [
|
2020-03-19 11:20:32 -04:00
|
|
|
(cb) ->
|
2019-08-07 11:25:23 -04:00
|
|
|
pubsubClient.healthCheck (error) ->
|
|
|
|
if error?
|
|
|
|
logger.err {err: error}, "failed redis health check"
|
|
|
|
cb(error)
|
|
|
|
(cb) ->
|
|
|
|
docUpdaterRedisClient.healthCheck (error) ->
|
|
|
|
if error?
|
|
|
|
logger.err {err: error}, "failed redis cluster health check"
|
|
|
|
cb(error)
|
|
|
|
(cb) ->
|
|
|
|
mongojs.healthCheck (error) ->
|
|
|
|
if error?
|
|
|
|
logger.err {err: error}, "failed mongo health check"
|
|
|
|
cb(error)
|
|
|
|
] , (error) ->
|
|
|
|
if error?
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 500
|
2019-08-07 11:25:23 -04:00
|
|
|
else
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 200
|
2019-08-07 11:25:23 -04:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
app.use (error, req, res, next) ->
|
|
|
|
if error instanceof Errors.NotFoundError
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 404
|
2016-05-31 08:24:19 -04:00
|
|
|
else if error instanceof Errors.OpRangeNotAvailableError
|
2020-03-19 11:39:57 -04:00
|
|
|
res.sendStatus 422 # Unprocessable Entity
|
2019-11-14 11:32:59 -05:00
|
|
|
else if error.statusCode is 413
|
2020-03-19 11:39:57 -04:00
|
|
|
res.status(413).send("request entity too large")
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
2017-02-14 10:20:05 -05:00
|
|
|
logger.error err: error, req: req, "request errored"
|
2020-03-19 11:39:57 -04:00
|
|
|
res.status(500).send("Oops, something went wrong")
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2014-05-07 05:05:07 -04:00
|
|
|
shutdownCleanly = (signal) ->
|
|
|
|
return () ->
|
|
|
|
logger.log signal: signal, "received interrupt, cleaning up"
|
|
|
|
Settings.shuttingDown = true
|
|
|
|
setTimeout () ->
|
|
|
|
logger.log signal: signal, "shutting down"
|
|
|
|
process.exit()
|
|
|
|
, 10000
|
|
|
|
|
2019-10-03 06:00:24 -04:00
|
|
|
watchForEvent = (eventName)->
|
|
|
|
docUpdaterRedisClient.on eventName, (e)->
|
|
|
|
console.log "redis event: #{eventName} #{e}"
|
|
|
|
|
|
|
|
events = ["connect", "ready", "error", "close", "reconnecting", "end"]
|
|
|
|
for eventName in events
|
|
|
|
watchForEvent(eventName)
|
|
|
|
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
port = Settings.internal?.documentupdater?.port or Settings.apis?.documentupdater?.port or 3003
|
2015-04-30 10:04:43 -04:00
|
|
|
host = Settings.internal.documentupdater.host or "localhost"
|
2018-02-15 11:28:40 -05:00
|
|
|
if !module.parent # Called directly
|
|
|
|
app.listen port, host, ->
|
|
|
|
logger.info "Document-updater starting up, listening on #{host}:#{port}"
|
2019-10-01 10:02:27 -04:00
|
|
|
if Settings.continuousBackgroundFlush
|
|
|
|
logger.info "Starting continuous background flush"
|
|
|
|
DeleteQueueManager.startBackgroundFlush()
|
|
|
|
|
2018-02-15 11:28:40 -05:00
|
|
|
module.exports = app
|
2014-05-07 05:05:07 -04:00
|
|
|
|
|
|
|
for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT']
|
2017-02-14 09:34:28 -05:00
|
|
|
process.on signal, shutdownCleanly(signal)
|
2019-09-30 10:35:05 -04:00
|
|
|
|