ClsiManager.coffee 3.9 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, options = {}, callback = (error, success) ->) ->
  11. ClsiManager._buildRequest project_id, options, (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, options.compileGroup, (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. deleteAuxFiles: (project_id, options, callback = (error) ->) ->
  23. compilerUrl = @_getCompilerUrl(options?.compileGroup)
  24. request.del "#{compilerUrl}/project/#{project_id}", callback
  25. _getCompilerUrl: (compileGroup) ->
  26. if compileGroup == "priority"
  27. return Settings.apis.clsi_priority.url
  28. else
  29. return Settings.apis.clsi.url
  30. _postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
  31. compilerUrl = @_getCompilerUrl(compileGroup)
  32. request.post {
  33. url: "#{compilerUrl}/project/#{project_id}/compile"
  34. json: req
  35. jar: false
  36. }, (error, response, body) ->
  37. return callback(error) if error?
  38. if 200 <= response.statusCode < 300
  39. callback null, body
  40. else if response.statusCode == 413
  41. callback null, compile:status:"project-too-large"
  42. else
  43. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  44. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  45. callback error, body
  46. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  47. outputFiles = []
  48. for file in rawOutputFiles
  49. outputFiles.push
  50. path: url.parse(file.url).path.replace("/project/#{project_id}/output/", "")
  51. type: file.type
  52. build: file.build
  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. }