app.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. express = require('express')
  2. http = require("http")
  3. Settings = require('settings-sharelatex')
  4. logger = require('logger-sharelatex')
  5. logger.initialize("documentupdater")
  6. RedisManager = require('./app/js/RedisManager')
  7. DispatchManager = require('./app/js/DispatchManager')
  8. Keys = require('./app/js/RedisKeyBuilder')
  9. Errors = require "./app/js/Errors"
  10. HttpController = require "./app/js/HttpController"
  11. redis = require("redis-sharelatex")
  12. rclient = redis.createClient(Settings.redis.web)
  13. Path = require "path"
  14. Metrics = require "metrics-sharelatex"
  15. Metrics.initialize("doc-updater")
  16. Metrics.mongodb.monitor(Path.resolve(__dirname + "/node_modules/mongojs/node_modules/mongodb"), logger)
  17. Metrics.event_loop.monitor(logger, 100)
  18. app = express()
  19. app.configure ->
  20. app.use(Metrics.http.monitor(logger));
  21. app.use express.bodyParser()
  22. app.use app.router
  23. DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10)
  24. app.param 'project_id', (req, res, next, project_id) ->
  25. if project_id?.match /^[0-9a-f]{24}$/
  26. next()
  27. else
  28. next new Error("invalid project id")
  29. app.param 'doc_id', (req, res, next, doc_id) ->
  30. if doc_id?.match /^[0-9a-f]{24}$/
  31. next()
  32. else
  33. next new Error("invalid doc id")
  34. app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
  35. app.post '/project/:project_id/doc/:doc_id', HttpController.setDoc
  36. app.post '/project/:project_id/doc/:doc_id/flush', HttpController.flushDocIfLoaded
  37. app.delete '/project/:project_id/doc/:doc_id', HttpController.flushAndDeleteDoc
  38. app.delete '/project/:project_id', HttpController.deleteProject
  39. app.post '/project/:project_id/flush', HttpController.flushProject
  40. app.get '/total', (req, res)->
  41. timer = new Metrics.Timer("http.allDocList")
  42. RedisManager.getCountOfDocsInMemory (err, count)->
  43. timer.done()
  44. res.send {total:count}
  45. app.get '/status', (req, res)->
  46. if Settings.shuttingDown
  47. res.send 503 # Service unavailable
  48. else
  49. res.send('document updater is alive')
  50. redisCheck = require("redis-sharelatex").activeHealthCheckRedis(Settings.redis.web)
  51. app.get "/health_check/redis", (req, res, next)->
  52. if redisCheck.isAlive()
  53. res.send 200
  54. else
  55. res.send 500
  56. app.get "/health_check/redis_cluster", (req, res, next) ->
  57. RedisManager.rclient.healthCheck (error, alive) ->
  58. if error?
  59. logger.err {err: error}, "failed redis cluster health check"
  60. res.send 500
  61. else
  62. res.send 200
  63. app.use (error, req, res, next) ->
  64. if error instanceof Errors.NotFoundError
  65. res.send 404
  66. else if error instanceof Errors.OpRangeNotAvailableError
  67. res.send 422 # Unprocessable Entity
  68. else
  69. logger.error err: error, "request errored"
  70. res.send(500, "Oops, something went wrong")
  71. shutdownCleanly = (signal) ->
  72. return () ->
  73. logger.log signal: signal, "received interrupt, cleaning up"
  74. Settings.shuttingDown = true
  75. setTimeout () ->
  76. logger.log signal: signal, "shutting down"
  77. process.exit()
  78. , 10000
  79. port = Settings.internal?.documentupdater?.port or Settings.apis?.documentupdater?.port or 3003
  80. host = Settings.internal.documentupdater.host or "localhost"
  81. app.listen port, host, ->
  82. logger.info "Document-updater starting up, listening on #{host}:#{port}"
  83. for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT']
  84. process.on signal, shutdownCleanly(signal)