2014-02-12 12:27:43 -05:00
|
|
|
spawn = require("child_process").spawn
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
2016-05-23 10:45:39 -04:00
|
|
|
logger.info "using standard command runner"
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
module.exports = CommandRunner =
|
2016-07-26 07:30:29 -04:00
|
|
|
run: (project_id, command, directory, image, timeout, environment, callback = (error) ->) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
command = (arg.replace('$COMPILE_DIR', directory) for arg in command)
|
|
|
|
logger.log project_id: project_id, command: command, directory: directory, "running command"
|
|
|
|
logger.warn "timeouts and sandboxing are not enabled with CommandRunner"
|
|
|
|
|
2016-07-26 07:30:29 -04:00
|
|
|
# merge environment settings
|
|
|
|
env = {}
|
|
|
|
env[key] = value for key, value of process.env
|
|
|
|
env[key] = value for key, value of environment
|
|
|
|
|
2016-07-14 09:47:36 -04:00
|
|
|
# run command as detached process so it has its own process group (which can be killed if needed)
|
2016-07-26 07:30:29 -04:00
|
|
|
proc = spawn command[0], command.slice(1), stdio: "inherit", cwd: directory, detached: true, env: env
|
2016-07-14 09:47:36 -04:00
|
|
|
|
2016-05-23 09:13:55 -04:00
|
|
|
proc.on "error", (err)->
|
|
|
|
logger.err err:err, project_id:project_id, command: command, directory: directory, "error running command"
|
|
|
|
callback(err)
|
2016-07-14 09:47:36 -04:00
|
|
|
|
|
|
|
proc.on "close", (code, signal) ->
|
|
|
|
logger.info code:code, signal:signal, project_id:project_id, "command exited"
|
|
|
|
if signal is 'SIGTERM' # signal from kill method below
|
|
|
|
err = new Error("terminated")
|
|
|
|
err.terminated = true
|
|
|
|
return callback(err)
|
2016-07-26 11:22:38 -04:00
|
|
|
else if code
|
|
|
|
err = new Error("exit")
|
|
|
|
err.code = code
|
|
|
|
return callback(err)
|
2016-07-14 09:47:36 -04:00
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
|
|
|
return proc.pid # return process id to allow job to be killed if necessary
|
|
|
|
|
|
|
|
kill: (pid, callback = (error) ->) ->
|
|
|
|
try
|
|
|
|
process.kill -pid # kill all processes in group
|
|
|
|
catch err
|
|
|
|
return callback(err)
|
|
|
|
callback()
|