ClsiManager.coffee 7.0 KB

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