overleaf/services/web/app/coffee/Features/Compile/CompileManager.coffee

110 lines
4.8 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
Settings = require('settings-sharelatex')
RedisWrapper = require("../../infrastructure/RedisWrapper")
rclient = RedisWrapper.client("clsi_recently_compiled")
ProjectGetter = require('../Project/ProjectGetter')
2014-02-12 05:23:40 -05:00
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
UserGetter = require "../User/UserGetter"
2014-02-12 05:23:40 -05:00
ClsiManager = require "./ClsiManager"
Metrics = require('metrics-sharelatex')
2014-02-12 05:23:40 -05:00
logger = require("logger-sharelatex")
rateLimiter = require("../../infrastructure/RateLimiter")
2014-02-12 05:23:40 -05:00
module.exports = CompileManager =
compile: (project_id, user_id, options = {}, _callback = (error) ->) ->
2014-02-12 05:23:40 -05:00
timer = new Metrics.Timer("editor.compile")
callback = (args...) ->
timer.done()
_callback(args...)
logger.log project_id: project_id, user_id: user_id, "compiling project"
CompileManager._checkIfRecentlyCompiled project_id, user_id, (error, recentlyCompiled) ->
return callback(error) if error?
if recentlyCompiled
logger.warn {project_id, user_id}, "project was recently compiled so not continuing"
return callback null, "too-recently-compiled", []
CompileManager._checkIfAutoCompileLimitHasBeenHit options.isAutoCompile, "everyone", (err, canCompile)->
if !canCompile
return callback null, "autocompile-backoff", []
ProjectRootDocManager.ensureRootDocumentIsSet project_id, (error) ->
2014-02-12 05:23:40 -05:00
return callback(error) if error?
2017-07-28 10:01:05 -04:00
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
2014-02-12 05:23:40 -05:00
return callback(error) if error?
2017-07-28 10:01:05 -04:00
for key, value of limits
options[key] = value
2017-10-09 11:31:01 -04:00
# Put a lower limit on autocompiles for free users, based on compileGroup
CompileManager._checkCompileGroupAutoCompileLimit options.isAutoCompile, limits.compileGroup, (err, canCompile)->
if !canCompile
return callback null, "autocompile-backoff", []
# only pass user_id down to clsi if this is a per-user compile
compileAsUser = if Settings.disablePerUserCompiles then undefined else user_id
ClsiManager.sendRequest project_id, compileAsUser, options, (error, status, outputFiles, clsiServerId, validationProblems) ->
return callback(error) if error?
logger.log files: outputFiles, "output files"
callback(null, status, outputFiles, clsiServerId, limits, validationProblems)
2017-07-28 10:01:05 -04:00
2016-07-14 09:48:46 -04:00
stopCompile: (project_id, user_id, callback = (error) ->) ->
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
return callback(error) if error?
ClsiManager.stopCompile project_id, user_id, limits, callback
deleteAuxFiles: (project_id, user_id, callback = (error) ->) ->
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
return callback(error) if error?
ClsiManager.deleteAuxFiles project_id, user_id, limits, callback
2014-02-12 05:23:40 -05:00
getProjectCompileLimits: (project_id, callback = (error, limits) ->) ->
ProjectGetter.getProject project_id, owner_ref: 1, (error, project) ->
return callback(error) if error?
UserGetter.getUser project.owner_ref, {"features":1}, (err, owner)->
return callback(error) if error?
callback null, {
timeout: owner.features?.compileTimeout || Settings.defaultFeatures.compileTimeout
compileGroup: owner.features?.compileGroup || Settings.defaultFeatures.compileGroup
}
2014-02-12 05:23:40 -05:00
COMPILE_DELAY: 1 # seconds
_checkIfRecentlyCompiled: (project_id, user_id, callback = (error, recentlyCompiled) ->) ->
key = "compile:#{project_id}:#{user_id}"
rclient.set key, true, "EX", @COMPILE_DELAY, "NX", (error, ok) ->
return callback(error) if error?
if ok == "OK"
return callback null, false
else
return callback null, true
2017-10-09 11:31:01 -04:00
_checkCompileGroupAutoCompileLimit: (isAutoCompile, compileGroup, callback = (err, canCompile)->)->
if !isAutoCompile
return callback(null, true)
if compileGroup is "standard"
# apply extra limits to the standard compile group
2017-10-09 11:31:01 -04:00
CompileManager._checkIfAutoCompileLimitHasBeenHit isAutoCompile, compileGroup, callback
else
Metrics.inc "auto-compile-#{compileGroup}"
return callback(null, true) # always allow priority group users to compile
2017-10-09 11:31:01 -04:00
_checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, compileGroup, callback = (err, canCompile)->)->
if !isAutoCompile
return callback(null, true)
2017-10-09 11:31:01 -04:00
Metrics.inc "auto-compile-#{compileGroup}"
opts =
endpointName:"auto_compile"
timeInterval:20
2017-10-09 11:31:01 -04:00
subjectName:compileGroup
throttle: Settings?.rateLimit?.autoCompile?[compileGroup] || 25
rateLimiter.addCount opts, (err, canCompile)->
if err?
canCompile = false
if !canCompile
2017-10-09 11:31:01 -04:00
Metrics.inc "auto-compile-#{compileGroup}-limited"
2014-02-12 05:23:40 -05:00
callback err, canCompile
wordCount: (project_id, user_id, file, callback = (error) ->) ->
2015-09-10 11:41:48 -04:00
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
return callback(error) if error?
ClsiManager.wordCount project_id, user_id, file, limits, callback