2014-02-12 05:23:40 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
2014-09-26 09:52:00 -04:00
|
|
|
|
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
|
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
|
2014-11-28 09:26:21 -05:00
|
|
|
UserGetter = require "../User/UserGetter"
|
2014-02-12 05:23:40 -05:00
|
|
|
ClsiManager = require "./ClsiManager"
|
|
|
|
Metrics = require('../../infrastructure/Metrics')
|
|
|
|
logger = require("logger-sharelatex")
|
2014-02-28 12:59:54 -05:00
|
|
|
rateLimiter = require("../../infrastructure/RateLimiter")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = CompileManager =
|
2014-11-28 09:26:21 -05:00
|
|
|
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...)
|
|
|
|
|
2014-11-28 09:26:21 -05:00
|
|
|
@_checkIfAutoCompileLimitHasBeenHit options.isAutoCompile, (err, canCompile)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if !canCompile
|
2014-05-19 10:28:35 -04:00
|
|
|
return callback null, "autocompile-backoff", []
|
2014-02-12 05:23:40 -05:00
|
|
|
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
|
|
|
|
return callback new Error("project was recently compiled so not continuing")
|
2014-06-01 13:26:33 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
CompileManager._ensureRootDocumentIsSet project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
2014-11-28 09:26:21 -05:00
|
|
|
CompileManager.getProjectCompileLimits project_id, (error, limits) ->
|
2014-05-21 10:20:14 -04:00
|
|
|
return callback(error) if error?
|
2014-11-28 09:26:21 -05:00
|
|
|
for key, value of limits
|
|
|
|
options[key] = value
|
|
|
|
ClsiManager.sendRequest project_id, options, (error, status, outputFiles, output) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
logger.log files: outputFiles, "output files"
|
|
|
|
callback(null, status, outputFiles, output)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-11-28 09:26:21 -05:00
|
|
|
getProjectCompileLimits: (project_id, callback = (error, limits) ->) ->
|
|
|
|
Project.findById 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-05-19 11:10:41 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
getLogLines: (project_id, callback)->
|
|
|
|
Metrics.inc "editor.raw-logs"
|
|
|
|
ClsiManager.getLogLines project_id, (error, logLines)->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, logLines
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
_checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)->
|
|
|
|
if !isAutoCompile
|
|
|
|
return callback(null, true)
|
2014-02-28 12:59:54 -05:00
|
|
|
opts =
|
|
|
|
endpointName:"auto_compile"
|
2014-03-04 10:29:45 -05:00
|
|
|
timeInterval:15
|
2014-02-28 12:59:54 -05:00
|
|
|
subjectName:"everyone"
|
2014-10-14 06:50:06 -04:00
|
|
|
throttle: 15
|
2014-02-28 12:59:54 -05:00
|
|
|
rateLimiter.addCount opts, (err, canCompile)->
|
|
|
|
if err?
|
2014-02-12 05:23:40 -05:00
|
|
|
canCompile = false
|
2014-02-28 12:59:54 -05:00
|
|
|
logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit"
|
2014-02-12 05:23:40 -05:00
|
|
|
callback err, canCompile
|
|
|
|
|
|
|
|
_ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
|
|
|
|
Project.findById project_id, 'rootDoc_id', (error, project)=>
|
|
|
|
return callback(error) if error?
|
|
|
|
if !project?
|
|
|
|
return callback new Error("project not found")
|
|
|
|
|
|
|
|
if project.rootDoc_id?
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically project_id, callback
|
|
|
|
|