ClsiManager.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 if response.statusCode == 413
  42. callback null, compile:status:"project-too-large"
  43. else
  44. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  45. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  46. callback error, body
  47. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  48. outputFiles = []
  49. for file in rawOutputFiles
  50. outputFiles.push
  51. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  52. type: file.type
  53. return outputFiles
  54. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  55. _buildRequest: (project_id, settingsOverride={}, callback = (error, request) ->) ->
  56. Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
  57. return callback(error) if error?
  58. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  59. if project.compiler not in ClsiManager.VALID_COMPILERS
  60. project.compiler = "pdflatex"
  61. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  62. return callback(error) if error?
  63. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  64. return callback(error) if error?
  65. resources = []
  66. rootResourcePath = null
  67. rootResourcePathOverride = null
  68. for path, doc of docs
  69. path = path.replace(/^\//, "") # Remove leading /
  70. resources.push
  71. path: path
  72. content: doc.lines.join("\n")
  73. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  74. rootResourcePath = path
  75. if settingsOverride.rootDoc_id? and doc._id.toString() == settingsOverride.rootDoc_id.toString()
  76. rootResourcePathOverride = path
  77. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  78. for path, file of files
  79. path = path.replace(/^\//, "") # Remove leading /
  80. resources.push
  81. path: path
  82. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  83. modified: file.created?.getTime()
  84. if !rootResourcePath?
  85. callback new Error("no root document exists")
  86. else
  87. callback null, {
  88. compile:
  89. options:
  90. compiler: project.compiler
  91. timeout: settingsOverride.timeout
  92. rootResourcePath: rootResourcePath
  93. resources: resources
  94. }