2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let LatexRunner
|
|
|
|
const Path = require('path')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-19 06:14:37 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const Metrics = require('./Metrics')
|
|
|
|
const CommandRunner = require('./CommandRunner')
|
2020-05-20 06:45:29 -04:00
|
|
|
const fs = require('fs')
|
2020-02-19 06:14:37 -05:00
|
|
|
|
|
|
|
const ProcessTable = {} // table of currently running jobs (pids or docker container names)
|
|
|
|
|
2021-05-19 06:17:08 -04:00
|
|
|
const TIME_V_METRICS = Object.entries({
|
|
|
|
'cpu-percent': /Percent of CPU this job got: (\d+)/m,
|
|
|
|
'cpu-time': /User time.*: (\d+.\d+)/m,
|
2021-07-13 07:04:48 -04:00
|
|
|
'sys-time': /System time.*: (\d+.\d+)/m,
|
2021-05-19 06:17:08 -04:00
|
|
|
})
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = LatexRunner = {
|
|
|
|
runLatex(project_id, options, callback) {
|
|
|
|
let command
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
let {
|
|
|
|
directory,
|
|
|
|
mainFile,
|
|
|
|
compiler,
|
|
|
|
timeout,
|
|
|
|
image,
|
|
|
|
environment,
|
2020-06-11 11:01:44 -04:00
|
|
|
flags,
|
2021-07-13 07:04:48 -04:00
|
|
|
compileGroup,
|
2020-02-19 06:14:37 -05:00
|
|
|
} = options
|
|
|
|
if (!compiler) {
|
|
|
|
compiler = 'pdflatex'
|
|
|
|
}
|
|
|
|
if (!timeout) {
|
|
|
|
timeout = 60000
|
|
|
|
} // milliseconds
|
|
|
|
|
|
|
|
logger.log(
|
2020-06-11 11:01:44 -04:00
|
|
|
{
|
|
|
|
directory,
|
|
|
|
compiler,
|
|
|
|
timeout,
|
|
|
|
mainFile,
|
|
|
|
environment,
|
|
|
|
flags,
|
2021-07-13 07:04:48 -04:00
|
|
|
compileGroup,
|
2020-06-11 11:01:44 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
'starting compile'
|
|
|
|
)
|
|
|
|
|
|
|
|
// We want to run latexmk on the tex file which we will automatically
|
|
|
|
// generate from the Rtex/Rmd/md file.
|
|
|
|
mainFile = mainFile.replace(/\.(Rtex|md|Rmd)$/, '.tex')
|
|
|
|
|
|
|
|
if (compiler === 'pdflatex') {
|
|
|
|
command = LatexRunner._pdflatexCommand(mainFile, flags)
|
|
|
|
} else if (compiler === 'latex') {
|
|
|
|
command = LatexRunner._latexCommand(mainFile, flags)
|
|
|
|
} else if (compiler === 'xelatex') {
|
|
|
|
command = LatexRunner._xelatexCommand(mainFile, flags)
|
|
|
|
} else if (compiler === 'lualatex') {
|
|
|
|
command = LatexRunner._lualatexCommand(mainFile, flags)
|
|
|
|
} else {
|
|
|
|
return callback(new Error(`unknown compiler: ${compiler}`))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings.clsi != null ? Settings.clsi.strace : undefined) {
|
|
|
|
command = ['strace', '-o', 'strace', '-ff'].concat(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = `${project_id}` // record running project under this id
|
|
|
|
|
|
|
|
return (ProcessTable[id] = CommandRunner.run(
|
|
|
|
project_id,
|
|
|
|
command,
|
|
|
|
directory,
|
|
|
|
image,
|
|
|
|
timeout,
|
|
|
|
environment,
|
2020-06-11 11:01:44 -04:00
|
|
|
compileGroup,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, output) {
|
2020-02-19 06:14:37 -05:00
|
|
|
delete ProcessTable[id]
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const runs =
|
|
|
|
__guard__(
|
2021-07-13 07:04:48 -04:00
|
|
|
__guard__(output != null ? output.stderr : undefined, x1 =>
|
2020-02-19 06:14:37 -05:00
|
|
|
x1.match(/^Run number \d+ of .*latex/gm)
|
|
|
|
),
|
2021-07-13 07:04:48 -04:00
|
|
|
x => x.length
|
2020-02-19 06:14:37 -05:00
|
|
|
) || 0
|
|
|
|
const failed =
|
2021-07-13 07:04:48 -04:00
|
|
|
__guard__(output != null ? output.stdout : undefined, x2 =>
|
2020-02-19 06:14:37 -05:00
|
|
|
x2.match(/^Latexmk: Errors/m)
|
|
|
|
) != null
|
|
|
|
? 1
|
|
|
|
: 0
|
|
|
|
// counters from latexmk output
|
|
|
|
const stats = {}
|
|
|
|
stats['latexmk-errors'] = failed
|
|
|
|
stats['latex-runs'] = runs
|
|
|
|
stats['latex-runs-with-errors'] = failed ? runs : 0
|
|
|
|
stats[`latex-runs-${runs}`] = 1
|
|
|
|
stats[`latex-runs-with-errors-${runs}`] = failed ? 1 : 0
|
|
|
|
// timing information from /usr/bin/time
|
|
|
|
const timings = {}
|
2021-05-19 06:17:08 -04:00
|
|
|
const stderr = (output && output.stderr) || ''
|
|
|
|
if (stderr.includes('Command being timed:')) {
|
|
|
|
// Add metrics for runs with `$ time -v ...`
|
|
|
|
for (const [timing, matcher] of TIME_V_METRICS) {
|
|
|
|
const match = stderr.match(matcher)
|
|
|
|
if (match) {
|
|
|
|
timings[timing] = parseFloat(match[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 06:45:29 -04:00
|
|
|
// record output files
|
|
|
|
LatexRunner.writeLogOutput(project_id, directory, output, () => {
|
|
|
|
return callback(error, output, stats, timings)
|
|
|
|
})
|
2020-06-02 04:18:38 -04:00
|
|
|
}
|
|
|
|
))
|
2020-05-20 06:45:29 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
writeLogOutput(project_id, directory, output, callback) {
|
|
|
|
if (!output) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
// internal method for writing non-empty log files
|
|
|
|
function _writeFile(file, content, cb) {
|
|
|
|
if (content && content.length > 0) {
|
2021-07-13 07:04:48 -04:00
|
|
|
fs.writeFile(file, content, err => {
|
2020-05-20 06:45:29 -04:00
|
|
|
if (err) {
|
2020-06-02 04:18:38 -04:00
|
|
|
logger.error({ project_id, file }, 'error writing log file') // don't fail on error
|
2020-05-20 06:45:29 -04:00
|
|
|
}
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cb()
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-05-20 06:45:29 -04:00
|
|
|
}
|
|
|
|
// write stdout and stderr, ignoring errors
|
2020-06-02 04:18:38 -04:00
|
|
|
_writeFile(Path.join(directory, 'output.stdout'), output.stdout, () => {
|
|
|
|
_writeFile(Path.join(directory, 'output.stderr'), output.stderr, () => {
|
2020-05-20 06:45:29 -04:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
killLatex(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const id = `${project_id}`
|
|
|
|
logger.log({ id }, 'killing running compile')
|
|
|
|
if (ProcessTable[id] == null) {
|
|
|
|
logger.warn({ id }, 'no such project to kill')
|
|
|
|
return callback(null)
|
|
|
|
} else {
|
|
|
|
return CommandRunner.kill(ProcessTable[id], callback)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_latexmkBaseCommand(flags) {
|
|
|
|
let args = [
|
|
|
|
'latexmk',
|
|
|
|
'-cd',
|
|
|
|
'-f',
|
|
|
|
'-jobname=output',
|
|
|
|
'-auxdir=$COMPILE_DIR',
|
|
|
|
'-outdir=$COMPILE_DIR',
|
|
|
|
'-synctex=1',
|
2021-07-13 07:04:48 -04:00
|
|
|
'-interaction=batchmode',
|
2020-02-19 06:14:37 -05:00
|
|
|
]
|
|
|
|
if (flags) {
|
|
|
|
args = args.concat(flags)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
__guard__(
|
|
|
|
Settings != null ? Settings.clsi : undefined,
|
2021-07-13 07:04:48 -04:00
|
|
|
x => x.latexmkCommandPrefix
|
2020-02-19 06:14:37 -05:00
|
|
|
) || []
|
|
|
|
).concat(args)
|
|
|
|
},
|
|
|
|
|
|
|
|
_pdflatexCommand(mainFile, flags) {
|
|
|
|
return LatexRunner._latexmkBaseCommand(flags).concat([
|
|
|
|
'-pdf',
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join('$COMPILE_DIR', mainFile),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
|
|
|
},
|
|
|
|
|
|
|
|
_latexCommand(mainFile, flags) {
|
|
|
|
return LatexRunner._latexmkBaseCommand(flags).concat([
|
|
|
|
'-pdfdvi',
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join('$COMPILE_DIR', mainFile),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
|
|
|
},
|
|
|
|
|
|
|
|
_xelatexCommand(mainFile, flags) {
|
|
|
|
return LatexRunner._latexmkBaseCommand(flags).concat([
|
|
|
|
'-xelatex',
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join('$COMPILE_DIR', mainFile),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
|
|
|
},
|
|
|
|
|
|
|
|
_lualatexCommand(mainFile, flags) {
|
|
|
|
return LatexRunner._latexmkBaseCommand(flags).concat([
|
|
|
|
'-lualatex',
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join('$COMPILE_DIR', mainFile),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:14:14 -05:00
|
|
|
function __guard__(value, transform) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|