2014-02-12 05:23:40 -05:00
|
|
|
Path = require "path"
|
|
|
|
async = require "async"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
request = require('request')
|
|
|
|
Project = require("../../models/Project").Project
|
2017-08-03 05:22:11 -04:00
|
|
|
ProjectGetter = require("../Project/ProjectGetter")
|
2014-05-06 07:54:26 -04:00
|
|
|
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require "logger-sharelatex"
|
2016-05-26 11:24:18 -04:00
|
|
|
Url = require("url")
|
2016-04-20 10:06:39 -04:00
|
|
|
ClsiCookieManager = require("./ClsiCookieManager")
|
2017-08-01 09:39:57 -04:00
|
|
|
ClsiStateManager = require("./ClsiStateManager")
|
2016-06-01 07:28:54 -04:00
|
|
|
_ = require("underscore")
|
|
|
|
async = require("async")
|
2016-06-02 08:09:11 -04:00
|
|
|
ClsiFormatChecker = require("./ClsiFormatChecker")
|
2017-07-28 10:01:05 -04:00
|
|
|
DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
|
2017-08-09 06:34:16 -04:00
|
|
|
Metrics = require('metrics-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ClsiManager =
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2017-08-01 09:39:57 -04:00
|
|
|
sendRequest: (project_id, user_id, options = {}, callback) ->
|
2017-08-08 11:48:37 -04:00
|
|
|
ClsiManager.sendRequestOnce project_id, user_id, options, (error, status, result...) ->
|
2017-08-01 09:39:57 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if status is 'conflict'
|
2017-08-08 11:48:37 -04:00
|
|
|
options = _.clone(options)
|
2017-08-03 06:44:10 -04:00
|
|
|
options.syncType = "full" # force full compile
|
2017-08-01 09:39:57 -04:00
|
|
|
ClsiManager.sendRequestOnce project_id, user_id, options, callback # try again
|
|
|
|
else
|
|
|
|
callback(error, status, result...)
|
|
|
|
|
|
|
|
sendRequestOnce: (project_id, user_id, options = {}, callback = (error, status, outputFiles, clsiServerId, validationProblems) ->) ->
|
2017-08-02 11:25:47 -04:00
|
|
|
ClsiManager._buildRequest project_id, options, (error, req) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2014-05-06 07:54:26 -04:00
|
|
|
logger.log project_id: project_id, "sending compile to CLSI"
|
2016-06-02 08:09:11 -04:00
|
|
|
ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)->
|
2016-06-01 11:46:41 -04:00
|
|
|
if err?
|
|
|
|
logger.err err, project_id, "could not check resources for potential problems before sending to clsi"
|
|
|
|
return callback(err)
|
2016-06-02 08:09:11 -04:00
|
|
|
if validationProblems?
|
|
|
|
logger.log project_id:project_id, validationProblems:validationProblems, "problems with users latex before compile was attempted"
|
|
|
|
return callback(null, "validation-problems", null, null, validationProblems)
|
2016-05-31 11:20:24 -04:00
|
|
|
ClsiManager._postToClsi project_id, user_id, req, options.compileGroup, (error, response) ->
|
2016-04-20 10:06:39 -04:00
|
|
|
if error?
|
|
|
|
logger.err err:error, project_id:project_id, "error sending request to clsi"
|
|
|
|
return callback(error)
|
2017-08-03 05:56:16 -04:00
|
|
|
logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, compile_status: response?.compile?.status, "received compile response from CLSI"
|
2016-05-18 05:09:22 -04:00
|
|
|
ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting server id"
|
|
|
|
return callback(err)
|
2016-05-21 05:23:17 -04:00
|
|
|
outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
|
2016-05-18 07:50:50 -04:00
|
|
|
callback(null, response?.compile?.status, outputFiles, clsiServerId)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-07-14 09:48:46 -04:00
|
|
|
stopCompile: (project_id, user_id, options, callback = (error) ->) ->
|
|
|
|
compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id, "compile/stop")
|
|
|
|
opts =
|
|
|
|
url:compilerUrl
|
|
|
|
method:"POST"
|
|
|
|
ClsiManager._makeRequest project_id, opts, callback
|
|
|
|
|
2016-05-31 11:20:24 -04:00
|
|
|
deleteAuxFiles: (project_id, user_id, options, callback = (error) ->) ->
|
|
|
|
compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id)
|
2016-04-19 11:48:51 -04:00
|
|
|
opts =
|
2016-05-31 11:20:24 -04:00
|
|
|
url:compilerUrl
|
2016-04-19 11:48:51 -04:00
|
|
|
method:"DELETE"
|
|
|
|
ClsiManager._makeRequest project_id, opts, callback
|
2014-05-19 11:10:41 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
_makeRequest: (project_id, opts, callback)->
|
|
|
|
ClsiCookieManager.getCookieJar project_id, (err, jar)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error getting cookie jar for clsi request"
|
|
|
|
return callback(err)
|
|
|
|
opts.jar = jar
|
2016-04-20 11:17:06 -04:00
|
|
|
request opts, (err, response, body)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
|
|
|
|
return callback(err)
|
2016-04-20 12:00:17 -04:00
|
|
|
ClsiCookieManager.setServerId project_id, response, (err)->
|
|
|
|
if err?
|
|
|
|
logger.warn err:err, project_id:project_id, "error setting server id"
|
|
|
|
|
|
|
|
return callback err, response, body
|
2016-04-20 10:06:39 -04:00
|
|
|
|
2016-05-31 11:20:24 -04:00
|
|
|
_getCompilerUrl: (compileGroup, project_id, user_id, action) ->
|
|
|
|
host = Settings.apis.clsi.url
|
|
|
|
path = "/project/#{project_id}"
|
|
|
|
path += "/user/#{user_id}" if user_id?
|
|
|
|
path += "/#{action}" if action?
|
|
|
|
return "#{host}#{path}"
|
2016-04-20 10:06:39 -04:00
|
|
|
|
2016-05-31 11:20:24 -04:00
|
|
|
_postToClsi: (project_id, user_id, req, compileGroup, callback = (error, response) ->) ->
|
|
|
|
compileUrl = @_getCompilerUrl(compileGroup, project_id, user_id, "compile")
|
|
|
|
opts =
|
|
|
|
url: compileUrl
|
2014-02-12 05:23:40 -05:00
|
|
|
json: req
|
2016-04-19 11:48:51 -04:00
|
|
|
method: "POST"
|
|
|
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
2014-05-19 08:46:22 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
callback null, body
|
2014-11-27 10:42:37 -05:00
|
|
|
else if response.statusCode == 413
|
2014-11-27 11:22:39 -05:00
|
|
|
callback null, compile:status:"project-too-large"
|
2017-08-01 09:39:57 -04:00
|
|
|
else if response.statusCode == 409
|
|
|
|
callback null, compile:status:"conflict"
|
2014-05-19 08:46:22 -04:00
|
|
|
else
|
|
|
|
error = new Error("CLSI returned non-success code: #{response.statusCode}")
|
|
|
|
logger.error err: error, project_id: project_id, "CLSI returned failure code"
|
|
|
|
callback error, body
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-05-21 05:23:17 -04:00
|
|
|
_parseOutputFiles: (project_id, rawOutputFiles = []) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
outputFiles = []
|
|
|
|
for file in rawOutputFiles
|
|
|
|
outputFiles.push
|
2016-05-26 11:26:58 -04:00
|
|
|
path: file.path # the clsi is now sending this to web
|
2016-06-02 10:41:33 -04:00
|
|
|
url: Url.parse(file.url).path # the location of the file on the clsi, excluding the host part
|
2014-02-12 05:23:40 -05:00
|
|
|
type: file.type
|
2015-02-25 12:06:27 -05:00
|
|
|
build: file.build
|
2014-02-12 05:23:40 -05:00
|
|
|
return outputFiles
|
|
|
|
|
|
|
|
VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
|
2017-08-01 09:39:57 -04:00
|
|
|
|
2017-08-02 11:25:47 -04:00
|
|
|
_buildRequest: (project_id, options={}, callback = (error, request) ->) ->
|
2017-08-03 05:22:11 -04:00
|
|
|
ProjectGetter.getProject project_id, {compiler: 1, rootDoc_id: 1, imageName: 1, rootFolder:1}, (error, project) ->
|
2014-05-06 07:54:26 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
|
|
|
|
if project.compiler not in ClsiManager.VALID_COMPILERS
|
|
|
|
project.compiler = "pdflatex"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-08-09 11:25:57 -04:00
|
|
|
if options.incrementalCompilesEnabled or options.syncType? # new way, either incremental or full
|
2017-08-09 06:34:16 -04:00
|
|
|
timer = new Metrics.Timer("editor.compile-getdocs-redis")
|
2017-08-08 11:48:47 -04:00
|
|
|
ClsiManager.getContentFromDocUpdaterIfMatch project_id, project, (error, projectStateHash, docUpdaterDocs) ->
|
2017-08-09 06:34:16 -04:00
|
|
|
timer.done()
|
2017-08-08 11:48:47 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
logger.log project_id: project_id, projectStateHash: projectStateHash, docs: docUpdaterDocs?, "checked project state"
|
|
|
|
# see if we can send an incremental update to the CLSI
|
|
|
|
if docUpdaterDocs? and options.syncType isnt "full"
|
2017-08-09 06:34:16 -04:00
|
|
|
Metrics.inc "compile-from-redis"
|
2017-08-08 11:48:47 -04:00
|
|
|
ClsiManager._buildRequestFromDocupdater project_id, options, project, projectStateHash, docUpdaterDocs, callback
|
|
|
|
else
|
2017-08-09 06:34:16 -04:00
|
|
|
Metrics.inc "compile-from-mongo"
|
2017-08-08 11:48:47 -04:00
|
|
|
ClsiManager._buildRequestFromMongo project_id, options, project, projectStateHash, callback
|
|
|
|
else # old way, always from mongo
|
2017-08-09 06:34:16 -04:00
|
|
|
timer = new Metrics.Timer("editor.compile-getdocs-mongo")
|
2017-08-08 11:48:47 -04:00
|
|
|
ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
|
2017-08-09 06:34:16 -04:00
|
|
|
timer.done()
|
2017-08-08 11:48:47 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
|
2017-08-07 09:45:04 -04:00
|
|
|
|
|
|
|
getContentFromDocUpdaterIfMatch: (project_id, project, callback = (error, projectStateHash, docs) ->) ->
|
|
|
|
ClsiStateManager.computeHash project, (error, projectStateHash) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
DocumentUpdaterHandler.getProjectDocsIfMatch project_id, projectStateHash, (error, docs) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, projectStateHash, docs)
|
|
|
|
|
|
|
|
_buildRequestFromDocupdater: (project_id, options, project, projectStateHash, docUpdaterDocs, callback = (error, request) ->) ->
|
|
|
|
ProjectEntityHandler.getAllDocPathsFromProject project, (error, docPath) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
docs = {}
|
|
|
|
for doc in docUpdaterDocs or []
|
|
|
|
path = docPath[doc._id]
|
|
|
|
docs[path] = doc
|
|
|
|
# send new docs but not files as those are already on the clsi
|
2017-08-08 11:48:37 -04:00
|
|
|
options = _.clone(options)
|
2017-08-07 09:45:04 -04:00
|
|
|
options.syncType = "incremental"
|
|
|
|
options.syncState = projectStateHash
|
|
|
|
ClsiManager._finaliseRequest project_id, options, project, docs, [], callback
|
|
|
|
|
|
|
|
_buildRequestFromMongo: (project_id, options, project, projectStateHash, callback = (error, request) ->) ->
|
|
|
|
ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
|
|
|
|
return callback(error) if error?
|
2017-08-08 11:48:37 -04:00
|
|
|
options = _.clone(options)
|
2017-08-07 09:45:04 -04:00
|
|
|
options.syncType = "full"
|
|
|
|
options.syncState = projectStateHash
|
|
|
|
ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
|
2017-08-01 09:39:57 -04:00
|
|
|
|
|
|
|
_getContentFromDocUpdater: (project_id, callback = (error, docs) ->) ->
|
2017-08-09 10:47:44 -04:00
|
|
|
# Workaround: for now, always flush project to mongo on compile
|
|
|
|
# until we have automatic periodic flushing on the docupdater
|
|
|
|
# side, to prevent documents staying in redis too long.
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
|
2017-08-01 09:39:57 -04:00
|
|
|
return callback(error) if error?
|
2017-08-09 10:47:44 -04:00
|
|
|
DocumentUpdaterHandler.getProjectDocs project_id, (error, docs) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, docs)
|
2017-07-28 10:17:10 -04:00
|
|
|
|
|
|
|
_getContentFromMongo: (project_id, callback = (error, docs, files) ->) ->
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
|
2017-08-01 09:39:57 -04:00
|
|
|
return callback(error) if error?
|
2017-07-28 10:17:10 -04:00
|
|
|
callback(null, docs, files)
|
|
|
|
|
|
|
|
_finaliseRequest: (project_id, options, project, docs, files, callback = (error, params) -> ) ->
|
|
|
|
resources = []
|
|
|
|
rootResourcePath = null
|
|
|
|
rootResourcePathOverride = null
|
|
|
|
|
|
|
|
for path, doc of docs
|
|
|
|
path = path.replace(/^\//, "") # Remove leading /
|
|
|
|
resources.push
|
|
|
|
path: path
|
|
|
|
content: doc.lines.join("\n")
|
|
|
|
if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
|
|
|
|
rootResourcePath = path
|
|
|
|
if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
|
|
|
|
rootResourcePathOverride = path
|
|
|
|
|
|
|
|
rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
|
|
|
|
if !rootResourcePath?
|
|
|
|
logger.warn {project_id}, "no root document found, setting to main.tex"
|
|
|
|
rootResourcePath = "main.tex"
|
|
|
|
|
|
|
|
for path, file of files
|
|
|
|
path = path.replace(/^\//, "") # Remove leading /
|
|
|
|
resources.push
|
|
|
|
path: path
|
|
|
|
url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
|
|
|
|
modified: file.created?.getTime()
|
|
|
|
|
|
|
|
callback null, {
|
|
|
|
compile:
|
|
|
|
options:
|
|
|
|
compiler: project.compiler
|
|
|
|
timeout: options.timeout
|
|
|
|
imageName: project.imageName
|
|
|
|
draft: !!options.draft
|
|
|
|
check: options.check
|
2017-08-03 06:44:10 -04:00
|
|
|
syncType: options.syncType
|
|
|
|
syncState: options.syncState
|
2017-07-28 10:17:10 -04:00
|
|
|
rootResourcePath: rootResourcePath
|
|
|
|
resources: resources
|
|
|
|
}
|
2015-09-10 11:41:48 -04:00
|
|
|
|
2016-05-31 11:20:24 -04:00
|
|
|
wordCount: (project_id, user_id, file, options, callback = (error, response) ->) ->
|
2015-09-11 08:53:06 -04:00
|
|
|
ClsiManager._buildRequest project_id, options, (error, req) ->
|
2015-10-19 17:14:52 -04:00
|
|
|
filename = file || req?.compile?.rootResourcePath
|
2016-05-31 11:20:24 -04:00
|
|
|
wordcount_url = ClsiManager._getCompilerUrl(options?.compileGroup, project_id, user_id, "wordcount")
|
2016-04-19 11:48:51 -04:00
|
|
|
opts =
|
2016-01-19 09:17:01 -05:00
|
|
|
url: wordcount_url
|
2016-05-31 11:20:24 -04:00
|
|
|
qs:
|
|
|
|
file: filename
|
|
|
|
image: req.compile.options.imageName
|
2016-04-19 11:48:51 -04:00
|
|
|
method: "GET"
|
|
|
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
2015-09-11 08:53:06 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
callback null, body
|
|
|
|
else
|
|
|
|
error = new Error("CLSI returned non-success code: #{response.statusCode}")
|
|
|
|
logger.error err: error, project_id: project_id, "CLSI returned failure code"
|
|
|
|
callback error, body
|
|
|
|
|