overleaf/services/document-updater/app.coffee

147 lines
5.3 KiB
CoffeeScript
Raw Normal View History

Metrics = require "metrics-sharelatex"
2019-02-12 13:27:13 +00:00
Metrics.initialize("doc-updater")
2014-02-12 10:40:42 +00:00
express = require('express')
http = require("http")
Settings = require('settings-sharelatex')
logger = require('logger-sharelatex')
2019-02-01 20:04:43 +00:00
logger.initialize("document-updater")
logger.logger.addSerializers(require("./app/js/LoggerSerializers"))
2017-02-14 14:34:28 +00:00
if Settings.sentry?.dsn?
logger.initializeErrorReporting(Settings.sentry.dsn)
RedisManager = require('./app/js/RedisManager')
DispatchManager = require('./app/js/DispatchManager')
2014-02-12 10:40:42 +00:00
Errors = require "./app/js/Errors"
HttpController = require "./app/js/HttpController"
mongojs = require "./app/js/mongojs"
async = require "async"
2014-02-12 10:40:42 +00:00
2014-05-08 08:28:13 +00:00
Path = require "path"
2014-05-08 08:28:13 +00:00
Metrics.mongodb.monitor(Path.resolve(__dirname + "/node_modules/mongojs/node_modules/mongodb"), logger)
2016-08-23 08:53:17 +00:00
Metrics.event_loop.monitor(logger, 100)
2014-05-08 08:28:13 +00:00
2014-02-12 10:40:42 +00:00
app = express()
app.configure ->
2014-05-08 08:28:13 +00:00
app.use(Metrics.http.monitor(logger));
2014-02-12 10:40:42 +00:00
app.use express.bodyParser()
app.use app.router
2019-01-04 09:22:09 +00:00
Metrics.injectMetricsRoute(app)
2014-02-12 10:40:42 +00:00
DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10)
2014-02-12 10:40:42 +00:00
2016-09-02 13:03:38 +00: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 14:32:54 +00:00
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
# 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
2017-05-04 14:32:54 +00:00
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.flushAndDeleteDoc
app.delete '/project/:project_id', HttpController.deleteProject
app.post '/project/:project_id', HttpController.updateProject
app.post '/project/:project_id/history/resync', HttpController.resyncProjectHistory
2017-05-04 14:32:54 +00:00
app.post '/project/:project_id/flush', HttpController.flushProject
2017-05-05 14:12:06 +00: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
2017-05-04 14:32:54 +00:00
app.del '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment
2014-02-12 10:40:42 +00:00
2019-05-02 15:30:36 +00:00
app.get '/flush_all_projects', HttpController.flushAllProjects
2014-02-12 10:40:42 +00:00
app.get '/total', (req, res)->
2014-05-08 08:28:13 +00:00
timer = new Metrics.Timer("http.allDocList")
2014-02-12 10:40:42 +00:00
RedisManager.getCountOfDocsInMemory (err, count)->
timer.done()
res.send {total:count}
app.get '/status', (req, res)->
2014-05-07 09:05:07 +00:00
if Settings.shuttingDown
res.send 503 # Service unavailable
else
res.send('document updater is alive')
2014-02-12 10:40:42 +00:00
pubsubClient = require("redis-sharelatex").createClient(Settings.redis.pubsub)
2019-02-07 15:10:40 +00:00
app.get "/health_check/redis", (req, res, next) ->
pubsubClient.healthCheck (error) ->
2019-02-07 15:10:40 +00:00
if error?
logger.err {err: error}, "failed redis health check"
res.send 500
else
res.send 200
2019-02-07 15:53:26 +00:00
docUpdaterRedisClient = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
app.get "/health_check/redis_cluster", (req, res, next) ->
docUpdaterRedisClient.healthCheck (error) ->
if error?
logger.err {err: error}, "failed redis cluster health check"
res.send 500
else
res.send 200
2014-11-20 11:40:52 +00:00
app.get "/health_check", (req, res, next) ->
async.series [
(cb) ->
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?
res.send 500
else
res.send 200
2014-02-12 10:40:42 +00:00
app.use (error, req, res, next) ->
if error instanceof Errors.NotFoundError
res.send 404
else if error instanceof Errors.OpRangeNotAvailableError
res.send 422 # Unprocessable Entity
2014-02-12 10:40:42 +00:00
else
2017-02-14 15:20:05 +00:00
logger.error err: error, req: req, "request errored"
2014-02-12 10:40:42 +00:00
res.send(500, "Oops, something went wrong")
2014-05-07 09:05:07 +00: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
2014-02-12 10:40:42 +00:00
port = Settings.internal?.documentupdater?.port or Settings.apis?.documentupdater?.port or 3003
2015-04-30 14:04:43 +00:00
host = Settings.internal.documentupdater.host or "localhost"
2018-02-15 16:28:40 +00:00
if !module.parent # Called directly
app.listen port, host, ->
logger.info "Document-updater starting up, listening on #{host}:#{port}"
module.exports = app
2014-05-07 09:05:07 +00:00
for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT']
2017-02-14 14:34:28 +00:00
process.on signal, shutdownCleanly(signal)