2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
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:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* 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 CommandRunner
|
|
|
|
const { spawn } = require('child_process')
|
2021-04-29 10:30:54 -04:00
|
|
|
const _ = require('lodash')
|
2020-02-19 06:14:37 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.info('using standard command runner')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = CommandRunner = {
|
2020-06-11 11:01:44 -04:00
|
|
|
run(
|
|
|
|
project_id,
|
|
|
|
command,
|
|
|
|
directory,
|
|
|
|
image,
|
|
|
|
timeout,
|
|
|
|
environment,
|
|
|
|
compileGroup,
|
|
|
|
callback
|
|
|
|
) {
|
2020-02-19 06:14:37 -05:00
|
|
|
let key, value
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2019-09-18 18:44:02 -04:00
|
|
|
} else {
|
|
|
|
callback = _.once(callback)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
command = Array.from(command).map(arg =>
|
2020-02-19 06:14:37 -05:00
|
|
|
arg.toString().replace('$COMPILE_DIR', directory)
|
|
|
|
)
|
|
|
|
logger.log({ project_id, command, directory }, 'running command')
|
|
|
|
logger.warn('timeouts and sandboxing are not enabled with CommandRunner')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
// merge environment settings
|
|
|
|
const env = {}
|
|
|
|
for (key in process.env) {
|
|
|
|
value = process.env[key]
|
|
|
|
env[key] = value
|
|
|
|
}
|
|
|
|
for (key in environment) {
|
|
|
|
value = environment[key]
|
|
|
|
env[key] = value
|
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
// run command as detached process so it has its own process group (which can be killed if needed)
|
|
|
|
const proc = spawn(command[0], command.slice(1), { cwd: directory, env })
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
let stdout = ''
|
2021-07-13 07:04:48 -04:00
|
|
|
proc.stdout.setEncoding('utf8').on('data', data => (stdout += data))
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
proc.on('error', function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.err(
|
|
|
|
{ err, project_id, command, directory },
|
|
|
|
'error running command'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
})
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
proc.on('close', function (code, signal) {
|
2020-02-19 06:14:37 -05:00
|
|
|
let err
|
|
|
|
logger.info({ code, signal, project_id }, 'command exited')
|
|
|
|
if (signal === 'SIGTERM') {
|
|
|
|
// signal from kill method below
|
|
|
|
err = new Error('terminated')
|
|
|
|
err.terminated = true
|
|
|
|
return callback(err)
|
|
|
|
} else if (code === 1) {
|
|
|
|
// exit status from chktex
|
|
|
|
err = new Error('exited')
|
|
|
|
err.code = code
|
|
|
|
return callback(err)
|
|
|
|
} else {
|
|
|
|
return callback(null, { stdout: stdout })
|
|
|
|
}
|
|
|
|
})
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
return proc.pid
|
|
|
|
}, // return process id to allow job to be killed if necessary
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
kill(pid, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
process.kill(-pid) // kill all processes in group
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return callback()
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|