2014-02-12 10:40:42 +00:00
|
|
|
express = require('express')
|
|
|
|
http = require("http")
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
logger.initialize("documentupdater")
|
2014-08-07 10:45:19 +00:00
|
|
|
RedisManager = require('./app/js/RedisManager')
|
2014-08-11 13:16:05 +00:00
|
|
|
DispatchManager = require('./app/js/DispatchManager')
|
2014-02-12 10:40:42 +00:00
|
|
|
Keys = require('./app/js/RedisKeyBuilder')
|
|
|
|
Errors = require "./app/js/Errors"
|
|
|
|
HttpController = require "./app/js/HttpController"
|
2015-08-31 14:57:26 +00:00
|
|
|
MongoHealthCheck = require('./app/js/MongoHealthCheck')
|
2014-02-12 10:40:42 +00:00
|
|
|
|
2014-10-07 11:08:36 +00:00
|
|
|
redis = require("redis-sharelatex")
|
2014-11-19 16:54:02 +00:00
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
2014-10-07 11:08:36 +00:00
|
|
|
|
2014-02-13 10:36:26 +00:00
|
|
|
|
2014-05-08 08:28:13 +00:00
|
|
|
Path = require "path"
|
|
|
|
Metrics = require "metrics-sharelatex"
|
|
|
|
Metrics.initialize("doc-updater")
|
|
|
|
Metrics.mongodb.monitor(Path.resolve(__dirname + "/node_modules/mongojs/node_modules/mongodb"), logger)
|
|
|
|
|
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
|
|
|
|
|
2014-08-11 13:16:05 +00:00
|
|
|
DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10)
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
|
|
|
|
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/flush', HttpController.flushProject
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-31 14:57:26 +00:00
|
|
|
app.get '/health_check/mongo', (req, res, next) ->
|
|
|
|
MongoHealthCheck.isAlive (error) ->
|
|
|
|
if error?
|
|
|
|
res.send 500, error.message
|
|
|
|
else
|
|
|
|
res.send 200
|
2014-11-20 11:40:52 +00:00
|
|
|
|
|
|
|
redisCheck = require("redis-sharelatex").activeHealthCheckRedis(Settings.redis.web)
|
|
|
|
app.get "/health_check/redis", (req, res, next)->
|
|
|
|
if redisCheck.isAlive()
|
|
|
|
res.send 200
|
|
|
|
else
|
|
|
|
res.send 500
|
|
|
|
|
|
|
|
|
2014-02-12 10:40:42 +00:00
|
|
|
app.use (error, req, res, next) ->
|
|
|
|
if error instanceof Errors.NotFoundError
|
|
|
|
res.send 404
|
2016-05-31 12:24:19 +00:00
|
|
|
else if error instanceof Errors.OpRangeNotAvailableError
|
|
|
|
res.send 422 # Unprocessable Entity
|
2014-02-12 10:40:42 +00:00
|
|
|
else
|
2016-05-31 12:24:19 +00:00
|
|
|
logger.error err: error, "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"
|
|
|
|
app.listen port, host, ->
|
|
|
|
logger.info "Document-updater starting up, listening on #{host}:#{port}"
|
2014-05-07 09:05:07 +00:00
|
|
|
|
|
|
|
for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT']
|
|
|
|
process.on signal, shutdownCleanly(signal)
|