ClsiManager.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ClsiCookieManager = require("./ClsiCookieManager")
  10. module.exports = ClsiManager =
  11. sendRequest: (project_id, options = {}, callback = (error, success) ->) ->
  12. ClsiManager._buildRequest project_id, options, (error, req) ->
  13. return callback(error) if error?
  14. logger.log project_id: project_id, "sending compile to CLSI"
  15. ClsiManager._postToClsi project_id, req, options.compileGroup, (error, response) ->
  16. if error?
  17. logger.err err:error, project_id:project_id, "error sending request to clsi"
  18. return callback(error)
  19. logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, "received compile response from CLSI"
  20. ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
  21. if err?
  22. logger.err err:err, project_id:project_id, "error getting server id"
  23. return callback(err)
  24. outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  25. callback(null, response?.compile?.status, outputFiles, clsiServerId)
  26. deleteAuxFiles: (project_id, options, callback = (error) ->) ->
  27. compilerUrl = @_getCompilerUrl(options?.compileGroup)
  28. opts =
  29. url:"#{compilerUrl}/project/#{project_id}"
  30. method:"DELETE"
  31. ClsiManager._makeRequest project_id, opts, callback
  32. _makeRequest: (project_id, opts, callback)->
  33. ClsiCookieManager.getCookieJar project_id, (err, jar)->
  34. if err?
  35. logger.err err:err, "error getting cookie jar for clsi request"
  36. return callback(err)
  37. opts.jar = jar
  38. request opts, (err, response, body)->
  39. if err?
  40. logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
  41. return callback(err)
  42. ClsiCookieManager.setServerId project_id, response, (err)->
  43. if err?
  44. logger.warn err:err, project_id:project_id, "error setting server id"
  45. return callback err, response, body
  46. _getCompilerUrl: (compileGroup) ->
  47. return Settings.apis.clsi.url
  48. _postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
  49. compilerUrl = Settings.apis.clsi.url
  50. opts =
  51. url: "#{compilerUrl}/project/#{project_id}/compile"
  52. json: req
  53. method: "POST"
  54. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  55. return callback(error) if error?
  56. if 200 <= response.statusCode < 300
  57. callback null, body
  58. else if response.statusCode == 413
  59. callback null, compile:status:"project-too-large"
  60. else
  61. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  62. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  63. callback error, body
  64. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  65. outputFiles = []
  66. for file in rawOutputFiles
  67. path = Url.parse(file.url).path
  68. path = path.replace("/project/#{project_id}/output/", "")
  69. outputFiles.push
  70. path: path
  71. type: file.type
  72. build: file.build
  73. return outputFiles
  74. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  75. _buildRequest: (project_id, options={}, callback = (error, request) ->) ->
  76. Project.findById project_id, {compiler: 1, rootDoc_id: 1, imageName: 1}, (error, project) ->
  77. return callback(error) if error?
  78. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  79. if project.compiler not in ClsiManager.VALID_COMPILERS
  80. project.compiler = "pdflatex"
  81. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  82. return callback(error) if error?
  83. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  84. return callback(error) if error?
  85. resources = []
  86. rootResourcePath = null
  87. rootResourcePathOverride = null
  88. for path, doc of docs
  89. path = path.replace(/^\//, "") # Remove leading /
  90. resources.push
  91. path: path
  92. content: doc.lines.join("\n")
  93. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  94. rootResourcePath = path
  95. if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
  96. rootResourcePathOverride = path
  97. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  98. if !rootResourcePath?
  99. logger.warn {project_id}, "no root document found, setting to main.tex"
  100. rootResourcePath = "main.tex"
  101. for path, file of files
  102. path = path.replace(/^\//, "") # Remove leading /
  103. resources.push
  104. path: path
  105. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  106. modified: file.created?.getTime()
  107. callback null, {
  108. compile:
  109. options:
  110. compiler: project.compiler
  111. timeout: options.timeout
  112. imageName: project.imageName
  113. draft: !!options.draft
  114. rootResourcePath: rootResourcePath
  115. resources: resources
  116. }
  117. wordCount: (project_id, file, options, callback = (error, response) ->) ->
  118. ClsiManager._buildRequest project_id, options, (error, req) ->
  119. compilerUrl = ClsiManager._getCompilerUrl(options?.compileGroup)
  120. filename = file || req?.compile?.rootResourcePath
  121. wordcount_url = "#{compilerUrl}/project/#{project_id}/wordcount?file=#{encodeURIComponent(filename)}"
  122. if req.compile.options.imageName?
  123. wordcount_url += "&image=#{encodeURIComponent(req.compile.options.imageName)}"
  124. opts =
  125. url: wordcount_url
  126. method: "GET"
  127. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  128. return callback(error) if error?
  129. if 200 <= response.statusCode < 300
  130. callback null, body
  131. else
  132. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  133. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  134. callback error, body