LocalCommandRunner.js 2.7 KB

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