LocalCommandRunner.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import { spawn } from 'node:child_process'
  15. import { promisify } from 'node:util'
  16. import _ from 'lodash'
  17. import logger from '@overleaf/logger'
  18. let CommandRunner
  19. logger.debug('using standard command runner')
  20. export default CommandRunner = {
  21. run(
  22. projectId,
  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({ projectId, 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. detached: true,
  54. })
  55. let stdout = ''
  56. proc.stdout.setEncoding('utf8').on('data', data => (stdout += data))
  57. proc.on('error', function (err) {
  58. logger.err(
  59. { err, projectId, command, directory },
  60. 'error running command'
  61. )
  62. return callback(err)
  63. })
  64. proc.on('close', function (code, signal) {
  65. let err
  66. logger.debug({ code, signal, projectId }, 'command exited')
  67. if (signal === 'SIGTERM') {
  68. // signal from kill method below
  69. err = new Error('terminated')
  70. err.terminated = true
  71. return callback(err)
  72. } else if (code === 1) {
  73. // exit status from chktex
  74. err = new Error('exited')
  75. err.code = code
  76. return callback(err)
  77. } else {
  78. return callback(null, { stdout, exitCode: code })
  79. }
  80. })
  81. return proc.pid
  82. }, // return process id to allow job to be killed if necessary
  83. kill(pid, callback) {
  84. if (callback == null) {
  85. callback = function () {}
  86. }
  87. try {
  88. process.kill(-pid) // kill all processes in group
  89. } catch (err) {
  90. return callback(err)
  91. }
  92. return callback()
  93. },
  94. canRunSyncTeXInOutputDir() {
  95. return true
  96. },
  97. }
  98. CommandRunner.promises = {
  99. run: promisify(CommandRunner.run),
  100. kill: promisify(CommandRunner.kill),
  101. }