ClsiManager.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Path = require "path"
  2. async = require "async"
  3. Settings = require "settings-sharelatex"
  4. request = require('request')
  5. Project = require("../../models/Project").Project
  6. ProjectEntityHandler = require("../Project/ProjectEntityHandler")
  7. logger = require "logger-sharelatex"
  8. url = require("url")
  9. module.exports = ClsiManager =
  10. sendRequest: (project_id, settingsOverride = {}, callback = (error, success) ->) ->
  11. ClsiManager._buildRequest project_id, settingsOverride, (error, req) ->
  12. return callback(error) if error?
  13. logger.log project_id: project_id, "sending compile to CLSI"
  14. ClsiManager._postToClsi project_id, req, settingsOverride.compiler, (error, response) ->
  15. return callback(error) if error?
  16. logger.log project_id: project_id, response: response, "received compile response from CLSI"
  17. callback(
  18. null
  19. response?.compile?.status
  20. ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  21. )
  22. getLogLines: (project_id, callback = (error, lines) ->) ->
  23. request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) ->
  24. return callback(error) if error?
  25. callback null, body?.split("\n") or []
  26. deleteAuxFiles: (project_id, callback = (error) ->) ->
  27. request.del "#{Settings.apis.clsi.url}/project/#{project_id}", callback
  28. _postToClsi: (project_id, req, compiler, callback = (error, response) ->) ->
  29. if compiler == "priority"
  30. compilerUrl = Settings.apis.clsi_priority.url
  31. else
  32. compilerUrl = Settings.apis.clsi.url
  33. request.post {
  34. url: "#{compilerUrl}/project/#{project_id}/compile"
  35. json: req
  36. jar: false
  37. }, (error, response, body) ->
  38. return callback(error) if error?
  39. if 200 <= response.statusCode < 300
  40. callback null, body
  41. else
  42. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  43. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  44. callback error, body
  45. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  46. outputFiles = []
  47. for file in rawOutputFiles
  48. outputFiles.push
  49. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  50. type: file.type
  51. return outputFiles
  52. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  53. _buildRequest: (project_id, settingsOverride={}, callback = (error, request) ->) ->
  54. Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
  55. return callback(error) if error?
  56. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  57. if project.compiler not in ClsiManager.VALID_COMPILERS
  58. project.compiler = "pdflatex"
  59. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  60. return callback(error) if error?
  61. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  62. return callback(error) if error?
  63. resources = []
  64. rootResourcePath = null
  65. rootResourcePathOverride = null
  66. for path, doc of docs
  67. path = path.replace(/^\//, "") # Remove leading /
  68. resources.push
  69. path: path
  70. content: doc.lines.join("\n")
  71. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  72. rootResourcePath = path
  73. if settingsOverride.rootDoc_id? and doc._id.toString() == settingsOverride.rootDoc_id.toString()
  74. rootResourcePathOverride = path
  75. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  76. for path, file of files
  77. path = path.replace(/^\//, "") # Remove leading /
  78. resources.push
  79. path: path
  80. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  81. modified: file.created?.getTime()
  82. if !rootResourcePath?
  83. callback new Error("no root document exists")
  84. else
  85. callback null, {
  86. compile:
  87. options:
  88. compiler: project.compiler
  89. timeout: settingsOverride.timeout
  90. rootResourcePath: rootResourcePath
  91. resources: resources
  92. }