ClsiManager.coffee 9.6 KB

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