2020-02-19 06:14:37 -05:00
|
|
|
const Path = require('path')
|
2022-07-07 08:27:20 -04:00
|
|
|
const { promisify } = require('util')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-19 06:14:37 -05:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
const COMPILER_FLAGS = {
|
|
|
|
latex: '-pdfdvi',
|
|
|
|
lualatex: '-lualatex',
|
|
|
|
pdflatex: '-pdf',
|
|
|
|
xelatex: '-xelatex',
|
|
|
|
}
|
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
function runLatex(projectId, options, callback) {
|
2022-06-06 07:41:36 -04:00
|
|
|
const {
|
2022-06-06 07:41:07 -04:00
|
|
|
directory,
|
|
|
|
mainFile,
|
|
|
|
image,
|
|
|
|
environment,
|
|
|
|
flags,
|
|
|
|
compileGroup,
|
2022-06-06 07:41:36 -04:00
|
|
|
stopOnFirstError,
|
2022-07-18 10:11:30 -04:00
|
|
|
stats,
|
|
|
|
timings,
|
2022-06-06 07:41:07 -04:00
|
|
|
} = options
|
2022-06-06 07:41:36 -04:00
|
|
|
const compiler = options.compiler || 'pdflatex'
|
|
|
|
const timeout = options.timeout || 60000 // milliseconds
|
2022-06-06 07:41:07 -04:00
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
{
|
2020-02-19 06:14:37 -05:00
|
|
|
directory,
|
|
|
|
compiler,
|
|
|
|
timeout,
|
2022-06-06 07:41:07 -04:00
|
|
|
mainFile,
|
2020-02-19 06:14:37 -05:00
|
|
|
environment,
|
2020-06-11 11:01:44 -04:00
|
|
|
flags,
|
2021-07-13 07:04:48 -04:00
|
|
|
compileGroup,
|
2022-06-06 07:41:36 -04:00
|
|
|
stopOnFirstError,
|
2022-06-06 07:41:07 -04:00
|
|
|
},
|
|
|
|
'starting compile'
|
|
|
|
)
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
let command
|
|
|
|
try {
|
|
|
|
command = _buildLatexCommand(mainFile, {
|
|
|
|
compiler,
|
|
|
|
stopOnFirstError,
|
|
|
|
flags,
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err)
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const id = `${projectId}` // record running project under this id
|
|
|
|
|
|
|
|
ProcessTable[id] = CommandRunner.run(
|
|
|
|
projectId,
|
|
|
|
command,
|
|
|
|
directory,
|
|
|
|
image,
|
|
|
|
timeout,
|
|
|
|
environment,
|
|
|
|
compileGroup,
|
|
|
|
function (error, output) {
|
|
|
|
delete ProcessTable[id]
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const runs =
|
|
|
|
output?.stderr?.match(/^Run number \d+ of .*latex/gm)?.length || 0
|
|
|
|
const failed = output?.stdout?.match(/^Latexmk: Errors/m) != null ? 1 : 0
|
|
|
|
// counters from latexmk output
|
|
|
|
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 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])
|
2021-05-19 06:17:08 -04:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 04:18:38 -04:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
// record output files
|
|
|
|
_writeLogOutput(projectId, directory, output, () => {
|
2022-07-18 10:11:30 -04:00
|
|
|
callback(error, output)
|
2022-06-06 07:41:07 -04:00
|
|
|
})
|
2020-05-20 06:45:29 -04:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function _writeLogOutput(projectId, 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) {
|
|
|
|
fs.writeFile(file, content, err => {
|
|
|
|
if (err) {
|
2023-04-27 06:16:29 -04:00
|
|
|
logger.error({ err, projectId, file }, 'error writing log file') // don't fail on error
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
2020-05-20 06:45:29 -04:00
|
|
|
cb()
|
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
cb()
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
// write stdout and stderr, ignoring errors
|
|
|
|
_writeFile(Path.join(directory, 'output.stdout'), output.stdout, () => {
|
|
|
|
_writeFile(Path.join(directory, 'output.stderr'), output.stderr, () => {
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function killLatex(projectId, callback) {
|
|
|
|
const id = `${projectId}`
|
|
|
|
logger.debug({ id }, 'killing running compile')
|
|
|
|
if (ProcessTable[id] == null) {
|
|
|
|
logger.warn({ id }, 'no such project to kill')
|
|
|
|
callback(null)
|
|
|
|
} else {
|
|
|
|
CommandRunner.kill(ProcessTable[id], callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
function _buildLatexCommand(mainFile, opts = {}) {
|
|
|
|
const command = []
|
|
|
|
|
|
|
|
if (Settings.clsi?.strace) {
|
|
|
|
command.push('strace', '-o', 'strace', '-ff')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Settings.clsi?.latexmkCommandPrefix) {
|
|
|
|
command.push(...Settings.clsi.latexmkCommandPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic command and flags
|
|
|
|
command.push(
|
2022-06-06 07:41:07 -04:00
|
|
|
'latexmk',
|
|
|
|
'-cd',
|
|
|
|
'-jobname=output',
|
|
|
|
'-auxdir=$COMPILE_DIR',
|
|
|
|
'-outdir=$COMPILE_DIR',
|
|
|
|
'-synctex=1',
|
2022-06-06 07:41:36 -04:00
|
|
|
'-interaction=batchmode'
|
|
|
|
)
|
|
|
|
|
|
|
|
// Stop on first error option
|
|
|
|
if (opts.stopOnFirstError) {
|
|
|
|
command.push('-halt-on-error')
|
|
|
|
} else {
|
|
|
|
// Run all passes despite errors
|
|
|
|
command.push('-f')
|
2022-06-06 07:41:07 -04:00
|
|
|
}
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
// Extra flags
|
|
|
|
if (opts.flags) {
|
|
|
|
command.push(...opts.flags)
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
// TeX Engine selection
|
|
|
|
const compilerFlag = COMPILER_FLAGS[opts.compiler]
|
|
|
|
if (compilerFlag) {
|
|
|
|
command.push(compilerFlag)
|
|
|
|
} else {
|
|
|
|
throw new Error(`unknown compiler: ${opts.compiler}`)
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
// We want to run latexmk on the tex file which we will automatically
|
|
|
|
// generate from the Rtex/Rmd/md file.
|
2023-06-13 11:45:14 -04:00
|
|
|
mainFile = mainFile.replace(/\.(Rtex|md|Rmd)$/, '.tex')
|
2022-06-06 07:41:36 -04:00
|
|
|
command.push(Path.join('$COMPILE_DIR', mainFile))
|
2022-06-06 07:41:07 -04:00
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
return command
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
module.exports = {
|
|
|
|
runLatex,
|
|
|
|
killLatex,
|
2022-07-07 08:27:20 -04:00
|
|
|
promises: {
|
2022-07-18 10:11:30 -04:00
|
|
|
runLatex: promisify(runLatex),
|
2022-07-07 08:27:20 -04:00
|
|
|
killLatex: promisify(killLatex),
|
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|