LocalCommandRunner.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let CommandRunner
  17. const { spawn } = require('child_process')
  18. const logger = require('logger-sharelatex')
  19. logger.info('using standard command runner')
  20. module.exports = CommandRunner = {
  21. run(
  22. project_id,
  23. command,
  24. directory,
  25. image,
  26. timeout,
  27. environment,
  28. compileGroup,
  29. callback
  30. ) {
  31. let key, value
  32. if (callback == null) {
  33. callback = function(error) {}
  34. }
  35. command = Array.from(command).map(arg =>
  36. arg.toString().replace('$COMPILE_DIR', directory)
  37. )
  38. logger.log({ project_id, command, directory }, 'running command')
  39. logger.warn('timeouts and sandboxing are not enabled with CommandRunner')
  40. // merge environment settings
  41. const env = {}
  42. for (key in process.env) {
  43. value = process.env[key]
  44. env[key] = value
  45. }
  46. for (key in environment) {
  47. value = environment[key]
  48. env[key] = value
  49. }
  50. // run command as detached process so it has its own process group (which can be killed if needed)
  51. const proc = spawn(command[0], command.slice(1), { cwd: directory, env })
  52. let stdout = ''
  53. proc.stdout.setEncoding('utf8').on('data', data => (stdout += data))
  54. proc.on('error', function(err) {
  55. logger.err(
  56. { err, project_id, command, directory },
  57. 'error running command'
  58. )
  59. return callback(err)
  60. })
  61. proc.on('close', function(code, signal) {
  62. let err
  63. logger.info({ code, signal, project_id }, 'command exited')
  64. if (signal === 'SIGTERM') {
  65. // signal from kill method below
  66. err = new Error('terminated')
  67. err.terminated = true
  68. return callback(err)
  69. } else if (code === 1) {
  70. // exit status from chktex
  71. err = new Error('exited')
  72. err.code = code
  73. return callback(err)
  74. } else {
  75. return callback(null, { stdout: stdout })
  76. }
  77. })
  78. return proc.pid
  79. }, // return process id to allow job to be killed if necessary
  80. kill(pid, callback) {
  81. if (callback == null) {
  82. callback = function(error) {}
  83. }
  84. try {
  85. process.kill(-pid) // kill all processes in group
  86. } catch (err) {
  87. return callback(err)
  88. }
  89. return callback()
  90. }
  91. }