2019-01-11 10:19:47 +00:00
|
|
|
Metrics = require "metrics-sharelatex"
|
|
|
|
Metrics.initialize("clsi")
|
|
|
|
|
2014-02-12 17:27:43 +00:00
|
|
|
CompileController = require "./app/js/CompileController"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
logger.initialize("clsi")
|
2016-03-30 13:35:47 +00:00
|
|
|
if Settings.sentry?.dsn?
|
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
|
|
|
|
2014-02-12 17:27:43 +00:00
|
|
|
smokeTest = require "smoke-test-sharelatex"
|
2015-09-21 13:04:08 +00:00
|
|
|
ContentTypeMapper = require "./app/js/ContentTypeMapper"
|
2017-06-23 13:46:40 +00:00
|
|
|
Errors = require './app/js/Errors'
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2014-10-28 12:07:17 +00:00
|
|
|
Path = require "path"
|
2014-12-04 22:07:37 +00:00
|
|
|
fs = require "fs"
|
2014-10-28 12:07:17 +00:00
|
|
|
|
2019-01-11 10:19:47 +00:00
|
|
|
|
2014-05-09 13:55:37 +00:00
|
|
|
Metrics.open_sockets.monitor(logger)
|
2015-08-14 13:47:42 +00:00
|
|
|
Metrics.memory.monitor(logger)
|
2014-05-09 13:55:37 +00:00
|
|
|
|
2014-02-12 17:27:43 +00:00
|
|
|
ProjectPersistenceManager = require "./app/js/ProjectPersistenceManager"
|
2015-02-27 13:16:01 +00:00
|
|
|
OutputCacheManager = require "./app/js/OutputCacheManager"
|
2014-02-12 17:27:43 +00:00
|
|
|
|
|
|
|
require("./app/js/db").sync()
|
|
|
|
|
|
|
|
express = require "express"
|
2014-05-19 11:18:57 +00:00
|
|
|
bodyParser = require "body-parser"
|
2014-02-12 17:27:43 +00:00
|
|
|
app = express()
|
|
|
|
|
2018-11-29 15:49:12 +00:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2014-05-09 13:55:37 +00:00
|
|
|
app.use Metrics.http.monitor(logger)
|
|
|
|
|
2014-05-22 11:18:56 +00:00
|
|
|
# Compile requests can take longer than the default two
|
|
|
|
# minutes (including file download time), so bump up the
|
|
|
|
# timeout a bit.
|
2019-06-06 15:39:16 +00:00
|
|
|
TIMEOUT = 10 * 60 * 1000
|
2014-05-22 11:18:56 +00:00
|
|
|
app.use (req, res, next) ->
|
|
|
|
req.setTimeout TIMEOUT
|
|
|
|
res.setTimeout TIMEOUT
|
2018-07-16 14:38:23 +00:00
|
|
|
res.removeHeader("X-Powered-By")
|
2014-05-22 11:18:56 +00:00
|
|
|
next()
|
|
|
|
|
2016-03-31 11:12:25 +00:00
|
|
|
app.param 'project_id', (req, res, next, project_id) ->
|
|
|
|
if project_id?.match /^[a-zA-Z0-9_-]+$/
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
next new Error("invalid project id")
|
|
|
|
|
2016-05-27 14:29:26 +00:00
|
|
|
app.param 'user_id', (req, res, next, user_id) ->
|
|
|
|
if user_id?.match /^[0-9a-f]{24}$/
|
2016-05-19 15:40:12 +00:00
|
|
|
next()
|
|
|
|
else
|
|
|
|
next new Error("invalid user id")
|
|
|
|
|
2016-05-13 09:10:48 +00:00
|
|
|
app.param 'build_id', (req, res, next, build_id) ->
|
|
|
|
if build_id?.match OutputCacheManager.BUILD_REGEX
|
|
|
|
next()
|
|
|
|
else
|
|
|
|
next new Error("invalid build id #{build_id}")
|
|
|
|
|
|
|
|
|
2019-01-08 12:56:16 +00:00
|
|
|
app.post "/project/:project_id/compile", bodyParser.json(limit: Settings.compileSizeLimit), CompileController.compile
|
2016-07-14 13:47:36 +00:00
|
|
|
app.post "/project/:project_id/compile/stop", CompileController.stopCompile
|
2014-05-19 11:18:57 +00:00
|
|
|
app.delete "/project/:project_id", CompileController.clearCache
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2014-04-08 14:18:56 +00:00
|
|
|
app.get "/project/:project_id/sync/code", CompileController.syncFromCode
|
|
|
|
app.get "/project/:project_id/sync/pdf", CompileController.syncFromPdf
|
2015-06-08 21:35:24 +00:00
|
|
|
app.get "/project/:project_id/wordcount", CompileController.wordcount
|
2016-04-20 14:38:05 +00:00
|
|
|
app.get "/project/:project_id/status", CompileController.status
|
2014-04-08 14:18:56 +00:00
|
|
|
|
2016-05-27 14:29:26 +00:00
|
|
|
# Per-user containers
|
2019-01-08 12:56:16 +00:00
|
|
|
app.post "/project/:project_id/user/:user_id/compile", bodyParser.json(limit: Settings.compileSizeLimit), CompileController.compile
|
2016-07-14 13:47:36 +00:00
|
|
|
app.post "/project/:project_id/user/:user_id/compile/stop", CompileController.stopCompile
|
2016-05-27 14:31:44 +00:00
|
|
|
app.delete "/project/:project_id/user/:user_id", CompileController.clearCache
|
2016-05-27 14:29:26 +00:00
|
|
|
|
|
|
|
app.get "/project/:project_id/user/:user_id/sync/code", CompileController.syncFromCode
|
|
|
|
app.get "/project/:project_id/user/:user_id/sync/pdf", CompileController.syncFromPdf
|
|
|
|
app.get "/project/:project_id/user/:user_id/wordcount", CompileController.wordcount
|
|
|
|
|
2015-02-27 13:57:57 +00:00
|
|
|
ForbidSymlinks = require "./app/js/StaticServerForbidSymlinks"
|
2015-02-25 17:05:19 +00:00
|
|
|
|
2015-02-27 13:57:57 +00:00
|
|
|
# create a static server which does not allow access to any symlinks
|
|
|
|
# avoids possible mismatch of root directory between middleware check
|
|
|
|
# and serving the files
|
|
|
|
staticServer = ForbidSymlinks express.static, Settings.path.compilesDir, setHeaders: (res, path, stat) ->
|
2014-10-28 12:07:17 +00:00
|
|
|
if Path.basename(path) == "output.pdf"
|
2014-12-02 14:30:13 +00:00
|
|
|
# Calculate an etag in the same way as nginx
|
|
|
|
# https://github.com/tj/send/issues/65
|
|
|
|
etag = (path, stat) ->
|
|
|
|
'"' + Math.ceil(+stat.mtime / 1000).toString(16) +
|
|
|
|
'-' + Number(stat.size).toString(16) + '"'
|
|
|
|
res.set("Etag", etag(path, stat))
|
2015-09-21 13:04:08 +00:00
|
|
|
res.set("Content-Type", ContentTypeMapper.map(path))
|
2014-12-02 14:30:13 +00:00
|
|
|
|
2016-05-19 15:40:12 +00:00
|
|
|
app.get "/project/:project_id/user/:user_id/build/:build_id/output/*", (req, res, next) ->
|
|
|
|
# for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url = "/#{req.params.project_id}-#{req.params.user_id}/" + OutputCacheManager.path(req.params.build_id, "/#{req.params[0]}")
|
|
|
|
staticServer(req, res, next)
|
|
|
|
|
2016-05-13 09:10:48 +00:00
|
|
|
app.get "/project/:project_id/build/:build_id/output/*", (req, res, next) ->
|
|
|
|
# for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url = "/#{req.params.project_id}/" + OutputCacheManager.path(req.params.build_id, "/#{req.params[0]}")
|
|
|
|
staticServer(req, res, next)
|
|
|
|
|
2016-06-15 15:12:19 +00:00
|
|
|
app.get "/project/:project_id/user/:user_id/output/*", (req, res, next) ->
|
|
|
|
# for specific user get the path to the top level file
|
|
|
|
req.url = "/#{req.params.project_id}-#{req.params.user_id}/#{req.params[0]}"
|
|
|
|
staticServer(req, res, next)
|
|
|
|
|
2015-02-27 13:57:57 +00:00
|
|
|
app.get "/project/:project_id/output/*", (req, res, next) ->
|
2015-02-27 13:16:01 +00:00
|
|
|
if req.query?.build? && req.query.build.match(OutputCacheManager.BUILD_REGEX)
|
2015-02-27 13:57:57 +00:00
|
|
|
# for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url = "/#{req.params.project_id}/" + OutputCacheManager.path(req.query.build, "/#{req.params[0]}")
|
2015-02-25 17:05:19 +00:00
|
|
|
else
|
|
|
|
req.url = "/#{req.params.project_id}/#{req.params[0]}"
|
2014-12-04 23:54:22 +00:00
|
|
|
staticServer(req, res, next)
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2016-03-30 13:35:47 +00:00
|
|
|
app.get "/oops", (req, res, next) ->
|
|
|
|
logger.error {err: "hello"}, "test error"
|
|
|
|
res.send "error\n"
|
|
|
|
|
|
|
|
|
2014-02-12 17:27:43 +00:00
|
|
|
app.get "/status", (req, res, next) ->
|
|
|
|
res.send "CLSI is alive\n"
|
|
|
|
|
2014-06-05 14:51:24 +00:00
|
|
|
resCacher =
|
|
|
|
contentType:(@setContentType)->
|
|
|
|
send:(@code, @body)->
|
|
|
|
|
2014-06-05 15:13:06 +00:00
|
|
|
#default the server to be down
|
|
|
|
code:500
|
|
|
|
body:{}
|
|
|
|
setContentType:"application/json"
|
|
|
|
|
2014-08-19 11:11:56 +00:00
|
|
|
if Settings.smokeTest
|
|
|
|
do runSmokeTest = ->
|
|
|
|
logger.log("running smoke tests")
|
|
|
|
smokeTest.run(require.resolve(__dirname + "/test/smoke/js/SmokeTests.js"))({}, resCacher)
|
2017-11-29 11:01:51 +00:00
|
|
|
setTimeout(runSmokeTest, 30 * 1000)
|
2014-06-05 14:51:24 +00:00
|
|
|
|
|
|
|
app.get "/health_check", (req, res)->
|
2014-06-05 15:13:06 +00:00
|
|
|
res.contentType(resCacher?.setContentType)
|
2015-05-12 10:40:29 +00:00
|
|
|
res.status(resCacher?.code).send(resCacher?.body)
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2017-12-29 08:08:19 +00:00
|
|
|
app.get "/smoke_test_force", (req, res)->
|
|
|
|
smokeTest.run(require.resolve(__dirname + "/test/smoke/js/SmokeTests.js"))(req, res)
|
|
|
|
|
2019-01-16 15:11:49 +00:00
|
|
|
profiler = require "v8-profiler-node8"
|
2015-03-16 15:02:45 +00:00
|
|
|
app.get "/profile", (req, res) ->
|
|
|
|
time = parseInt(req.query.time || "1000")
|
|
|
|
profiler.startProfiling("test")
|
|
|
|
setTimeout () ->
|
|
|
|
profile = profiler.stopProfiling("test")
|
|
|
|
res.json(profile)
|
|
|
|
, time
|
|
|
|
|
2015-04-09 13:40:02 +00:00
|
|
|
app.get "/heapdump", (req, res)->
|
|
|
|
require('heapdump').writeSnapshot '/tmp/' + Date.now() + '.clsi.heapsnapshot', (err, filename)->
|
|
|
|
res.send filename
|
|
|
|
|
2014-02-12 17:27:43 +00:00
|
|
|
app.use (error, req, res, next) ->
|
2017-06-23 13:46:40 +00:00
|
|
|
if error instanceof Errors.NotFoundError
|
|
|
|
logger.warn {err: error, url: req.url}, "not found error"
|
|
|
|
return res.sendStatus(404)
|
|
|
|
else
|
|
|
|
logger.error {err: error, url: req.url}, "server error"
|
|
|
|
res.sendStatus(error?.statusCode || 500)
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2018-03-29 11:12:29 +00:00
|
|
|
net = require "net"
|
|
|
|
os = require "os"
|
|
|
|
|
|
|
|
STATE = "up"
|
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
|
|
|
|
loadTcpServer = net.createServer (socket) ->
|
2018-03-29 11:12:29 +00:00
|
|
|
socket.on "error", (err)->
|
|
|
|
if err.code == "ECONNRESET"
|
|
|
|
# this always comes up, we don't know why
|
|
|
|
return
|
|
|
|
logger.err err:err, "error with socket on load check"
|
|
|
|
socket.destroy()
|
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
if STATE == "up" and Settings.internal.load_balancer_agent.report_load
|
2018-03-29 11:12:29 +00:00
|
|
|
currentLoad = os.loadavg()[0]
|
|
|
|
|
|
|
|
# staging clis's have 1 cpu core only
|
|
|
|
if os.cpus().length == 1
|
|
|
|
availableWorkingCpus = 1
|
|
|
|
else
|
|
|
|
availableWorkingCpus = os.cpus().length - 1
|
|
|
|
|
|
|
|
freeLoad = availableWorkingCpus - currentLoad
|
|
|
|
freeLoadPercentage = Math.round((freeLoad / availableWorkingCpus) * 100)
|
|
|
|
if freeLoadPercentage <= 0
|
|
|
|
freeLoadPercentage = 1 # when its 0 the server is set to drain and will move projects to different servers
|
|
|
|
socket.write("up, #{freeLoadPercentage}%\n", "ASCII")
|
|
|
|
socket.end()
|
|
|
|
else
|
|
|
|
socket.write("#{STATE}\n", "ASCII")
|
|
|
|
socket.end()
|
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
loadHttpServer = express()
|
2018-03-29 11:12:29 +00:00
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
loadHttpServer.post "/state/up", (req, res, next) ->
|
|
|
|
STATE = "up"
|
|
|
|
logger.info "getting message to set server to down"
|
|
|
|
res.sendStatus 204
|
2018-03-29 11:12:29 +00:00
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
loadHttpServer.post "/state/down", (req, res, next) ->
|
|
|
|
STATE = "down"
|
|
|
|
logger.info "getting message to set server to down"
|
|
|
|
res.sendStatus 204
|
2018-03-29 11:12:29 +00:00
|
|
|
|
2018-07-05 14:07:07 +00:00
|
|
|
loadHttpServer.post "/state/maint", (req, res, next) ->
|
|
|
|
STATE = "maint"
|
|
|
|
logger.info "getting message to set server to maint"
|
|
|
|
res.sendStatus 204
|
|
|
|
|
|
|
|
|
2018-02-16 11:36:32 +00:00
|
|
|
port = (Settings.internal?.clsi?.port or 3013)
|
|
|
|
host = (Settings.internal?.clsi?.host or "localhost")
|
2018-03-29 11:12:29 +00:00
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
load_tcp_port = Settings.internal.load_balancer_agent.load_port
|
|
|
|
load_http_port = Settings.internal.load_balancer_agent.local_port
|
2018-02-16 11:36:32 +00:00
|
|
|
|
|
|
|
if !module.parent # Called directly
|
|
|
|
app.listen port, host, (error) ->
|
|
|
|
logger.info "CLSI starting up, listening on #{host}:#{port}"
|
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
loadTcpServer.listen load_tcp_port, host, (error) ->
|
2018-03-29 11:12:29 +00:00
|
|
|
throw error if error?
|
2018-06-28 15:04:34 +00:00
|
|
|
logger.info "Load tcp agent listening on load port #{load_tcp_port}"
|
2018-02-16 11:36:32 +00:00
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
loadHttpServer.listen load_http_port, host, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
logger.info "Load http agent listening on load port #{load_http_port}"
|
2018-02-16 11:36:32 +00:00
|
|
|
|
2018-06-28 15:04:34 +00:00
|
|
|
module.exports = app
|
2014-02-12 17:27:43 +00:00
|
|
|
|
|
|
|
setInterval () ->
|
|
|
|
ProjectPersistenceManager.clearExpiredProjects()
|
|
|
|
, tenMinutes = 10 * 60 * 1000
|
2016-04-08 12:31:23 +00:00
|
|
|
|