2019-08-12 07:33:01 -04:00
|
|
|
const async = require('async')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const request = require('request')
|
|
|
|
const ProjectGetter = require('../Project/ProjectGetter')
|
|
|
|
const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const Url = require('url')
|
|
|
|
const ClsiCookieManager = require('./ClsiCookieManager')(
|
|
|
|
Settings.apis.clsi != null ? Settings.apis.clsi.backendGroupName : undefined
|
|
|
|
)
|
|
|
|
const NewBackendCloudClsiCookieManager = require('./ClsiCookieManager')(
|
|
|
|
Settings.apis.clsi_new != null
|
|
|
|
? Settings.apis.clsi_new.backendGroupName
|
|
|
|
: undefined
|
|
|
|
)
|
|
|
|
const ClsiStateManager = require('./ClsiStateManager')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const ClsiFormatChecker = require('./ClsiFormatChecker')
|
|
|
|
const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
|
|
|
const Metrics = require('metrics-sharelatex')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
const ClsiManager = {
|
|
|
|
sendRequest(projectId, userId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager.sendRequestOnce(projectId, userId, options, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
status,
|
|
|
|
...result
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (status === 'conflict') {
|
|
|
|
options = _.clone(options)
|
|
|
|
options.syncType = 'full' // force full compile
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager.sendRequestOnce(projectId, userId, options, callback) // try again
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(error, status, ...result)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
sendRequestOnce(projectId, userId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._buildRequest(projectId, options, function(error, req) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
if (error.message === 'no main file specified') {
|
|
|
|
return callback(null, 'validation-problems', null, null, {
|
|
|
|
mainFile: error.message
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
logger.log({ projectId }, 'sending compile to CLSI')
|
|
|
|
ClsiManager._sendBuiltRequest(projectId, userId, req, options, function(
|
|
|
|
error,
|
|
|
|
status,
|
|
|
|
...result
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(error, status, ...result)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// for public API requests where there is no project id
|
2019-08-12 07:33:01 -04:00
|
|
|
sendExternalRequest(submissionId, clsiRequest, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
logger.log(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ submissionId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'sending external compile to CLSI',
|
2019-08-12 07:33:01 -04:00
|
|
|
clsiRequest
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._sendBuiltRequest(
|
|
|
|
submissionId,
|
2019-05-29 05:21:06 -04:00
|
|
|
null,
|
2019-08-12 07:33:01 -04:00
|
|
|
clsiRequest,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
function(error, status, ...result) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(error, status, ...result)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
stopCompile(projectId, userId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const compilerUrl = this._getCompilerUrl(
|
|
|
|
options != null ? options.compileGroup : undefined,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'compile/stop'
|
|
|
|
)
|
|
|
|
const opts = {
|
|
|
|
url: compilerUrl,
|
|
|
|
method: 'POST'
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._makeRequest(projectId, opts, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
deleteAuxFiles(projectId, userId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const compilerUrl = this._getCompilerUrl(
|
|
|
|
options != null ? options.compileGroup : undefined,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
|
|
|
userId
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
const opts = {
|
|
|
|
url: compilerUrl,
|
|
|
|
method: 'DELETE'
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._makeRequest(projectId, opts, clsiError =>
|
2019-05-29 05:21:06 -04:00
|
|
|
// always clear the project state from the docupdater, even if there
|
|
|
|
// was a problem with the request to the clsi
|
2019-08-12 07:33:01 -04:00
|
|
|
DocumentUpdaterHandler.clearProjectState(projectId, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
docUpdaterError
|
|
|
|
) {
|
|
|
|
const error = clsiError || docUpdaterError
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_sendBuiltRequest(projectId, userId, req, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiFormatChecker.checkRecoursesForProblems(
|
2019-05-29 05:21:06 -04:00
|
|
|
req.compile != null ? req.compile.resources : undefined,
|
|
|
|
function(err, validationProblems) {
|
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
err,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'could not check resources for potential problems before sending to clsi'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (validationProblems != null) {
|
|
|
|
logger.log(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ projectId, validationProblems },
|
2019-05-29 05:21:06 -04:00
|
|
|
'problems with users latex before compile was attempted'
|
|
|
|
)
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
'validation-problems',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
validationProblems
|
|
|
|
)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._postToClsi(
|
|
|
|
projectId,
|
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
req,
|
|
|
|
options.compileGroup,
|
|
|
|
function(error, response) {
|
|
|
|
if (error != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ err: error, projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error sending request to clsi'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
if (response != null) {
|
|
|
|
logger.log(
|
|
|
|
{
|
|
|
|
projectId,
|
|
|
|
outputFilesLength:
|
|
|
|
response.outputFiles && response.outputFiles.length,
|
|
|
|
status: response.status,
|
|
|
|
compile_status: response.compile && response.compile.status
|
|
|
|
},
|
|
|
|
'received compile response from CLSI'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ClsiCookieManager._getServerId(projectId, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
err,
|
|
|
|
clsiServerId
|
|
|
|
) {
|
|
|
|
if (err != null) {
|
2019-08-12 07:33:01 -04:00
|
|
|
logger.warn({ err, projectId }, 'error getting server id')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const outputFiles = ClsiManager._parseOutputFiles(
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
|
|
|
response && response.compile && response.compile.outputFiles
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(
|
2019-05-29 05:21:06 -04:00
|
|
|
null,
|
2019-08-12 07:33:01 -04:00
|
|
|
response && response.compile && response.compile.status,
|
2019-05-29 05:21:06 -04:00
|
|
|
outputFiles,
|
|
|
|
clsiServerId
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_makeRequest(projectId, opts, callback) {
|
|
|
|
async.series(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
currentBackend(cb) {
|
|
|
|
const startTime = new Date()
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiCookieManager.getCookieJar(projectId, function(err, jar) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err }, 'error getting cookie jar for clsi request')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
opts.jar = jar
|
|
|
|
const timer = new Metrics.Timer('compile.currentBackend')
|
2019-08-12 07:33:01 -04:00
|
|
|
request(opts, function(err, response, body) {
|
2019-05-29 05:21:06 -04:00
|
|
|
timer.done()
|
|
|
|
Metrics.inc(
|
|
|
|
`compile.currentBackend.response.${
|
|
|
|
response != null ? response.statusCode : undefined
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ err, projectId, url: opts != null ? opts.url : undefined },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error making request to clsi'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiCookieManager.setServerId(projectId, response, function(err) {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn({ err, projectId }, 'error setting server id')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(err, response, body) // return as soon as the standard compile has returned
|
|
|
|
cb(err, {
|
|
|
|
response,
|
|
|
|
body,
|
|
|
|
finishTime: new Date() - startTime
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
newBackend(cb) {
|
|
|
|
const startTime = new Date()
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._makeNewBackendRequest(projectId, opts, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
err,
|
|
|
|
response,
|
|
|
|
body
|
|
|
|
) {
|
|
|
|
Metrics.inc(
|
|
|
|
`compile.newBackend.response.${
|
|
|
|
response != null ? response.statusCode : undefined
|
|
|
|
}`
|
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
cb(err, {
|
2019-05-29 05:21:06 -04:00
|
|
|
response,
|
|
|
|
body,
|
|
|
|
finishTime: new Date() - startTime
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, results) {
|
2019-08-12 07:33:01 -04:00
|
|
|
if (err != null) {
|
|
|
|
logger.warn({ err }, 'Error making request to CLSI')
|
|
|
|
return
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
const timeDifference =
|
|
|
|
(results.newBackend != null
|
|
|
|
? results.newBackend.finishTime
|
|
|
|
: undefined) -
|
|
|
|
(results.currentBackend != null
|
|
|
|
? results.currentBackend.finishTime
|
|
|
|
: undefined)
|
2019-08-12 07:33:01 -04:00
|
|
|
const newStatusCode =
|
|
|
|
results.newBackend &&
|
|
|
|
results.newBackend.response &&
|
|
|
|
results.newBackend.response.statusCode
|
|
|
|
const currentStatusCode =
|
|
|
|
results.currentBackend &&
|
|
|
|
results.currentBackend.response &&
|
|
|
|
results.currentBackend.response.statusCode
|
|
|
|
const statusCodeSame = newStatusCode === currentStatusCode
|
2019-05-29 05:21:06 -04:00
|
|
|
const currentCompileTime =
|
|
|
|
results.currentBackend != null
|
|
|
|
? results.currentBackend.finishTime
|
|
|
|
: undefined
|
|
|
|
const newBackendCompileTime =
|
|
|
|
results.newBackend != null ? results.newBackend.finishTime : undefined
|
2019-08-12 07:33:01 -04:00
|
|
|
logger.log(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
statusCodeSame,
|
|
|
|
timeDifference,
|
|
|
|
currentCompileTime,
|
|
|
|
newBackendCompileTime,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'both clsi requests returned'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_makeNewBackendRequest(projectId, baseOpts, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
|
|
|
(Settings.apis.clsi_new != null
|
|
|
|
? Settings.apis.clsi_new.url
|
|
|
|
: undefined) == null
|
|
|
|
) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const opts = _.clone(baseOpts)
|
|
|
|
opts.url = opts.url.replace(
|
|
|
|
Settings.apis.clsi.url,
|
|
|
|
Settings.apis.clsi_new != null ? Settings.apis.clsi_new.url : undefined
|
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
NewBackendCloudClsiCookieManager.getCookieJar(projectId, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
err,
|
|
|
|
jar
|
|
|
|
) {
|
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err }, 'error getting cookie jar for clsi request')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
opts.jar = jar
|
|
|
|
const timer = new Metrics.Timer('compile.newBackend')
|
2019-08-12 07:33:01 -04:00
|
|
|
request(opts, function(err, response, body) {
|
2019-05-29 05:21:06 -04:00
|
|
|
timer.done()
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ err, projectId, url: opts != null ? opts.url : undefined },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error making request to new clsi'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
NewBackendCloudClsiCookieManager.setServerId(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
response,
|
|
|
|
function(err) {
|
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ err, projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error setting server id new backend'
|
|
|
|
)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(err, response, body)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_getCompilerUrl(compileGroup, projectId, userId, action) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const host = Settings.apis.clsi.url
|
2019-08-12 07:33:01 -04:00
|
|
|
let path = `/project/${projectId}`
|
|
|
|
if (userId != null) {
|
|
|
|
path += `/user/${userId}`
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (action != null) {
|
|
|
|
path += `/${action}`
|
|
|
|
}
|
|
|
|
return `${host}${path}`
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_postToClsi(projectId, userId, req, compileGroup, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const compileUrl = this._getCompilerUrl(
|
|
|
|
compileGroup,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'compile'
|
|
|
|
)
|
|
|
|
const opts = {
|
|
|
|
url: compileUrl,
|
|
|
|
json: req,
|
|
|
|
method: 'POST'
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._makeRequest(projectId, opts, function(error, response, body) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, body)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (response.statusCode === 413) {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, { compile: { status: 'project-too-large' } })
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (response.statusCode === 409) {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, { compile: { status: 'conflict' } })
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (response.statusCode === 423) {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, { compile: { status: 'compile-in-progress' } })
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
error = new Error(
|
|
|
|
`CLSI returned non-success code: ${response.statusCode}`
|
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
logger.warn({ err: error, projectId }, 'CLSI returned failure code')
|
|
|
|
callback(error, body)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_parseOutputFiles(projectId, rawOutputFiles) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (rawOutputFiles == null) {
|
|
|
|
rawOutputFiles = []
|
|
|
|
}
|
|
|
|
const outputFiles = []
|
2019-08-12 07:33:01 -04:00
|
|
|
for (const file of rawOutputFiles) {
|
2019-05-29 05:21:06 -04:00
|
|
|
outputFiles.push({
|
|
|
|
path: file.path, // the clsi is now sending this to web
|
|
|
|
url: Url.parse(file.url).path, // the location of the file on the clsi, excluding the host part
|
|
|
|
type: file.type,
|
|
|
|
build: file.build
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return outputFiles
|
|
|
|
},
|
|
|
|
|
|
|
|
VALID_COMPILERS: ['pdflatex', 'latex', 'xelatex', 'lualatex'],
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_buildRequest(projectId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ProjectGetter.getProject(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
{ compiler: 1, rootDoc_id: 1, imageName: 1, rootFolder: 1 },
|
|
|
|
function(error, project) {
|
|
|
|
let timer
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(
|
2019-08-12 07:33:01 -04:00
|
|
|
new Errors.NotFoundError(`project does not exist: ${projectId}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
if (!ClsiManager.VALID_COMPILERS.includes(project.compiler)) {
|
2019-05-29 05:21:06 -04:00
|
|
|
project.compiler = 'pdflatex'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.incrementalCompilesEnabled || options.syncType != null) {
|
|
|
|
// new way, either incremental or full
|
|
|
|
timer = new Metrics.Timer('editor.compile-getdocs-redis')
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager.getContentFromDocUpdaterIfMatch(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
project,
|
|
|
|
options,
|
|
|
|
function(error, projectStateHash, docUpdaterDocs) {
|
|
|
|
timer.done()
|
|
|
|
if (error != null) {
|
|
|
|
logger.error(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ err: error, projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error checking project state'
|
|
|
|
)
|
|
|
|
// note: we don't bail out when there's an error getting
|
|
|
|
// incremental files from the docupdater, we just fall back
|
|
|
|
// to a normal compile below
|
|
|
|
} else {
|
|
|
|
logger.log(
|
|
|
|
{
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
projectStateHash,
|
|
|
|
docs: docUpdaterDocs != null
|
|
|
|
},
|
|
|
|
'checked project state'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// see if we can send an incremental update to the CLSI
|
|
|
|
if (
|
|
|
|
docUpdaterDocs != null &&
|
|
|
|
options.syncType !== 'full' &&
|
|
|
|
error == null
|
|
|
|
) {
|
|
|
|
Metrics.inc('compile-from-redis')
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._buildRequestFromDocupdater(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
projectStateHash,
|
|
|
|
docUpdaterDocs,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Metrics.inc('compile-from-mongo')
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._buildRequestFromMongo(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
projectStateHash,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// old way, always from mongo
|
|
|
|
timer = new Metrics.Timer('editor.compile-getdocs-mongo')
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._getContentFromMongo(projectId, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
docs,
|
|
|
|
files
|
|
|
|
) {
|
|
|
|
timer.done()
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._finaliseRequest(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
docs,
|
|
|
|
files,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
getContentFromDocUpdaterIfMatch(projectId, project, options, callback) {
|
|
|
|
ClsiStateManager.computeHash(project, options, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
projectStateHash
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
DocumentUpdaterHandler.getProjectDocsIfMatch(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
projectStateHash,
|
|
|
|
function(error, docs) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, projectStateHash, docs)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
getOutputFileStream(projectId, userId, buildId, outputFilePath, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const url = `${
|
|
|
|
Settings.apis.clsi.url
|
2019-08-12 07:33:01 -04:00
|
|
|
}/project/${projectId}/user/${userId}/build/${buildId}/output/${outputFilePath}`
|
|
|
|
ClsiCookieManager.getCookieJar(projectId, function(err, jar) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const options = { url, method: 'GET', timeout: 60 * 1000, jar }
|
|
|
|
const readStream = request(options)
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, readStream)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildRequestFromDocupdater(
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
projectStateHash,
|
|
|
|
docUpdaterDocs,
|
|
|
|
callback
|
|
|
|
) {
|
2019-08-12 07:33:01 -04:00
|
|
|
ProjectEntityHandler.getAllDocPathsFromProject(project, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
docPath
|
|
|
|
) {
|
|
|
|
let path
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const docs = {}
|
2019-08-12 07:33:01 -04:00
|
|
|
for (let doc of docUpdaterDocs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
path = docPath[doc._id]
|
|
|
|
docs[path] = doc
|
|
|
|
}
|
|
|
|
// send new docs but not files as those are already on the clsi
|
|
|
|
options = _.clone(options)
|
|
|
|
options.syncType = 'incremental'
|
|
|
|
options.syncState = projectStateHash
|
|
|
|
// create stub doc entries for any possible root docs, if not
|
|
|
|
// present in the docupdater. This allows finaliseRequest to
|
|
|
|
// identify the root doc.
|
|
|
|
const possibleRootDocIds = [options.rootDoc_id, project.rootDoc_id]
|
2019-08-12 07:33:01 -04:00
|
|
|
for (const rootDocId of possibleRootDocIds) {
|
|
|
|
if (rootDocId != null && rootDocId in docPath) {
|
|
|
|
path = docPath[rootDocId]
|
2019-05-29 05:21:06 -04:00
|
|
|
if (docs[path] == null) {
|
2019-08-12 07:33:01 -04:00
|
|
|
docs[path] = { _id: rootDocId, path }
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._finaliseRequest(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
docs,
|
|
|
|
[],
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildRequestFromMongo(
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
projectStateHash,
|
|
|
|
callback
|
|
|
|
) {
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._getContentFromMongo(projectId, function(error, docs, files) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
options = _.clone(options)
|
|
|
|
options.syncType = 'full'
|
|
|
|
options.syncState = projectStateHash
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._finaliseRequest(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
options,
|
|
|
|
project,
|
|
|
|
docs,
|
|
|
|
files,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_getContentFromMongo(projectId, callback) {
|
|
|
|
DocumentUpdaterHandler.flushProjectToMongo(projectId, function(error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ProjectEntityHandler.getAllDocs(projectId, function(error, docs) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (docs == null) {
|
|
|
|
docs = {}
|
|
|
|
}
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ProjectEntityHandler.getAllFiles(projectId, function(error, files) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (files == null) {
|
|
|
|
files = {}
|
|
|
|
}
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, docs, files)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
_finaliseRequest(projectId, options, project, docs, files, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let doc, path
|
|
|
|
const resources = []
|
|
|
|
let rootResourcePath = null
|
|
|
|
let rootResourcePathOverride = null
|
|
|
|
let hasMainFile = false
|
|
|
|
let numberOfDocsInProject = 0
|
|
|
|
|
|
|
|
for (path in docs) {
|
|
|
|
doc = docs[path]
|
|
|
|
path = path.replace(/^\//, '') // Remove leading /
|
|
|
|
numberOfDocsInProject++
|
|
|
|
if (doc.lines != null) {
|
|
|
|
// add doc to resources unless it is just a stub entry
|
|
|
|
resources.push({
|
|
|
|
path,
|
|
|
|
content: doc.lines.join('\n')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
project.rootDoc_id != null &&
|
|
|
|
doc._id.toString() === project.rootDoc_id.toString()
|
|
|
|
) {
|
|
|
|
rootResourcePath = path
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
options.rootDoc_id != null &&
|
|
|
|
doc._id.toString() === options.rootDoc_id.toString()
|
|
|
|
) {
|
|
|
|
rootResourcePathOverride = path
|
|
|
|
}
|
|
|
|
if (path === 'main.tex') {
|
|
|
|
hasMainFile = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rootResourcePathOverride != null) {
|
|
|
|
rootResourcePath = rootResourcePathOverride
|
|
|
|
}
|
|
|
|
if (rootResourcePath == null) {
|
|
|
|
if (hasMainFile) {
|
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'no root document found, setting to main.tex'
|
|
|
|
)
|
|
|
|
rootResourcePath = 'main.tex'
|
|
|
|
} else if (numberOfDocsInProject === 1) {
|
|
|
|
// only one file, must be the main document
|
|
|
|
for (path in docs) {
|
|
|
|
doc = docs[path]
|
|
|
|
rootResourcePath = path.replace(/^\//, '')
|
|
|
|
} // Remove leading /
|
|
|
|
logger.warn(
|
2019-08-12 07:33:01 -04:00
|
|
|
{ projectId, rootResourcePath },
|
2019-05-29 05:21:06 -04:00
|
|
|
'no root document found, single document in project'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback(new Error('no main file specified'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (path in files) {
|
|
|
|
const file = files[path]
|
|
|
|
path = path.replace(/^\//, '') // Remove leading /
|
|
|
|
resources.push({
|
|
|
|
path,
|
|
|
|
url: `${Settings.apis.filestore.url}/project/${project._id}/file/${
|
|
|
|
file._id
|
|
|
|
}`,
|
|
|
|
modified: file.created != null ? file.created.getTime() : undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, {
|
2019-05-29 05:21:06 -04:00
|
|
|
compile: {
|
|
|
|
options: {
|
|
|
|
compiler: project.compiler,
|
|
|
|
timeout: options.timeout,
|
|
|
|
imageName: project.imageName,
|
|
|
|
draft: !!options.draft,
|
|
|
|
check: options.check,
|
|
|
|
syncType: options.syncType,
|
|
|
|
syncState: options.syncState
|
|
|
|
},
|
|
|
|
rootResourcePath,
|
|
|
|
resources
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
wordCount(projectId, userId, file, options, callback) {
|
|
|
|
ClsiManager._buildRequest(projectId, options, function(error, req) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const filename = file || req.compile.rootResourcePath
|
|
|
|
const wordCountUrl = ClsiManager._getCompilerUrl(
|
2019-05-29 05:21:06 -04:00
|
|
|
options != null ? options.compileGroup : undefined,
|
2019-08-12 07:33:01 -04:00
|
|
|
projectId,
|
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'wordcount'
|
|
|
|
)
|
|
|
|
const opts = {
|
2019-08-12 07:33:01 -04:00
|
|
|
url: wordCountUrl,
|
2019-05-29 05:21:06 -04:00
|
|
|
qs: {
|
|
|
|
file: filename,
|
|
|
|
image: req.compile.options.imageName
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}
|
2019-08-12 07:33:01 -04:00
|
|
|
ClsiManager._makeRequest(projectId, opts, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
response,
|
|
|
|
body
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2019-08-12 07:33:01 -04:00
|
|
|
callback(null, body)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
error = new Error(
|
|
|
|
`CLSI returned non-success code: ${response.statusCode}`
|
|
|
|
)
|
2019-08-12 07:33:01 -04:00
|
|
|
logger.warn({ err: error, projectId }, 'CLSI returned failure code')
|
|
|
|
callback(error, body)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 07:33:01 -04:00
|
|
|
module.exports = ClsiManager
|