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

236 lines
9.7 KiB
CoffeeScript
Raw Normal View History

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
2014-05-06 07:54:26 -04:00
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
2014-02-12 05:23:40 -05:00
logger = require "logger-sharelatex"
Url = require("url")
ClsiCookieManager = require("./ClsiCookieManager")
2017-08-01 09:39:57 -04:00
ClsiStateManager = require("./ClsiStateManager")
_ = 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"
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) ->
ClsiManager.sendRequestOnce project_id, user_id, _.clone(options), (error, status, result...) ->
return callback(error) if error?
if status is 'conflict'
options.state = "conflict" # will force full compile
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) ->) ->
ClsiManager._buildRequest project_id, user_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"
2017-08-01 09:39:57 -04:00
console.log "REQUEST", JSON.stringify(req, null, 2)
2016-06-02 08:09:11 -04:00
ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)->
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)
ClsiManager._postToClsi project_id, user_id, req, options.compileGroup, (error, response) ->
if error?
logger.err err:error, project_id:project_id, "error sending request to clsi"
return callback(error)
2017-08-01 09:39:57 -04:00
if response?.compile?.status is "conflict"
# FIXME try again without incremental option
console.log "CONFLICT TRY AGAIN"
logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, "received compile response from CLSI"
ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
if err?
logger.err err:err, project_id:project_id, "error getting server id"
return callback(err)
outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
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
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 =
url:compilerUrl
2016-04-19 11:48:51 -04:00
method:"DELETE"
ClsiManager._makeRequest project_id, opts, callback
_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
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)
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
_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}"
_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) ->
return callback(error) if error?
if 200 <= response.statusCode < 300
callback null, body
else if response.statusCode == 413
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"
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
_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
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
_buildRequest: (project_id, user_id, options={}, callback = (error, request) ->) ->
Project.findById project_id, {}, (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?
2017-08-01 09:39:57 -04:00
console.log "PROJECT", project, JSON.stringify(project.rootFolder,null,2)
2014-05-06 07:54:26 -04:00
if project.compiler not in ClsiManager.VALID_COMPILERS
project.compiler = "pdflatex"
2014-02-12 05:23:40 -05:00
2017-08-01 09:39:57 -04:00
ClsiStateManager.checkState project_id, user_id, project, (error, stateOk, state) ->
2014-05-06 07:54:26 -04:00
return callback(error) if error?
2017-08-01 09:39:57 -04:00
logger.log project_id: project_id, checkState: stateOk, "checked project state"
console.log "OPTIONS ARE", options
if stateOk and not options.state? # incremental
ClsiManager._getContentFromDocUpdater project_id, (error, docUpdaterDocs) ->
return callback(error) if error?
# make this incremental
ProjectEntityHandler.getAllDocPathsFromProject project, (error, docPath) ->
return callback(error) if error?
console.log "PATHS", docPath
console.log "DOCS", docUpdaterDocs
docs = {}
for doc in docUpdaterDocs or []
path = docPath[doc._id]
docs[path] = doc
console.log "MAPPED DOCS", docs
options.incremental = state
ClsiManager._finaliseRequest project_id, options, project, docs, [], callback
else
ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
return callback(error) if error?
console.log "DOCS", docs
# FIXME want to store state after project has been sent to clsi
ClsiStateManager.setState project_id, user_id, project, (error, state) ->
if error?
logger.err err:error, project_id:project_id, "error storing state in redis"
#return callback(error)
options.state = state
ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
_getContentFromDocUpdater: (project_id, callback = (error, docs) ->) ->
DocumentUpdaterHandler.getProjectDocs project_id, (error, docs) ->
return callback(error) if error?
callback(null, docs)
_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?
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")
2017-08-01 09:39:57 -04:00
rev: doc.rev
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-01 09:39:57 -04:00
incremental: options.incremental
state: options.state
rootResourcePath: rootResourcePath
resources: resources
}
2015-09-10 11:41:48 -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
wordcount_url = ClsiManager._getCompilerUrl(options?.compileGroup, project_id, user_id, "wordcount")
2016-04-19 11:48:51 -04:00
opts =
url: wordcount_url
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