ClsiManager.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. Path = require "path"
  2. async = require "async"
  3. Settings = require "settings-sharelatex"
  4. request = require('request')
  5. Project = require("../../models/Project").Project
  6. ProjectGetter = require("../Project/ProjectGetter")
  7. ProjectEntityHandler = require("../Project/ProjectEntityHandler")
  8. logger = require "logger-sharelatex"
  9. Url = require("url")
  10. ClsiCookieManager = require("./ClsiCookieManager")
  11. ClsiStateManager = require("./ClsiStateManager")
  12. _ = require("underscore")
  13. async = require("async")
  14. ClsiFormatChecker = require("./ClsiFormatChecker")
  15. DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
  16. module.exports = ClsiManager =
  17. sendRequest: (project_id, user_id, options = {}, callback) ->
  18. ClsiManager.sendRequestOnce project_id, user_id, options, (error, status, result...) ->
  19. return callback(error) if error?
  20. if status is 'conflict'
  21. options = _.clone(options)
  22. options.syncType = "full" # force full compile
  23. ClsiManager.sendRequestOnce project_id, user_id, options, callback # try again
  24. else
  25. callback(error, status, result...)
  26. sendRequestOnce: (project_id, user_id, options = {}, callback = (error, status, outputFiles, clsiServerId, validationProblems) ->) ->
  27. ClsiManager._buildRequest project_id, options, (error, req) ->
  28. return callback(error) if error?
  29. logger.log project_id: project_id, "sending compile to CLSI"
  30. ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)->
  31. if err?
  32. logger.err err, project_id, "could not check resources for potential problems before sending to clsi"
  33. return callback(err)
  34. if validationProblems?
  35. logger.log project_id:project_id, validationProblems:validationProblems, "problems with users latex before compile was attempted"
  36. return callback(null, "validation-problems", null, null, validationProblems)
  37. ClsiManager._postToClsi project_id, user_id, req, options.compileGroup, (error, response) ->
  38. if error?
  39. logger.err err:error, project_id:project_id, "error sending request to clsi"
  40. return callback(error)
  41. logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, compile_status: response?.compile?.status, "received compile response from CLSI"
  42. ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
  43. if err?
  44. logger.err err:err, project_id:project_id, "error getting server id"
  45. return callback(err)
  46. outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  47. callback(null, response?.compile?.status, outputFiles, clsiServerId)
  48. stopCompile: (project_id, user_id, options, callback = (error) ->) ->
  49. compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id, "compile/stop")
  50. opts =
  51. url:compilerUrl
  52. method:"POST"
  53. ClsiManager._makeRequest project_id, opts, callback
  54. deleteAuxFiles: (project_id, user_id, options, callback = (error) ->) ->
  55. compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id)
  56. opts =
  57. url:compilerUrl
  58. method:"DELETE"
  59. ClsiManager._makeRequest project_id, opts, callback
  60. _makeRequest: (project_id, opts, callback)->
  61. ClsiCookieManager.getCookieJar project_id, (err, jar)->
  62. if err?
  63. logger.err err:err, "error getting cookie jar for clsi request"
  64. return callback(err)
  65. opts.jar = jar
  66. request opts, (err, response, body)->
  67. if err?
  68. logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
  69. return callback(err)
  70. ClsiCookieManager.setServerId project_id, response, (err)->
  71. if err?
  72. logger.warn err:err, project_id:project_id, "error setting server id"
  73. return callback err, response, body
  74. _getCompilerUrl: (compileGroup, project_id, user_id, action) ->
  75. host = Settings.apis.clsi.url
  76. path = "/project/#{project_id}"
  77. path += "/user/#{user_id}" if user_id?
  78. path += "/#{action}" if action?
  79. return "#{host}#{path}"
  80. _postToClsi: (project_id, user_id, req, compileGroup, callback = (error, response) ->) ->
  81. compileUrl = @_getCompilerUrl(compileGroup, project_id, user_id, "compile")
  82. opts =
  83. url: compileUrl
  84. json: req
  85. method: "POST"
  86. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  87. return callback(error) if error?
  88. if 200 <= response.statusCode < 300
  89. callback null, body
  90. else if response.statusCode == 413
  91. callback null, compile:status:"project-too-large"
  92. else if response.statusCode == 409
  93. callback null, compile:status:"conflict"
  94. else
  95. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  96. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  97. callback error, body
  98. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  99. outputFiles = []
  100. for file in rawOutputFiles
  101. outputFiles.push
  102. path: file.path # the clsi is now sending this to web
  103. url: Url.parse(file.url).path # the location of the file on the clsi, excluding the host part
  104. type: file.type
  105. build: file.build
  106. return outputFiles
  107. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  108. _buildRequest: (project_id, options={}, callback = (error, request) ->) ->
  109. ProjectGetter.getProject project_id, {compiler: 1, rootDoc_id: 1, imageName: 1, rootFolder:1}, (error, project) ->
  110. return callback(error) if error?
  111. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  112. if project.compiler not in ClsiManager.VALID_COMPILERS
  113. project.compiler = "pdflatex"
  114. if options.syncType? # new way, either incremental or full
  115. ClsiManager.getContentFromDocUpdaterIfMatch project_id, project, (error, projectStateHash, docUpdaterDocs) ->
  116. return callback(error) if error?
  117. logger.log project_id: project_id, projectStateHash: projectStateHash, docs: docUpdaterDocs?, "checked project state"
  118. # see if we can send an incremental update to the CLSI
  119. if docUpdaterDocs? and options.syncType isnt "full"
  120. ClsiManager._buildRequestFromDocupdater project_id, options, project, projectStateHash, docUpdaterDocs, callback
  121. else
  122. ClsiManager._buildRequestFromMongo project_id, options, project, projectStateHash, callback
  123. else # old way, always from mongo
  124. ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
  125. return callback(error) if error?
  126. ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
  127. getContentFromDocUpdaterIfMatch: (project_id, project, callback = (error, projectStateHash, docs) ->) ->
  128. ClsiStateManager.computeHash project, (error, projectStateHash) ->
  129. return callback(error) if error?
  130. DocumentUpdaterHandler.getProjectDocsIfMatch project_id, projectStateHash, (error, docs) ->
  131. return callback(error) if error?
  132. callback(null, projectStateHash, docs)
  133. _buildRequestFromDocupdater: (project_id, options, project, projectStateHash, docUpdaterDocs, callback = (error, request) ->) ->
  134. ProjectEntityHandler.getAllDocPathsFromProject project, (error, docPath) ->
  135. return callback(error) if error?
  136. docs = {}
  137. for doc in docUpdaterDocs or []
  138. path = docPath[doc._id]
  139. docs[path] = doc
  140. # send new docs but not files as those are already on the clsi
  141. options = _.clone(options)
  142. options.syncType = "incremental"
  143. options.syncState = projectStateHash
  144. ClsiManager._finaliseRequest project_id, options, project, docs, [], callback
  145. _buildRequestFromMongo: (project_id, options, project, projectStateHash, callback = (error, request) ->) ->
  146. ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
  147. return callback(error) if error?
  148. options = _.clone(options)
  149. options.syncType = "full"
  150. options.syncState = projectStateHash
  151. ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
  152. _getContentFromDocUpdater: (project_id, callback = (error, docs) ->) ->
  153. DocumentUpdaterHandler.getProjectDocs project_id, (error, docs) ->
  154. return callback(error) if error?
  155. callback(null, docs)
  156. _getContentFromMongo: (project_id, callback = (error, docs, files) ->) ->
  157. DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
  158. return callback(error) if error?
  159. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  160. return callback(error) if error?
  161. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  162. return callback(error) if error?
  163. callback(null, docs, files)
  164. _finaliseRequest: (project_id, options, project, docs, files, callback = (error, params) -> ) ->
  165. resources = []
  166. rootResourcePath = null
  167. rootResourcePathOverride = null
  168. for path, doc of docs
  169. path = path.replace(/^\//, "") # Remove leading /
  170. resources.push
  171. path: path
  172. content: doc.lines.join("\n")
  173. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  174. rootResourcePath = path
  175. if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
  176. rootResourcePathOverride = path
  177. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  178. if !rootResourcePath?
  179. logger.warn {project_id}, "no root document found, setting to main.tex"
  180. rootResourcePath = "main.tex"
  181. for path, file of files
  182. path = path.replace(/^\//, "") # Remove leading /
  183. resources.push
  184. path: path
  185. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  186. modified: file.created?.getTime()
  187. callback null, {
  188. compile:
  189. options:
  190. compiler: project.compiler
  191. timeout: options.timeout
  192. imageName: project.imageName
  193. draft: !!options.draft
  194. check: options.check
  195. syncType: options.syncType
  196. syncState: options.syncState
  197. rootResourcePath: rootResourcePath
  198. resources: resources
  199. }
  200. wordCount: (project_id, user_id, file, options, callback = (error, response) ->) ->
  201. ClsiManager._buildRequest project_id, options, (error, req) ->
  202. filename = file || req?.compile?.rootResourcePath
  203. wordcount_url = ClsiManager._getCompilerUrl(options?.compileGroup, project_id, user_id, "wordcount")
  204. opts =
  205. url: wordcount_url
  206. qs:
  207. file: filename
  208. image: req.compile.options.imageName
  209. method: "GET"
  210. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  211. return callback(error) if error?
  212. if 200 <= response.statusCode < 300
  213. callback null, body
  214. else
  215. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  216. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  217. callback error, body